1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Driver; |
8
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
9
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
10
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
class ConnectionFactoryTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @expectedException \Doctrine\DBAL\DBALException |
17
|
|
|
*/ |
18
|
|
|
public function testContainer() : void |
19
|
|
|
{ |
20
|
|
|
$typesConfig = []; |
21
|
|
|
$factory = new ConnectionFactory($typesConfig); |
22
|
|
|
$params = ['driverClass' => FakeDriver::class]; |
23
|
|
|
$config = null; |
24
|
|
|
$eventManager = null; |
25
|
|
|
$mappingTypes = [0]; |
26
|
|
|
$exception = new DriverException('', $this->createMock(Driver\AbstractDriverException::class)); |
|
|
|
|
27
|
|
|
|
28
|
|
|
// put the mock into the fake driver |
29
|
|
|
FakeDriver::$exception = $exception; |
30
|
|
|
|
31
|
|
|
try { |
32
|
|
|
$factory->createConnection($params, $config, $eventManager, $mappingTypes); |
33
|
|
|
} catch (Exception $e) { |
34
|
|
|
$this->assertTrue(strpos($e->getMessage(), 'can circumvent this by setting') > 0); |
35
|
|
|
throw $e; |
36
|
|
|
} finally { |
37
|
|
|
FakeDriver::$exception = null; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testDefaultCharset() : void |
42
|
|
|
{ |
43
|
|
|
$factory = new ConnectionFactory([]); |
44
|
|
|
$params = [ |
45
|
|
|
'driverClass' => FakeDriver::class, |
46
|
|
|
'wrapperClass' => FakeConnection::class, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
$creationCount = FakeConnection::$creationCount; |
50
|
|
|
$connection = $factory->createConnection($params); |
51
|
|
|
|
52
|
|
|
$this->assertInstanceof(FakeConnection::class, $connection); |
53
|
|
|
$this->assertSame('utf8', $connection->getParams()['charset']); |
54
|
|
|
$this->assertSame(1 + $creationCount, FakeConnection::$creationCount); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testDefaultCharsetMySql() : void |
58
|
|
|
{ |
59
|
|
|
$factory = new ConnectionFactory([]); |
60
|
|
|
$params = ['driver' => 'pdo_mysql']; |
61
|
|
|
|
62
|
|
|
$connection = $factory->createConnection($params); |
63
|
|
|
|
64
|
|
|
$this->assertSame('utf8mb4', $connection->getParams()['charset']); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* FakeDriver class to simulate a problem discussed in DoctrineBundle issue #673 |
70
|
|
|
* In order to not use a real database driver we have to create our own fake/mock implementation. |
71
|
|
|
* |
72
|
|
|
* @link https://github.com/doctrine/DoctrineBundle/issues/673 |
73
|
|
|
*/ |
74
|
|
|
class FakeDriver implements Driver |
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* Exception Mock |
78
|
|
|
* |
79
|
|
|
* @var DriverException |
80
|
|
|
*/ |
81
|
|
|
public static $exception; |
82
|
|
|
|
83
|
|
|
/** @var AbstractPlatform|null */ |
84
|
|
|
public static $platform; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* This method gets called to determine the database version which in our case leeds to the problem. |
88
|
|
|
* So we have to fake the exception a driver would normally throw. |
89
|
|
|
* |
90
|
|
|
* @link https://github.com/doctrine/DoctrineBundle/issues/673 |
91
|
|
|
*/ |
92
|
|
|
public function getDatabasePlatform() : AbstractPlatform |
93
|
|
|
{ |
94
|
|
|
if (self::$exception !== null) { |
95
|
|
|
throw self::$exception; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return static::$platform ?? new MySqlPlatform(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// ----- below this line follow only dummy methods to satisfy the interface requirements ---- |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param mixed[] $params |
105
|
|
|
* @param string|null $username |
106
|
|
|
* @param string|null $password |
107
|
|
|
* @param mixed[] $driverOptions |
108
|
|
|
*/ |
109
|
|
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) : void |
110
|
|
|
{ |
111
|
|
|
throw new Exception('not implemented'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getSchemaManager(Connection $conn) : void |
115
|
|
|
{ |
116
|
|
|
throw new Exception('not implemented'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getName() : string |
120
|
|
|
{ |
121
|
|
|
return 'FakeDriver'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getDatabase(Connection $conn) : string |
125
|
|
|
{ |
126
|
|
|
return 'fake_db'; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
class FakeConnection extends Connection |
131
|
|
|
{ |
132
|
|
|
/** @var int */ |
133
|
|
|
public static $creationCount = 0; |
134
|
|
|
|
135
|
|
|
public function __construct(array $params, FakeDriver $driver, ?Configuration $config = null, ?EventManager $eventManager = null) |
136
|
|
|
{ |
137
|
|
|
++self::$creationCount; |
138
|
|
|
|
139
|
|
|
parent::__construct($params, $driver, $config, $eventManager); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: