Completed
Pull Request — master (#677)
by Sebastian
03:34
created

ConnectionFactoryTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
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