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

namespace Doctrine\Tests\DBAL;

use Doctrine\DBAL\DBALException;
Sergei Morozov's avatar
Sergei Morozov committed
6
use Doctrine\DBAL\Driver;
7
use Doctrine\DBAL\Driver\DriverException as InnerDriverException;
Sergei Morozov's avatar
Sergei Morozov committed
8
use Doctrine\DBAL\Exception\DriverException;
9
use Doctrine\Tests\DbalTestCase;
Sergei Morozov's avatar
Sergei Morozov committed
10 11
use Exception;
use stdClass;
12 13 14
use function chr;
use function fopen;
use function sprintf;
15

16
class DBALExceptionTest extends DbalTestCase
17 18 19
{
    public function testDriverExceptionDuringQueryAcceptsBinaryData()
    {
Sergei Morozov's avatar
Sergei Morozov committed
20
        /** @var Driver $driver */
21
        $driver = $this->createMock(Driver::class);
Sergei Morozov's avatar
Sergei Morozov committed
22
        $e      = DBALException::driverExceptionDuringQuery($driver, new Exception(), '', ['ABC', chr(128)]);
23
        self::assertContains('with params ["ABC", "\x80"]', $e->getMessage());
24
    }
Sergei Morozov's avatar
Sergei Morozov committed
25

26 27
    public function testDriverExceptionDuringQueryAcceptsResource()
    {
Sergei Morozov's avatar
Sergei Morozov committed
28
        /** @var Driver $driver */
29
        $driver = $this->createMock(Driver::class);
Sergei Morozov's avatar
Sergei Morozov committed
30
        $e      = DBALException::driverExceptionDuringQuery($driver, new Exception(), 'INSERT INTO file (`content`) VALUES (?)', [1 => fopen(__FILE__, 'r')]);
31 32
        self::assertContains('Resource', $e->getMessage());
    }
33 34 35

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

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

    public function testDriverRequiredWithUrl()
    {
Sergei Morozov's avatar
Sergei Morozov committed
61
        $url       = 'mysql://localhost';
62 63
        $exception = DBALException::driverRequired($url);

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

    /**
76
     * @group #2821
77
     */
Sergei Morozov's avatar
Sergei Morozov committed
78
    public function testInvalidPlatformTypeObject() : void
79
    {
Sergei Morozov's avatar
Sergei Morozov committed
80
        $exception = DBALException::invalidPlatformType(new stdClass());
81

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

    /**
89
     * @group #2821
90
     */
Sergei Morozov's avatar
Sergei Morozov committed
91
    public function testInvalidPlatformTypeScalar() : void
92
    {
93
        $exception = DBALException::invalidPlatformType('some string');
94 95 96 97 98 99

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