Completed
Push — master ( 6fd059...47e338 )
by Darío
04:29
created

MySQLTest::textCanExecuteDMLStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 10
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;
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
        $options = [
81
            "dbhost"       => "localhost",
82
            "dbuser"       => "root",
83
            "dbpass"       => "",
84
            "dbname"       => "test",
85
            "dbchar"       => "utf8",
86
            "dbport"       => "3306",
87
            "auto_connect" => false
88
        ];
89
90
        $conn = new MySQL($options);
91
92
        $errorObject = null;
93
94
        try {
95
            $conn->disconnect();
96
        }
97
        catch (\Exception $e)
98
        {
99
            $errorObject = ($e instanceof \LogicException);
100
        }
101
        finally
102
        {
103
            $this->assertNotTrue($conn->isConnected());
104
            $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...
105
        }
106
    }
107
108
    /**
109
     * Tests if we can reconnect to the database server
110
     *
111
     * @return null
112
     */
113
    public function testCanStablishConnectionAgain()
114
    {
115
        $conn = new MySQL($this->options);
116
117
        $conn->connect();
118
        $mysqliObject = $conn->reconnect();
119
120
        $this->assertInstanceOf(\mysqli, $mysqliObject);
0 ignored issues
show
Bug introduced by
The constant mysqli was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
        $this->assertTrue($conn->isConnected());
122
    }
123
124
    /**
125
     * Tests if we can reconnect to the database server when there is not a connection stablished
126
     *
127
     * @return null
128
     */
129
    public function testCannotStablishConnectionAgain()
130
    {
131
        $conn = new MySQL($this->options);
132
133
        $errorObject = null;
134
135
        try {
136
            $conn->reconnect();
137
        }
138
        catch (\Exception $e)
139
        {
140
            $errorObject = ($e instanceof \LogicException);
141
        }
142
        finally
143
        {
144
            $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...
145
            $this->assertNotTrue($conn->isConnected());
146
        }
147
    }
148
149
    /**
150
     * Tests if a connection failed throws a ConnectionException
151
     *
152
     * @return null
153
     */
154
    public function testCannotStablishConnection()
155
    {
156
        $this->options["dbhost"] = "myserver";   // this server does not exists
157
158
        $conn = new MySQL($this->options);
159
160
        $mysqliObject = $errorObject = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $mysqliObject is dead and can be removed.
Loading history...
161
162
        try {
163
            $mysqliObject = $conn->connect();
164
        }
165
        catch (\Exception $e)
166
        {
167
            $errorObject = ($e instanceof ConnectionException);
0 ignored issues
show
Bug introduced by
The type DroneTest\Util\ConnectionException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
168
        }
169
        finally
170
        {
171
            $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...
172
            $this->assertNotTrue($conn->isConnected());
173
        }
174
    }
175
176
    /*
177
    |--------------------------------------------------------------------------
178
    | Quering and Transactions
179
    |--------------------------------------------------------------------------
180
    |
181
    | The following tests are related to query and transaction operations and its
182
    | exceptions and returned values.
183
    |
184
    */
185
186
    /**
187
     * Tests if we can execute DDL statements
188
     *
189
     * @return null
190
     */
191
    public function textCanExecuteDLLStatement()
192
    {
193
        $this->options["auto_connect"] = true;
194
195
        $conn = new MySQL($this->options);
196
        $sql = "CREATE TABLE MYTABLE (ID INTEGER AUTO_INCREMENT, DESCRIPTION VARCHAR(100))";
197
        $result = $conn->execute($sql);
198
199
        $this->assertTrue(is_resource($result));
200
201
        # properties modified by execute() method
202
        $this->assertEquals(0, $conn->getNumRows());
203
        $this->assertEquals(0, $conn->getNumFields());
204
        $this->assertEquals(0, $conn->getRowsAffected());
205
    }
206
207
    /**
208
     * Tests if we can execute DML statements
209
     *
210
     * @return null
211
     */
212
    public function textCanExecuteDMLStatement()
213
    {
214
        $this->options["auto_connect"] = true;
215
216
        $conn = new MySQL($this->options);
217
        $sql = "INSERT INTO MYTABLE VALUES (1, 'Hello world!')";
218
        $result = $conn->execute($sql);
219
220
        $this->assertTrue(is_resource($result));
221
222
        # properties modified by execute() method
223
        $this->assertEquals(0, $conn->getNumRows());
224
        $this->assertEquals(0, $conn->getNumFields());
225
        $this->assertEquals(1, $conn->getRowsAffected());
226
    }
227
}