DBALExceptionTest.php 2.84 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests;
4 5

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 9
use Doctrine\DBAL\Exception\DriverException;
use Exception;
10
use PHPUnit\Framework\TestCase;
Sergei Morozov's avatar
Sergei Morozov committed
11
use stdClass;
12 13 14
use function chr;
use function fopen;
use function sprintf;
15

16
class DBALExceptionTest extends TestCase
17
{
18
    public function testDriverExceptionDuringQueryAcceptsBinaryData() : void
19
    {
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::assertStringContainsString('with params ["ABC", "\x80"]', $e->getMessage());
24
    }
Sergei Morozov's avatar
Sergei Morozov committed
25

26
    public function testDriverExceptionDuringQueryAcceptsResource() : void
27
    {
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
        self::assertStringContainsString('Resource', $e->getMessage());
32
    }
33

34
    public function testAvoidOverWrappingOnDriverException() : void
35
    {
Sergei Morozov's avatar
Sergei Morozov committed
36
        /** @var Driver $driver */
37 38
        $driver = $this->createMock(Driver::class);

39 40 41 42 43
        /** @var InnerDriverException $inner */
        $inner = $this->createMock(InnerDriverException::class);

        $ex = new DriverException('', $inner);
        $e  = DBALException::driverExceptionDuringQuery($driver, $ex, '');
44
        self::assertSame($ex, $e);
45
    }
46

47
    public function testDriverRequiredWithUrl() : void
48
    {
Sergei Morozov's avatar
Sergei Morozov committed
49
        $url       = 'mysql://localhost';
50 51
        $exception = DBALException::driverRequired($url);

52 53
        self::assertInstanceOf(DBALException::class, $exception);
        self::assertSame(
54 55
            sprintf(
                "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
56
                'is given to DriverManager::getConnection(). Given URL: %s',
57 58 59 60 61
                $url
            ),
            $exception->getMessage()
        );
    }
62 63

    /**
64
     * @group #2821
65
     */
Sergei Morozov's avatar
Sergei Morozov committed
66
    public function testInvalidPlatformTypeObject() : void
67
    {
Sergei Morozov's avatar
Sergei Morozov committed
68
        $exception = DBALException::invalidPlatformType(new stdClass());
69

70 71 72 73 74 75 76
        self::assertSame(
            "Option 'platform' must be a subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform', instance of 'stdClass' given",
            $exception->getMessage()
        );
    }

    /**
77
     * @group #2821
78
     */
Sergei Morozov's avatar
Sergei Morozov committed
79
    public function testInvalidPlatformTypeScalar() : void
80
    {
81
        $exception = DBALException::invalidPlatformType('some string');
82 83 84 85 86 87

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