Completed
Pull Request — master (#750)
by Mike
11:44
created

FakeDriver::getDatabasePlatform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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->getMockBuilder(Driver\AbstractDriverException::class)->disableOriginalConstructor()->getMock());
0 ignored issues
show
Compatibility introduced by
$this->getMockBuilder(\D...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is not a sub-type of object<Doctrine\DBAL\Driver\DriverException>. It seems like you assume a child interface of the interface PHPUnit\Framework\MockObject\MockObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

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