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
|
|
|
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
|
|
|
$this->assertContains('with params ["ABC", "\x80"]', $e->getMessage()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testAvoidOverWrappingOnDriverException() |
22
|
|
|
{ |
23
|
|
|
/* @var $driver Driver */ |
24
|
|
|
$driver = $this->createMock(Driver::class); |
25
|
|
|
$inner = new class extends \Exception implements InnerDriverException |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
*/ |
30
|
|
|
public function getErrorCode() |
31
|
|
|
{ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function getSQLState() |
38
|
|
|
{ |
39
|
|
|
} |
40
|
|
|
}; |
41
|
|
|
$ex = new DriverException('', $inner); |
42
|
|
|
$e = DBALException::driverExceptionDuringQuery($driver, $ex, ''); |
43
|
|
|
$this->assertSame($ex, $e); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testDriverRequiredWithUrl() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$url = 'mysql://localhost'; |
49
|
|
|
$exception = DBALException::driverRequired($url); |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf(DBALException::class, $exception); |
52
|
|
|
$this->assertSame( |
53
|
|
|
sprintf( |
54
|
|
|
"The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " . |
55
|
|
|
'is given to DriverManager::getConnection(). Given URL: %s', |
56
|
|
|
$url |
57
|
|
|
), |
58
|
|
|
$exception->getMessage() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
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.