Completed
Pull Request — master (#677)
by Sebastian
02:04
created

FakeException   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 12
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLState() 0 4 1
A getErrorCode() 0 4 1
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
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
22
23
class ConnectionFactoryTest extends TestCase
24
{
25
    protected function setUp()
26
    {
27
        parent::setUp();
28
29
        if (!class_exists('Doctrine\\ORM\\Version')) {
30
            $this->markTestSkipped('Doctrine ORM is not available.');
31
        }
32
    }
33
34
    /**
35
     * @expectedException \Doctrine\DBAL\DBALException
36
     */
37
    public function testContainer()
38
    {
39
        $typesConfig  = [];
40
        $factory      = new ConnectionFactory($typesConfig);
41
        $params       = ['driverClass' => '\\Doctrine\\Bundle\\DoctrineBundle\\Tests\\FakeDriver'];
42
        $config       = null;
43
        $eventManager = null;
44
        $mappingTypes = [0];
45
46
        try {
47
            $factory->createConnection($params, $config, $eventManager, $mappingTypes);
48
        } catch (\Exception $e) {
49
            $this->assertTrue(strpos($e->getMessage(), 'can circumvent this by setting') > 0);
50
            throw $e;
51
        }
52
    }
53
}
54
55
/**
56
 * FakeDriver class to simulate a problem discussed in DoctrineBundle issue #673
57
 * In order to not use a real database driver we have to create our own fake/mock implementation.
58
 *
59
 * @link https://github.com/doctrine/DoctrineBundle/issues/673
60
 */
61
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...
62
{
63
    /**
64
     * This method gets called to determine the database version which in our case leeds to the problem.
65
     * So we have to fake the exception a driver would normally throw.
66
     *
67
     *
68
     * @link https://github.com/doctrine/DoctrineBundle/issues/673
69
     */
70
    public function getDatabasePlatform()
71
    {
72
        throw new DriverException('Fake driver throws exceptions', new FakeException('foo'));
73
    }
74
75
    // ----- below this line follow only dummy methods to satisfy the interface requirements ----
76
77
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
78
    {
79
        throw new \Exception('not implemented');
80
    }
81
82
    public function getSchemaManager(Connection $conn)
83
    {
84
        throw new \Exception('not implemented');
85
    }
86
87
    public function getName()
88
    {
89
        return 'FakeDriver';
90
    }
91
92
    public function getDatabase(Connection $conn)
93
    {
94
        return 'fake_db';
95
    }
96
}
97
98
/**
99
 * FakeException that implements the DriverException interface.
100
 * Normally these exceptions get wrapped by the DBAL component.
101
 * In order to test the enhancement for issue #673 we have to
102
 * fake such an exception our self.
103
 *
104
 * @link https://github.com/doctrine/DoctrineBundle/issues/673
105
 *
106
 * @package Doctrine\Bundle\DoctrineBundle\Tests
107
 */
108
class FakeException extends \Exception implements DriverExceptionInterface
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...
109
{
110
    public function getSQLState()
111
    {
112
        return null;
113
    }
114
115
    public function getErrorCode()
116
    {
117
        return 0;
118
    }
119
}
120