Completed
Pull Request — 2.11.x (#2448)
by
unknown
12:14
created

ConnectorTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
c 1
b 0
f 0
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A failingDriverMock() 0 15 3
A getParams() 0 36 5
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Connection\Connector;
4
5
use Doctrine\DBAL\Driver;
6
use Doctrine\DBAL\Driver\Connection as DriverConnection;
7
use Doctrine\DBAL\Exception\ConnectionException;
8
use Doctrine\Tests\DbalTestCase;
9
use function array_rand;
10
use function is_array;
11
12
abstract class ConnectorTest extends DbalTestCase
13
{
14
    /**
15
     * @return array
16
     */
17
    protected function getParams(int $slaves = 2, int $failSlaves = 0, bool $failMaster = false) : array
18
    {
19
        $params = [
20
            'master' => [
21
                'driver' => 'pdo_sqlite',
22
                'user' => 'root',
23
                'password' => '',
24
                'memory' => true,
25
                'fail' => $failMaster,
26
            ],
27
            'slaves' => [],
28
        ];
29
30
        for ($i = 0; $i < $slaves; $i++) {
31
            $params['slaves'][] = [
32
                'driver' => 'pdo_sqlite',
33
                'user' => 'root',
34
                'password' => '',
35
                'memory' => true,
36
                'fail' => false,
37
            ];
38
        }
39
40
        if ($failSlaves > 0) {
41
            $slaves = array_rand($params['slaves'], $failSlaves);
42
43
            if (! is_array($slaves)) {
44
                $slaves = [$slaves];
45
            }
46
47
            foreach ($slaves as $slaveIndex) {
48
                $params['slaves'][$slaveIndex]['fail'] = true;
49
            }
50
        }
51
52
        return $params;
53
    }
54
55
    protected function failingDriverMock()
56
    {
57
        $driverMock = $this->createMock(Driver::class);
58
59
        $driverMock->expects($this->any())
60
            ->method('connect')
61
            ->will($this->returnCallback(function (array $params) {
62
                if (isset($params['fail']) && $params['fail']) {
63
                    throw $this->createMock(ConnectionException::class);
64
                }
65
66
                return $this->createMock(DriverConnection::class);
67
            }));
68
69
        return $driverMock;
70
    }
71
}
72