Completed
Pull Request — develop (#3536)
by Jonathan
60:42
created

DBALExceptionTest::testDriverRequiredWithUrl()

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
eloc 9
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Driver;
9
use Doctrine\DBAL\Driver\DriverException as InnerDriverException;
10
use Doctrine\DBAL\Exception\DriverException;
11
use Doctrine\DBAL\Exception\DriverRequired;
12
use Doctrine\DBAL\Exception\InvalidPlatformType;
13
use Doctrine\Tests\DbalTestCase;
14
use Exception;
15
use stdClass;
16
use function chr;
17
use function fopen;
18
use function sprintf;
19
use function tmpfile;
20
21
class DBALExceptionTest extends DbalTestCase
22
{
23
    public function testDriverExceptionDuringQueryAcceptsBinaryData()
24
    {
25
        /** @var Driver $driver */
26
        $driver = $this->createMock(Driver::class);
27
        $e      = DBALException::driverExceptionDuringQuery($driver, new Exception(), '', ['ABC', chr(128)]);
28
        self::assertStringContainsString('with params ["ABC", "\x80"]', $e->getMessage());
29
    }
30
31
    public function testDriverExceptionDuringQueryAcceptsResource()
32
    {
33
        /** @var Driver $driver */
34
        $driver = $this->createMock(Driver::class);
35
        $e      = DBALException::driverExceptionDuringQuery($driver, new Exception(), 'INSERT INTO file (`content`) VALUES (?)', [1 => fopen(__FILE__, 'r')]);
36
        self::assertStringContainsString('Resource', $e->getMessage());
37
    }
38
39
    public function testAvoidOverWrappingOnDriverException()
40
    {
41
        /** @var Driver $driver */
42
        $driver = $this->createMock(Driver::class);
43
        $inner  = new class extends Exception implements InnerDriverException
44
        {
45
            /**
46
             * {@inheritDoc}
47
             */
48
            public function getSQLState() : ?string
49
            {
50
            }
51
        };
52
        $ex     = new DriverException('', $inner);
53
        $e      = DBALException::driverExceptionDuringQuery($driver, $ex, '');
54
        self::assertSame($ex, $e);
55
    }
56
57
    public function testDriverRequiredWithUrl()
58
    {
59
        $url       = 'mysql://localhost';
60
        $exception = DriverRequired::new($url);
61
62
        self::assertInstanceOf(DBALException::class, $exception);
63
        self::assertSame(
64
            sprintf(
65
                'The options "driver" or "driverClass" are mandatory if a connection URL without scheme ' .
66
                'is given to DriverManager::getConnection(). Given URL "%s".',
67
                $url
68
            ),
69
            $exception->getMessage()
70
        );
71
    }
72
73
    /**
74
     * @group #2821
75
     */
76
    public function testInvalidPlatformTypeObject() : void
77
    {
78
        $exception = InvalidPlatformType::new(new stdClass());
79
80
        self::assertSame(
81
            'Option "platform" must be a subtype of "Doctrine\DBAL\Platforms\AbstractPlatform", instance of "stdClass" given.',
82
            $exception->getMessage()
83
        );
84
    }
85
86
    /**
87
     * @group #2821
88
     */
89
    public function testInvalidPlatformTypeScalar() : void
90
    {
91
        $exception = InvalidPlatformType::new('some string');
92
93
        self::assertSame(
94
            'Option "platform" must be an object and subtype of "Doctrine\DBAL\Platforms\AbstractPlatform". Got "string".',
95
            $exception->getMessage()
96
        );
97
    }
98
99
    /**
100
     * @dataProvider provideDataForFormatValue
101
     */
102
    public function testFormatValue($expected, $value) : void
103
    {
104
        self::assertSame($expected, DBALException::formatValue($value));
105
    }
106
107
    /**
108
     * @return array<int, array<int, mixed>>
109
     */
110
    public function provideDataForFormatValue() : array
111
    {
112
        return [
113
            ['', ''],
114
            ['test', 'test'],
115
            ['NULL', null],
116
            ['stdClass', new stdClass()],
117
            ['stream', tmpfile()],
118
            ['true', true],
119
            ['false', false],
120
            ['[true, 1, 2, 3, "test"]', [true, 1, 2, 3, 'test']],
121
        ];
122
    }
123
}
124