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