1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine Bundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project, Benjamin Eberlei <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests; |
16
|
|
|
|
17
|
|
|
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory; |
18
|
|
|
use Doctrine\DBAL\Connection; |
19
|
|
|
use Doctrine\DBAL\Driver; |
20
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
21
|
|
|
|
22
|
|
|
class ConnectionFactoryTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
protected function setUp() |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
if (!class_exists('Doctrine\\ORM\\Version')) { |
29
|
|
|
$this->markTestSkipped('Doctrine ORM is not available.'); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @expectedException \Doctrine\DBAL\DBALException |
35
|
|
|
*/ |
36
|
|
|
public function testContainer() |
37
|
|
|
{ |
38
|
|
|
$typesConfig = []; |
39
|
|
|
$factory = new ConnectionFactory($typesConfig); |
40
|
|
|
$params = ['driverClass' => '\\Doctrine\\Bundle\\DoctrineBundle\\Tests\\FakeDriver']; |
41
|
|
|
$config = null; |
42
|
|
|
$eventManager = null; |
43
|
|
|
$mappingTypes = [0]; |
44
|
|
|
$exception = new DriverException('', $this->createMock('\Doctrine\DBAL\Driver\DriverException')); |
45
|
|
|
|
46
|
|
|
// put the mock into the fake driver |
47
|
|
|
FakeDriver::$exception = $exception; |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$factory->createConnection($params, $config, $eventManager, $mappingTypes); |
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
$this->assertTrue(strpos($e->getMessage(), 'can circumvent this by setting') > 0); |
53
|
|
|
throw $e; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* FakeDriver class to simulate a problem discussed in DoctrineBundle issue #673 |
60
|
|
|
* In order to not use a real database driver we have to create our own fake/mock implementation. |
61
|
|
|
* |
62
|
|
|
* @link https://github.com/doctrine/DoctrineBundle/issues/673 |
63
|
|
|
*/ |
64
|
|
|
class FakeDriver implements Driver |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* Exception Mock |
68
|
|
|
* |
69
|
|
|
* @var \Doctrine\DBAL\Exception\DriverException |
70
|
|
|
*/ |
71
|
|
|
public static $exception; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This method gets called to determine the database version which in our case leeds to the problem. |
75
|
|
|
* So we have to fake the exception a driver would normally throw. |
76
|
|
|
* |
77
|
|
|
* |
78
|
|
|
* @link https://github.com/doctrine/DoctrineBundle/issues/673 |
79
|
|
|
*/ |
80
|
|
|
public function getDatabasePlatform() |
81
|
|
|
{ |
82
|
|
|
throw self::$exception; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// ----- below this line follow only dummy methods to satisfy the interface requirements ---- |
86
|
|
|
|
87
|
|
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) |
88
|
|
|
{ |
89
|
|
|
throw new \Exception('not implemented'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getSchemaManager(Connection $conn) |
93
|
|
|
{ |
94
|
|
|
throw new \Exception('not implemented'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getName() |
98
|
|
|
{ |
99
|
|
|
return 'FakeDriver'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getDatabase(Connection $conn) |
103
|
|
|
{ |
104
|
|
|
return 'fake_db'; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.