DBALExceptionTest.php 3.01 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL;

use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Exception\DriverException;
7 8 9
use Doctrine\DBAL\Driver\DriverException as InnerDriverException;
use Doctrine\Tests\DbalTestCase;
use Doctrine\DBAL\Driver;
10 11 12
use function chr;
use function fopen;
use function sprintf;
13

14
class DBALExceptionTest extends DbalTestCase
15 16 17
{
    public function testDriverExceptionDuringQueryAcceptsBinaryData()
    {
18 19
        /* @var $driver Driver */
        $driver = $this->createMock(Driver::class);
20
        $e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128)));
21
        self::assertContains('with params ["ABC", "\x80"]', $e->getMessage());
22
    }
23 24 25 26 27
    
    public function testDriverExceptionDuringQueryAcceptsResource()
    {
        /* @var $driver Driver */
        $driver = $this->createMock(Driver::class);
28
        $e = \Doctrine\DBAL\DBALException::driverExceptionDuringQuery($driver, new \Exception, "INSERT INTO file (`content`) VALUES (?)", [1 => fopen(__FILE__, 'r')]);
29 30
        self::assertContains('Resource', $e->getMessage());
    }
31 32 33

    public function testAvoidOverWrappingOnDriverException()
    {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        /* @var $driver Driver */
        $driver = $this->createMock(Driver::class);
        $inner = new class extends \Exception implements InnerDriverException
        {
            /**
             * {@inheritDoc}
             */
            public function getErrorCode()
            {
            }

            /**
             * {@inheritDoc}
             */
            public function getSQLState()
            {
            }
        };
        $ex = new DriverException('', $inner);
53
        $e = DBALException::driverExceptionDuringQuery($driver, $ex, '');
54
        self::assertSame($ex, $e);
55
    }
56 57 58 59 60 61

    public function testDriverRequiredWithUrl()
    {
        $url = 'mysql://localhost';
        $exception = DBALException::driverRequired($url);

62 63
        self::assertInstanceOf(DBALException::class, $exception);
        self::assertSame(
64 65
            sprintf(
                "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
66
                'is given to DriverManager::getConnection(). Given URL: %s',
67 68 69 70 71
                $url
            ),
            $exception->getMessage()
        );
    }
72 73

    /**
74
     * @group #2821
75
     */
76
    public function testInvalidPlatformTypeObject(): void
77
    {
78
        $exception = DBALException::invalidPlatformType(new \stdClass());
79

80 81 82 83 84 85 86
        self::assertSame(
            "Option 'platform' must be a subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform', instance of 'stdClass' given",
            $exception->getMessage()
        );
    }

    /**
87
     * @group #2821
88
     */
89
    public function testInvalidPlatformTypeScalar(): void
90
    {
91
        $exception = DBALException::invalidPlatformType('some string');
92 93 94 95 96 97

        self::assertSame(
            "Option 'platform' must be an object and subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform'. Got 'string'",
            $exception->getMessage()
        );
    }
98
}