Failed Conditions
Push — master ( 631156...8c1e51 )
by Luís
16s
created

createMasterSlaveConnectionParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use Doctrine\DBAL\Connections\MasterSlaveConnection;
6
use Doctrine\DBAL\DriverManager;
7
use Doctrine\Tests\DbalFunctionalTestCase;
8
9
/**
10
 * @group DBAL-20
11
 */
12
class MasterSlaveConnectionTest extends DbalFunctionalTestCase
13
{
14
    protected function setUp()
15
    {
16
        parent::setUp();
17
18
        $platformName = $this->_conn->getDatabasePlatform()->getName();
19
20
        // This is a MySQL specific test, skip other vendors.
21
        if ($platformName != 'mysql') {
22
            $this->markTestSkipped(sprintf('Test does not work on %s.', $platformName));
23
        }
24
25
        try {
26
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
27
            $table = new \Doctrine\DBAL\Schema\Table("master_slave_table");
28
            $table->addColumn('test_int', 'integer');
29
            $table->setPrimaryKey(array('test_int'));
30
31
            $sm = $this->_conn->getSchemaManager();
32
            $sm->createTable($table);
33
34
35
        } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
36
        }
37
38
        $this->_conn->executeUpdate('DELETE FROM master_slave_table');
39
        $this->_conn->insert('master_slave_table', array('test_int' => 1));
40
    }
41
42
    private function createMasterSlaveConnection(bool $keepSlave = false) : MasterSlaveConnection
43
    {
44
        return DriverManager::getConnection($this->createMasterSlaveConnectionParams($keepSlave));
0 ignored issues
show
Bug Best Practice introduced by
The expression return Doctrine\DBAL\Dri...tionParams($keepSlave)) returns the type Doctrine\DBAL\Connection which includes types incompatible with the type-hinted return Doctrine\DBAL\Connections\MasterSlaveConnection.
Loading history...
45
    }
46
47
    private function createMasterSlaveConnectionParams(bool $keepSlave = false) : array
48
    {
49
        $params = $this->_conn->getParams();
50
        $params['master']       = $params;
51
        $params['slaves']       = array($params, $params);
52
        $params['keepSlave']    = $keepSlave;
53
        $params['wrapperClass'] = MasterSlaveConnection::class;
54
55
        return $params;
56
    }
57
58
    public function testInheritCharsetFromMaster() : void
59
    {
60
        $charsets = [
61
            'utf8',
62
            'latin1'
63
        ];
64
65
        foreach ($charsets as $charset) {
66
            $params = $this->createMasterSlaveConnectionParams();
67
            $params['master']['charset'] = $charset;
68
69
            foreach ($params['slaves'] as $index => $slaveParams) {
70
                if (isset($slaveParams['charset'])) {
71
                    unset($params['slaves'][$index]['charset']);
72
                }
73
            }
74
75
            /** @var MasterSlaveConnection $conn */
76
            $conn = DriverManager::getConnection($params);
77
            $conn->connect('slave');
78
79
            self::assertFalse($conn->isConnectedToMaster());
80
81
            $clientCharset = $conn->fetchColumn('select @@character_set_client as c');
82
83
            self::assertSame(
84
                $charset,
85
                substr(strtolower($clientCharset), 0, strlen($charset))
86
            );
87
        }
88
    }
89
90 View Code Duplication
    public function testMasterOnConnect()
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...
91
    {
92
        $conn = $this->createMasterSlaveConnection();
93
94
        self::assertFalse($conn->isConnectedToMaster());
95
        $conn->connect('slave');
96
        self::assertFalse($conn->isConnectedToMaster());
97
        $conn->connect('master');
98
        self::assertTrue($conn->isConnectedToMaster());
99
    }
100
101
    public function testNoMasterOnExecuteQuery()
102
    {
103
        $conn = $this->createMasterSlaveConnection();
104
105
        $sql = "SELECT count(*) as num FROM master_slave_table";
106
        $data = $conn->fetchAll($sql);
107
        $data[0] = array_change_key_case($data[0], CASE_LOWER);
108
109
        self::assertEquals(1, $data[0]['num']);
110
        self::assertFalse($conn->isConnectedToMaster());
111
    }
112
113
    public function testMasterOnWriteOperation()
114
    {
115
        $conn = $this->createMasterSlaveConnection();
116
        $conn->insert('master_slave_table', array('test_int' => 30));
117
118
        self::assertTrue($conn->isConnectedToMaster());
119
120
        $sql = "SELECT count(*) as num FROM master_slave_table";
121
        $data = $conn->fetchAll($sql);
122
        $data[0] = array_change_key_case($data[0], CASE_LOWER);
123
124
        self::assertEquals(2, $data[0]['num']);
125
        self::assertTrue($conn->isConnectedToMaster());
126
    }
127
128
    /**
129
     * @group DBAL-335
130
     */
131 View Code Duplication
    public function testKeepSlaveBeginTransactionStaysOnMaster()
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...
132
    {
133
        $conn = $this->createMasterSlaveConnection($keepSlave = true);
134
        $conn->connect('slave');
135
136
        $conn->beginTransaction();
137
        $conn->insert('master_slave_table', array('test_int' => 30));
138
        $conn->commit();
139
140
        self::assertTrue($conn->isConnectedToMaster());
141
142
        $conn->connect();
143
        self::assertTrue($conn->isConnectedToMaster());
144
145
        $conn->connect('slave');
146
        self::assertFalse($conn->isConnectedToMaster());
147
    }
148
149
    /**
150
     * @group DBAL-335
151
     */
152 View Code Duplication
    public function testKeepSlaveInsertStaysOnMaster()
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...
153
    {
154
        $conn = $this->createMasterSlaveConnection($keepSlave = true);
155
        $conn->connect('slave');
156
157
        $conn->insert('master_slave_table', array('test_int' => 30));
158
159
        self::assertTrue($conn->isConnectedToMaster());
160
161
        $conn->connect();
162
        self::assertTrue($conn->isConnectedToMaster());
163
164
        $conn->connect('slave');
165
        self::assertFalse($conn->isConnectedToMaster());
166
    }
167
168 View Code Duplication
    public function testMasterSlaveConnectionCloseAndReconnect()
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...
169
    {
170
        $conn = $this->createMasterSlaveConnection();
171
        $conn->connect('master');
172
        self::assertTrue($conn->isConnectedToMaster());
173
174
        $conn->close();
175
        self::assertFalse($conn->isConnectedToMaster());
176
177
        $conn->connect('master');
178
        self::assertTrue($conn->isConnectedToMaster());
179
    }
180
}
181