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 Doctrine\ORM\Version; |
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
class ConnectionFactoryTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
protected function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
if (class_exists(Version::class)) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->markTestSkipped('Doctrine ORM is not available.'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @expectedException \Doctrine\DBAL\DBALException |
29
|
|
|
*/ |
30
|
|
|
public function testContainer() |
31
|
|
|
{ |
32
|
|
|
$typesConfig = []; |
33
|
|
|
$factory = new ConnectionFactory($typesConfig); |
34
|
|
|
$params = ['driverClass' => FakeDriver::class]; |
35
|
|
|
$config = null; |
36
|
|
|
$eventManager = null; |
37
|
|
|
$mappingTypes = [0]; |
38
|
|
|
$exception = new DriverException('', $this->createMock(Driver\AbstractDriverException::class)); |
|
|
|
|
39
|
|
|
|
40
|
|
|
// put the mock into the fake driver |
41
|
|
|
FakeDriver::$exception = $exception; |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$factory->createConnection($params, $config, $eventManager, $mappingTypes); |
45
|
|
|
} catch (Exception $e) { |
46
|
|
|
$this->assertTrue(strpos($e->getMessage(), 'can circumvent this by setting') > 0); |
47
|
|
|
throw $e; |
48
|
|
|
} finally { |
49
|
|
|
FakeDriver::$exception = null; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testDefaultCharset() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$factory = new ConnectionFactory([]); |
56
|
|
|
$params = ['driverClass' => FakeDriver::class]; |
57
|
|
|
|
58
|
|
|
$connection = $factory->createConnection($params); |
59
|
|
|
|
60
|
|
|
$this->assertSame('utf8', $connection->getParams()['charset']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @requires extension pdo |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function testDefaultCharsetMySql() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$factory = new ConnectionFactory([]); |
69
|
|
|
$params = ['driver' => 'pdo_mysql']; |
70
|
|
|
|
71
|
|
|
$connection = $factory->createConnection($params); |
72
|
|
|
|
73
|
|
|
$this->assertSame('utf8mb4', $connection->getParams()['charset']); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* FakeDriver class to simulate a problem discussed in DoctrineBundle issue #673 |
79
|
|
|
* In order to not use a real database driver we have to create our own fake/mock implementation. |
80
|
|
|
* |
81
|
|
|
* @link https://github.com/doctrine/DoctrineBundle/issues/673 |
82
|
|
|
*/ |
83
|
|
|
class FakeDriver implements Driver |
84
|
|
|
{ |
85
|
|
|
/** |
86
|
|
|
* Exception Mock |
87
|
|
|
* |
88
|
|
|
* @var DriverException |
89
|
|
|
*/ |
90
|
|
|
public static $exception; |
91
|
|
|
|
92
|
|
|
/** @var AbstractPlatform|null */ |
93
|
|
|
public static $platform; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* This method gets called to determine the database version which in our case leeds to the problem. |
97
|
|
|
* So we have to fake the exception a driver would normally throw. |
98
|
|
|
* |
99
|
|
|
* @link https://github.com/doctrine/DoctrineBundle/issues/673 |
100
|
|
|
*/ |
101
|
|
|
public function getDatabasePlatform() |
102
|
|
|
{ |
103
|
|
|
if (self::$exception !== null) { |
104
|
|
|
throw self::$exception; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return static::$platform ?? new MySqlPlatform(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// ----- below this line follow only dummy methods to satisfy the interface requirements ---- |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed[] $params |
114
|
|
|
* @param string|null $username |
115
|
|
|
* @param string|null $password |
116
|
|
|
* @param mixed[] $driverOptions |
117
|
|
|
*/ |
118
|
|
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) |
119
|
|
|
{ |
120
|
|
|
throw new Exception('not implemented'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getSchemaManager(Connection $conn) |
124
|
|
|
{ |
125
|
|
|
throw new Exception('not implemented'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getName() |
129
|
|
|
{ |
130
|
|
|
return 'FakeDriver'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getDatabase(Connection $conn) |
134
|
|
|
{ |
135
|
|
|
return 'fake_db'; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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: