Passed
Push — master ( c78efc...84ef81 )
by Darío
01:42
created

MyEntityGateway::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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\Entity;
14
use Drone\Db\Driver\MySQL;
15
use Drone\Db\TableGateway\TableGateway;
16
use Drone\Db\TableGateway\AbstractTableGateway;
17
use Drone\Db\Driver\Exception\ConnectionException;
18
use Drone\Db\Driver\Exception\InvalidQueryException;
19
use PHPUnit\Framework\TestCase;
20
21
class TableGatewayTest extends TestCase
22
{
23
    /**
24
     * Database parameters
25
     */
26
    private $options = [
27
        "dbhost"       => "localhost",
28
        "dbuser"       => "root",
29
        "dbpass"       => "",
30
        "dbname"       => "test",
31
        "dbchar"       => "utf8",
32
        "dbport"       => "3306",
33
        "auto_connect" => false,
34
        "driver"       => 'Mysqli'  # needed for the DriverFactory
35
    ];
36
37
    /*
38
    |--------------------------------------------------------------------------
39
    | Stablishing connections
40
    |--------------------------------------------------------------------------
41
    |
42
    | The following tests are related to the connection mechanisms and its
43
    | exceptions and returned values.
44
    |
45
    */
46
47
    /**
48
     * Tests if we can connect to the database server through a TableGateway
49
     *
50
     * @return null
51
     */
52
    public function testCanStablishConnection()
53
    {
54
        $entity = new MyEntity();
55
        $gateway = new TableGateway($entity, ["default" => $this->options]);
56
57
        $mysqliObject = $gateway->getDb()->connect();
58
59
        $this->assertInstanceOf('\mysqli', $mysqliObject);
60
        $this->assertTrue($gateway->getDb()->isConnected());
61
    }
62
63
    /**
64
     * Tests if a failed connection throws a RuntimeException when connection exists
65
     *
66
     * @return null
67
     */
68
    public function testCannotStablishConnectionWhenExists()
69
    {
70
        $options = $this->options;
71
        $options["dbhost"] = 'myserver';   // this server does not exists
72
73
        $errorObject = null;
74
75
        $message = "No exception";
76
77
        try
78
        {
79
            $entity = new MyEntity();
80
            $gateway = new TableGateway($entity, ["default" => $options]);
0 ignored issues
show
Unused Code introduced by
The assignment to $gateway is dead and can be removed.
Loading history...
81
        }
82
        catch (\Exception $e)
83
        {
84
            $errorObject = ($e instanceof \RuntimeException);
85
            $message = $e->getMessage();
86
        }
87
        finally
88
        {
89
            $this->assertTrue($errorObject, $message);
90
        }
91
    }
92
93
    /**
94
     * Tests if a failed connection throws a ConnectionException
95
     *
96
     * @return null
97
     */
98
    public function testCannotStablishConnection()
99
    {
100
        $options = $this->options;
101
        $options["dbhost"] = 'myserver';   // this server does not exists
102
103
        $entity = new MyEntity();
104
        $gateway = new TableGateway($entity, ["other" => $options]);
105
106
        $errorObject = null;
107
108
        $message = "No exception";
109
110
        try
111
        {
112
            $gateway->getDb()->connect();
113
        }
114
        catch (\Exception $e)
115
        {
116
            $errorObject = ($e instanceof ConnectionException);
117
            $message = $e->getMessage();
118
        }
119
        finally
120
        {
121
            $this->assertTrue($errorObject, $message);
122
            $this->assertNotTrue($gateway->getDb()->isConnected());
123
        }
124
    }
125
126
    /**
127
     * Tests if we get created and not created connections
128
     *
129
     * @return null
130
     */
131
    public function testGettingConnections()
132
    {
133
        $db = AbstractTableGateway::getDriver('default');
134
        $this->assertTrue(($db instanceof MySQL));
135
        $this->assertTrue($db->isConnected());
136
137
        $db = AbstractTableGateway::getDriver('other');
138
        $this->assertTrue(($db instanceof MySQL));
139
        $this->assertNotTrue($db->isConnected());
140
141
        $errorObject = null;
142
143
        $message = "No exception";
144
145
        try
146
        {
147
            AbstractTableGateway::getDriver('other3');
148
        }
149
        catch (\Exception $e)
150
        {
151
            $errorObject = ($e instanceof \RuntimeException);
152
            $message = $e->getMessage();
153
        }
154
        finally
155
        {
156
            $this->assertTrue($errorObject, $message);
157
        }
158
    }
159
160
    /**
161
     * Tests if we can create a table gateway with an existing connection
162
     *
163
     * @return null
164
     */
165
    public function testGatewayCreationWithExistingConnection()
166
    {
167
        $options = $this->options;
168
        $options["auto_connect"] = true;
169
170
        $entity = new MyEntity();
171
        $gateway = new TableGateway($entity, "default");
172
173
        $this->assertTrue($gateway->getDb()->isConnected());
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 testCanExecuteDLLStatement()
192
    {
193
        $options = $this->options;
194
        $options["auto_connect"] = true;
195
196
        $entity = new MyEntity();
197
        $gateway = new MyEntityGateway($entity, "default");
198
199
        $result = $gateway->create();
200
201
        # mysqli
202
        $this->assertTrue(is_object($result));
203
204
        # properties modified by execute() method
205
        $this->assertEquals(0, $gateway->getDb()->getNumRows());
206
        $this->assertEquals(0, $gateway->getDb()->getNumFields());
207
        $this->assertEquals(0, $gateway->getDb()->getRowsAffected());
208
    }
209
210
    /**
211
     * Tests if we can execute DML statements
212
     *
213
     * @return null
214
     */
215
    public function testCanExecuteDMLStatement()
216
    {
217
        $options = $this->options;
218
        $options["auto_connect"] = true;
219
220
        $entity = new MyEntity();
221
        $gateway = new MyEntityGateway($entity, "default");
222
223
        # mysqli
224
        $result = $gateway->customDML(); // insert statement
225
226
        $this->assertTrue(is_object($result));
227
228
        # properties modified by execute() method
229
        $this->assertEquals(0, $gateway->getDb()->getNumRows());
230
        $this->assertEquals(0, $gateway->getDb()->getNumFields());
231
        $this->assertEquals(1, $gateway->getDb()->getRowsAffected());
232
    }
233
234
    /**
235
     * Tests if a wrong query execution throws an InvalidQueryException
236
     *
237
     * @return null
238
     */
239
    public function testGettingInvalidQueryException()
240
    {
241
        $options = $this->options;
242
        $options["auto_connect"] = true;
243
244
        $entity = new MyEntity();
245
        $gateway = new MyEntityGateway($entity, "default");
246
247
        $errorObject = null;
248
        $message = "No exception";
249
250
        try
251
        {
252
            $gateway->wrongDML();
253
        }
254
        catch (\Exception $e)
255
        {
256
            $errorObject = ($e instanceof InvalidQueryException);
257
            $message = $e->getMessage();
258
        }
259
        finally
260
        {
261
            $this->assertTrue($errorObject, $message);
262
        }
263
    }
264
265
    /**
266
     * Tests getting results
267
     *
268
     * @return null
269
     */
270
    public function testGettingResults()
271
    {
272
        $options = $this->options;
273
        $options["auto_connect"] = true;
274
275
        $entity = new MyEntity();
276
        $gateway = new MyEntityGateway($entity, "default");
277
278
        $rows = $gateway->getResults();
279
280
        # get only the first
281
        $row = array_shift($rows);
282
283
        $this->assertArrayHasKey("ID", $row);
284
        $this->assertArrayHasKey("DESCRIPTION", $row);
285
286
        # properties modified by execute() method
287
        $this->assertEquals(1, $gateway->getDb()->getNumRows());
288
        $this->assertEquals(2, $gateway->getDb()->getNumFields());
289
        $this->assertEquals(0, $gateway->getDb()->getRowsAffected());
290
    }
291
292
    # AS YOU CAN SEE IN THE ABOVE TEST, IT'S EASY TO ACCESS TO THE DRIVER (HERE MYSQL) USING THE
293
    # getDb() METHOD. A COMPLETE TEST FOR A DRIVER IS AVAILABLE IN MySQLTes.php. ALL TRANSACTION
294
    # BEHAVIOR OF TABLE GATEWAY IS RELATED TO THE DRIVER. ONLY GET THE DRIVER AND LET'S DO IT.
295
296
    /*
297
    |--------------------------------------------------------------------------
298
    | TABLE GATEWAY
299
    |--------------------------------------------------------------------------
300
    |
301
    | The following tests are related to table gateway.
302
    |
303
    */
304
305
    /**
306
     * Tests if we can execute INSERT statement through the ORM tool
307
     *
308
     * @return null
309
     */
310
    public function testINSERTING()
311
    {
312
        $options = $this->options;
313
        $options["auto_connect"] = true;
314
315
        $entity = new MyEntity();
316
317
        # Here we can use the generic table gatway or ours
318
        $gateway = new TableGateway($entity, "default");
319
320
        $result = $gateway->insert(["ID" => 500, "DESCRIPTION" => "NEW ELEMENT ONE"]);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
321
        $result = $gateway->insert(["ID" => 501, "DESCRIPTION" => "NEW ELEMENT TWO"]);
322
323
        $this->assertTrue(is_object($result));
324
325
        # properties modified by execute() method
326
        $this->assertEquals(0, $gateway->getDb()->getNumRows());
327
        $this->assertEquals(0, $gateway->getDb()->getNumFields());
328
329
        # here 1 is the latest affected row, could be 2 if auto_commit were false
330
        $this->assertEquals(1, $gateway->getDb()->getRowsAffected());
331
    }
332
333
    /**
334
     * Tests if we can execute UPDATE statement through the ORM tool
335
     *
336
     * @return null
337
     */
338
    public function testUPDATING()
339
    {
340
        $options = $this->options;
341
        $options["auto_connect"] = true;
342
343
        $entity = new MyEntity();
344
345
        # Here we can use the generic table gatway or ours
346
        $gateway = new TableGateway($entity, "default");
347
348
        $result = $gateway->update(["DESCRIPTION" => "NEW ELEMENT MODIFIED"], ["ID" => 500]);
349
350
        $this->assertTrue(is_object($result));
351
352
        # properties modified by execute() method
353
        $this->assertEquals(0, $gateway->getDb()->getNumRows());
354
        $this->assertEquals(0, $gateway->getDb()->getNumFields());
355
        $this->assertEquals(1, $gateway->getDb()->getRowsAffected());
356
    }
357
358
    /**
359
     * Tests if we can execute DELETE statement through the ORM tool
360
     *
361
     * @return null
362
     */
363
    public function testDELETING()
364
    {
365
        $options = $this->options;
366
        $options["auto_connect"] = true;
367
368
        $entity = new MyEntity();
369
370
        # Here we can use the generic table gatway or ours
371
        $gateway = new TableGateway($entity, "default");
372
373
        $result = $gateway->delete(["ID" => 500]);
374
375
        $this->assertTrue(is_object($result));
376
377
        # properties modified by execute() method
378
        $this->assertEquals(0, $gateway->getDb()->getNumRows());
379
        $this->assertEquals(0, $gateway->getDb()->getNumFields());
380
        $this->assertEquals(1, $gateway->getDb()->getRowsAffected());
381
    }
382
383
    /**
384
     * Tests if we can execute SELECT statement through the ORM tool
385
     *
386
     * @return null
387
     */
388
    public function testSELECTING()
389
    {
390
        $options = $this->options;
391
        $options["auto_connect"] = true;
392
393
        $entity = new MyEntity();
394
395
        # Here we can use the generic table gatway or ours
396
        $gateway = new TableGateway($entity, "default");
397
398
        $rows = $gateway->select(["ID" => 501]);
399
400
        $this->assertTrue(is_array($rows));
401
402
        # get only the first
403
        $row = array_shift($rows);
404
405
        $this->assertArrayHasKey("ID", $row);
406
        $this->assertArrayHasKey("DESCRIPTION", $row);
407
408
        # properties modified by execute() method
409
        $this->assertEquals(1, $gateway->getDb()->getNumRows());
410
        $this->assertEquals(2, $gateway->getDb()->getNumFields());
411
        $this->assertEquals(0, $gateway->getDb()->getRowsAffected());
412
413
        $this->endTests();
414
    }
415
416
    /**
417
     * Function to leave all in order, you can execute tests again without problems.
418
     *
419
     * @return null
420
     */
421
    private function endTests()
422
    {
423
        $options = $this->options;
424
        $options["auto_connect"] = true;
425
426
        $entity = new MyEntity();
427
        $gateway = new MyEntityGateway($entity, "default");
428
429
        # remove all work
430
        $gateway->drop();
431
    }
432
}
433
434
class MyEntity extends Entity
435
{
436
    public $ID;
437
    public $DESCRIPTION;
438
439
    public function __construct($data = [])
440
    {
441
        parent::__construct($data);
442
        $this->setTableName("MYTABLE");
443
        //$this->setConnectionParams("local");
444
   }
445
}
446
447
class MyEntityGateway extends TableGateway
448
{
449
    public function create()
450
    {
451
        $sql = "CREATE TABLE MYTABLE (ID INTEGER(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, DESCRIPTION VARCHAR(100))";
452
        return $this->getDb()->execute($sql);
453
    }
454
455
    public function drop()
456
    {
457
        $sql = "DROP TABLE MYTABLE";
458
        return $this->getDb()->execute($sql);
459
    }
460
461
    public function customDML()
462
    {
463
        $sql = "INSERT INTO MYTABLE VALUES(1000, 'Some data')";
464
        return $this->getDb()->execute($sql);
465
    }
466
467
    public function wrongDML()
468
    {
469
        $sql = "INSERT INTO MYTABLE (DESCRIPTION, WRONG) VALUES ('Hello world!')";
470
        return $this->getDb()->execute($sql);
471
    }
472
473
    public function getResults()
474
    {
475
        $sql = "SELECT * FROM MYTABLE WHERE ID = 1000";
476
        $this->getDb()->execute($sql);
477
        return $this->getDb()->getArrayResult();
478
    }
479
}