Completed
Pull Request — master (#1070)
by
unknown
02:03 queued 31s
created

ConnectionFactoryTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.9332
cc 2
nc 2
nop 0
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));
0 ignored issues
show
Documentation introduced by
$this->createMock(\Doctr...DriverException::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Driver\DriverException>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testDefaultCharsetMySql()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $factory = new ConnectionFactory([]);
66
        $params = ['driver' => 'pdo_mysql'];
67
68
        $connection = $factory->createConnection($params);
69
70
        $this->assertSame('utf8mb4', $connection->getParams()['charset']);
71
    }
72
}
73
74
/**
75
 * FakeDriver class to simulate a problem discussed in DoctrineBundle issue #673
76
 * In order to not use a real database driver we have to create our own fake/mock implementation.
77
 *
78
 * @link https://github.com/doctrine/DoctrineBundle/issues/673
79
 */
80
class FakeDriver implements Driver
81
{
82
    /**
83
     * Exception Mock
84
     *
85
     * @var DriverException
86
     */
87
    public static $exception;
88
89
    /** @var AbstractPlatform|null */
90
    public static $platform;
91
92
    /**
93
     * This method gets called to determine the database version which in our case leeds to the problem.
94
     * So we have to fake the exception a driver would normally throw.
95
     *
96
     * @link https://github.com/doctrine/DoctrineBundle/issues/673
97
     */
98
    public function getDatabasePlatform()
99
    {
100
        if (self::$exception !== null) {
101
            throw self::$exception;
102
        }
103
104
        return static::$platform ?? new MySqlPlatform();
105
    }
106
107
    // ----- below this line follow only dummy methods to satisfy the interface requirements ----
108
109
    /**
110
     * @param mixed[]     $params
111
     * @param string|null $username
112
     * @param string|null $password
113
     * @param mixed[]     $driverOptions
114
     */
115
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
116
    {
117
        throw new Exception('not implemented');
118
    }
119
120
    public function getSchemaManager(Connection $conn)
121
    {
122
        throw new Exception('not implemented');
123
    }
124
125
    public function getName()
126
    {
127
        return 'FakeDriver';
128
    }
129
130
    public function getDatabase(Connection $conn)
131
    {
132
        return 'fake_db';
133
    }
134
}
135