Passed
Push — master ( ad15ea...7afef2 )
by Darío
02:13
created

MySQLTest::testCommitBehavior()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 31
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace DroneTest\Util;
12
13
use Drone\Db\Driver\MySQL;
14
use Drone\Db\Driver\Exception\ConnectionException;
15
use PHPUnit\Framework\TestCase;
16
17
class MySQLTest extends TestCase
18
{
19
    /**
20
     * Database parameters
21
     */
22
    private $options = [
23
        "dbhost"       => "localhost",
24
        "dbuser"       => "root",
25
        "dbpass"       => "",
26
        "dbname"       => "test",
27
        "dbchar"       => "utf8",
28
        "dbport"       => "3306",
29
        "auto_connect" => false
30
    ];
31
32
    /*
33
    |--------------------------------------------------------------------------
34
    | Stablishing connections
35
    |--------------------------------------------------------------------------
36
    |
37
    | The following tests are related to the connection methods and its
38
    | exceptions and returned values.
39
    |
40
    */
41
42
    /**
43
     * Tests if we can connect to the database server
44
     *
45
     * @return null
46
     */
47
    public function testCanStablishConnection()
48
    {
49
        $conn = new MySQL($this->options);
50
51
        $mysqliObject = $conn->connect();
52
53
        $this->assertInstanceOf('\mysqli', $mysqliObject);
54
        $this->assertTrue($conn->isConnected());
55
    }
56
57
    /**
58
     * Tests if we can disconnect from the database server
59
     *
60
     * @return null
61
     */
62
    public function testCanDownConnection()
63
    {
64
        $conn = new MySQL($this->options);
65
66
        $conn->connect();
67
        $result = $conn->disconnect();
68
69
        $this->assertNotTrue($conn->isConnected());
70
        $this->assertTrue($result);
71
    }
72
73
    /**
74
     * Tests if we can disconnect from server when there is not a connection stablished
75
     *
76
     * @return null
77
     */
78
    public function testCannotDisconectWhenNotConnected()
79
    {
80
        $conn = new MySQL($this->options);
81
82
        $errorObject = null;
83
84
        try {
85
            $conn->disconnect();
86
        }
87
        catch (\Exception $e)
88
        {
89
            $errorObject = ($e instanceof \LogicException);
90
        }
91
        finally
92
        {
93
            $this->assertNotTrue($conn->isConnected());
94
            $this->assertTrue($errorObject, $e->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
95
        }
96
    }
97
98
    /**
99
     * Tests if we can reconnect to the database server
100
     *
101
     * @return null
102
     */
103
    public function testCanStablishConnectionAgain()
104
    {
105
        $conn = new MySQL($this->options);
106
107
        $conn->connect();
108
        $mysqliObject = $conn->reconnect();
109
110
        $this->assertInstanceOf('\mysqli', $mysqliObject);
111
        $this->assertTrue($conn->isConnected());
112
    }
113
114
    /**
115
     * Tests if we can reconnect to the database server when there is not a connection stablished
116
     *
117
     * @return null
118
     */
119
    public function testCannotStablishReconnection()
120
    {
121
        $conn = new MySQL($this->options);
122
123
        $errorObject = null;
124
125
        try {
126
            $conn->reconnect();
127
        }
128
        catch (\Exception $e)
129
        {
130
            $errorObject = ($e instanceof \LogicException);
131
        }
132
        finally
133
        {
134
            $this->assertTrue($errorObject, $e->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
135
            $this->assertNotTrue($conn->isConnected());
136
        }
137
    }
138
139
    /**
140
     * Tests if a failed connection throws a ConnectionException
141
     *
142
     * @return null
143
     */
144
    public function testCannotStablishConnection()
145
    {
146
        $options = $this->options;
147
        $options["dbhost"] = 'myserver';   // this server does not exists
148
149
        $conn = new MySQL($options);
150
151
        $mysqliObject = $errorObject = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $mysqliObject is dead and can be removed.
Loading history...
152
153
        $message = "No exception";
154
155
        try {
156
            $mysqliObject = $conn->connect();
157
        }
158
        catch (\Exception $e)
159
        {
160
            $errorObject = ($e instanceof ConnectionException);
161
            $message = $e->getMessage();
162
        }
163
        finally
164
        {
165
            $this->assertTrue($errorObject, $message);
166
            $this->assertNotTrue($conn->isConnected());
167
        }
168
    }
169
170
    /*
171
    |--------------------------------------------------------------------------
172
    | Quering and Transactions
173
    |--------------------------------------------------------------------------
174
    |
175
    | The following tests are related to query and transaction operations and its
176
    | exceptions and returned values.
177
    |
178
    */
179
180
    /**
181
     * Tests if we can execute DDL statements
182
     *
183
     * @return null
184
     */
185
    public function testCanExecuteDLLStatement()
186
    {
187
        $options = $this->options;
188
        $options["auto_connect"] = true;
189
190
        $conn = new MySQL($options);
191
        $sql = "CREATE TABLE MYTABLE (ID INTEGER(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, DESCRIPTION VARCHAR(100))";
192
        $result = $conn->execute($sql);
193
194
        $this->assertTrue(is_object($result));
195
196
        # properties modified by execute() method
197
        $this->assertEquals(0, $conn->getNumRows());
198
        $this->assertEquals(0, $conn->getNumFields());
199
        $this->assertEquals(0, $conn->getRowsAffected());
200
    }
201
202
    /**
203
     * Tests if we can execute DML statements
204
     *
205
     * @return null
206
     */
207
    public function testCanExecuteDMLStatement()
208
    {
209
        $options = $this->options;
210
        $options["auto_connect"] = true;
211
212
        $conn = new MySQL($options);
213
        $sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('Hello world!')";
214
        $result = $conn->execute($sql);
215
216
        $this->assertTrue(is_object($result));
217
218
        # properties modified by execute() method
219
        $this->assertEquals(0, $conn->getNumRows());
220
        $this->assertEquals(0, $conn->getNumFields());
221
        $this->assertEquals(1, $conn->getRowsAffected());
222
    }
223
224
    /**
225
     * Tests getting results
226
     *
227
     * @return null
228
     */
229
    public function testGettingResults()
230
    {
231
        $options = $this->options;
232
        $options["auto_connect"] = true;
233
234
        $conn = new MySQL($options);
235
        $sql = "SELECT * FROM MYTABLE LIMIT 2";
236
        $result = $conn->execute($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
237
238
        # properties modified by execute() method
239
        $this->assertEquals(1, $conn->getNumRows());
240
        $this->assertEquals(2, $conn->getNumFields());
241
        $this->assertEquals(0, $conn->getRowsAffected());
242
243
        $rowset = $conn->getArrayResult();    # array with results
244
        $row = array_shift($rowset);
245
246
        $this->assertArrayHasKey("ID", $row);
247
        $this->assertArrayHasKey("DESCRIPTION", $row);
248
    }
249
250
    /**
251
     * Tests if we can commit transactions
252
     *
253
     * @return null
254
     */
255
    public function testCommitBehavior()
256
    {
257
        $options = $this->options;
258
        $options["auto_connect"] = true;
259
260
        $conn = new MySQL($options);
261
        $conn->autocommit(false);
262
263
        $sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_1')";
264
        $result = $conn->execute($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
265
266
        $sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'COMMIT_ROW_1'";
267
        $result = $conn->execute($sql);
268
        $rowset = $conn->getArrayResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $rowset is dead and can be removed.
Loading history...
269
270
        $rowcount = count($row);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $row seems to be never defined.
Loading history...
271
272
        $this->assertTrue(($rowcount === 0));
273
274
        # properties modified by execute() method
275
        $this->assertEquals(0, $conn->getNumRows());
276
        $this->assertEquals(0, $conn->getNumFields());
277
        $this->assertEquals(0, $conn->getRowsAffected());
278
279
        $conn->commit();
280
        $this->assertTrue(($rowcount === 0));
281
282
        # properties modified by execute() method
283
        $this->assertEquals(0, $conn->getNumRows());
284
        $this->assertEquals(0, $conn->getNumFields());
285
        $this->assertEquals(1, $conn->getRowsAffected());
286
    }
287
}