1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
7
|
|
|
use Doctrine\DBAL\Driver\DriverException as InnerDriverException; |
8
|
|
|
use Doctrine\Tests\DbalTestCase; |
9
|
|
|
use Doctrine\DBAL\Driver; |
10
|
|
|
|
11
|
|
|
class DBALExceptionTest extends DbalTestCase |
12
|
|
|
{ |
13
|
|
View Code Duplication |
public function testDriverExceptionDuringQueryAcceptsBinaryData() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/* @var $driver Driver */ |
16
|
|
|
$driver = $this->createMock(Driver::class); |
17
|
|
|
$e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128))); |
18
|
|
|
self::assertContains('with params ["ABC", "\x80"]', $e->getMessage()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
View Code Duplication |
public function testDriverExceptionDuringQueryAcceptsResource() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/* @var $driver Driver */ |
24
|
|
|
$driver = $this->createMock(Driver::class); |
25
|
|
|
$e = \Doctrine\DBAL\DBALException::driverExceptionDuringQuery($driver, new \Exception, "INSERT INTO file (`content`) VALUES (?)", [1 => fopen(__FILE__, 'r')]); |
26
|
|
|
self::assertContains('Resource', $e->getMessage()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testAvoidOverWrappingOnDriverException() |
30
|
|
|
{ |
31
|
|
|
/* @var $driver Driver */ |
32
|
|
|
$driver = $this->createMock(Driver::class); |
33
|
|
|
$inner = new class extends \Exception implements InnerDriverException |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function getErrorCode() |
39
|
|
|
{ |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function getSQLState() |
46
|
|
|
{ |
47
|
|
|
} |
48
|
|
|
}; |
49
|
|
|
$ex = new DriverException('', $inner); |
50
|
|
|
$e = DBALException::driverExceptionDuringQuery($driver, $ex, ''); |
51
|
|
|
self::assertSame($ex, $e); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testDriverRequiredWithUrl() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$url = 'mysql://localhost'; |
57
|
|
|
$exception = DBALException::driverRequired($url); |
58
|
|
|
|
59
|
|
|
self::assertInstanceOf(DBALException::class, $exception); |
60
|
|
|
self::assertSame( |
61
|
|
|
sprintf( |
62
|
|
|
"The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " . |
63
|
|
|
'is given to DriverManager::getConnection(). Given URL: %s', |
64
|
|
|
$url |
65
|
|
|
), |
66
|
|
|
$exception->getMessage() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @group #2821 |
72
|
|
|
*/ |
73
|
|
|
public function testInvalidPlatformTypeObject(): void |
74
|
|
|
{ |
75
|
|
|
$exception = DBALException::invalidPlatformType(new \stdClass()); |
76
|
|
|
|
77
|
|
|
self::assertSame( |
78
|
|
|
"Option 'platform' must be a subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform', instance of 'stdClass' given", |
79
|
|
|
$exception->getMessage() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @group #2821 |
85
|
|
|
*/ |
86
|
|
|
public function testInvalidPlatformTypeScalar(): void |
87
|
|
|
{ |
88
|
|
|
$exception = DBALException::invalidPlatformType('some string'); |
89
|
|
|
|
90
|
|
|
self::assertSame( |
91
|
|
|
"Option 'platform' must be an object and subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform'. Got 'string'", |
92
|
|
|
$exception->getMessage() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.