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

RandomConnectorTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 37
c 1
b 0
f 0
dl 0
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testConnectRandomFail() 0 12 3
A testConnectFailSlave() 0 20 3
A testConnect() 0 7 1
A testConnectFailMaster() 0 20 3
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Connection\Connector;
4
5
use Doctrine\DBAL\Connections\Connector\RandomConnector;
6
use Throwable;
7
8
class RandomConnectorTest extends ConnectorTest
9
{
10
    public function testConnect() : void
11
    {
12
        $params = $this->getParams();
13
14
        $connector = new RandomConnector($params, $this->failingDriverMock());
15
        $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('master'));
16
        $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('slave'));
17
    }
18
19
    public function testConnectFailMaster() : void
20
    {
21
        $params = $this->getParams(2, 0, true);
22
23
        $connector = new RandomConnector($params, $this->failingDriverMock());
24
        $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('slave'));
25
26
        $failed = false;
27
        try {
28
            $connector->connectTo('master');
29
            $failed = true;
30
        } catch (Throwable $e) {
31
            $this->assertInstanceOf('Doctrine\DBAL\Exception\ConnectionException', $e);
32
        }
33
34
        if (! $failed) {
35
            return;
36
        }
37
38
        $this->fail('Connect has not failed');
39
    }
40
41
    public function testConnectFailSlave() : void
42
    {
43
        $params = $this->getParams(2, 2);
44
45
        $connector = new RandomConnector($params, $this->failingDriverMock());
46
        $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('master'));
47
48
        $failed = false;
49
        try {
50
            $connector->connectTo('slave');
51
            $failed = true;
52
        } catch (Throwable $e) {
53
            $this->assertInstanceOf('Doctrine\DBAL\Exception\ConnectionException', $e);
54
        }
55
56
        if (! $failed) {
57
            return;
58
        }
59
60
        $this->fail('Connect has not failed');
61
    }
62
63
    public function testConnectRandomFail() : void
64
    {
65
        for ($i = 0; $i < 100; $i++) {
66
            $params = $this->getParams(2, 1);
67
68
            $connector = new RandomConnector($params, $this->failingDriverMock());
69
            $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('master'));
70
71
            try {
72
                $connector->connectTo('slave');
73
            } catch (Throwable $e) {
74
                $this->assertInstanceOf('Doctrine\DBAL\Exception\ConnectionException', $e);
75
            }
76
        }
77
    }
78
}
79