Failed Conditions
Pull Request — develop (#3367)
by Benjamin
12:47
created

php$0 ➔ testDriverRequiredWithUrl()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A DBALExceptionTest::testInvalidPlatformTypeObject() 0 7 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Driver;
7
use Doctrine\DBAL\Driver\DriverException as InnerDriverException;
8
use Doctrine\DBAL\Exception\DriverException;
9
use Doctrine\Tests\DbalTestCase;
10
use Exception;
11
use stdClass;
12
use function chr;
13
use function fopen;
14
use function sprintf;
15
16
class DBALExceptionTest extends DbalTestCase
17
{
18
    public function testDriverExceptionDuringQueryAcceptsBinaryData()
19
    {
20
        /** @var Driver $driver */
21
        $driver = $this->createMock(Driver::class);
22
        $e      = DBALException::driverExceptionDuringQuery($driver, new Exception(), '', ['ABC', chr(128)]);
23
        self::assertContains('with params ["ABC", "\x80"]', $e->getMessage());
24
    }
25
26
    public function testDriverExceptionDuringQueryAcceptsResource()
27
    {
28
        /** @var Driver $driver */
29
        $driver = $this->createMock(Driver::class);
30
        $e      = DBALException::driverExceptionDuringQuery($driver, new Exception(), 'INSERT INTO file (`content`) VALUES (?)', [1 => fopen(__FILE__, 'r')]);
31
        self::assertContains('Resource', $e->getMessage());
32
    }
33
34
    public function testAvoidOverWrappingOnDriverException()
35
    {
36
        /** @var Driver $driver */
37
        $driver = $this->createMock(Driver::class);
38
        $inner  = new InnerDriverException('');
39
        $ex     = new DriverException('', $inner);
40
        $e      = DBALException::driverExceptionDuringQuery($driver, $ex, '');
41
        self::assertSame($ex, $e);
42
    }
43
44
    public function testDriverRequiredWithUrl()
45
    {
46
        $url       = 'mysql://localhost';
47
        $exception = DBALException::driverRequired($url);
48
49
        self::assertInstanceOf(DBALException::class, $exception);
50
        self::assertSame(
51
            sprintf(
52
                "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
53
                'is given to DriverManager::getConnection(). Given URL: %s',
54
                $url
55
            ),
56
            $exception->getMessage()
57
        );
58
    }
59
60
    /**
61
     * @group #2821
62
     */
63
    public function testInvalidPlatformTypeObject() : void
64
    {
65
        $exception = DBALException::invalidPlatformType(new stdClass());
66
67
        self::assertSame(
68
            "Option 'platform' must be a subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform', instance of 'stdClass' given",
69
            $exception->getMessage()
70
        );
71
    }
72
73
    /**
74
     * @group #2821
75
     */
76
    public function testInvalidPlatformTypeScalar() : void
77
    {
78
        $exception = DBALException::invalidPlatformType('some string');
79
80
        self::assertSame(
81
            "Option 'platform' must be an object and subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform'. Got 'string'",
82
            $exception->getMessage()
83
        );
84
    }
85
}
86