Passed
Push — master ( 84ef81...00604e )
by Darío
01:38
created

User::__construct()   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 1
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\EntityAdapter;
16
use Drone\Db\TableGateway\TableGateway;
17
use Drone\Db\TableGateway\AbstractTableGateway;
18
use Drone\Db\Driver\Exception\ConnectionException;
19
use Drone\Db\Driver\Exception\InvalidQueryException;
20
use PHPUnit\Framework\TestCase;
21
22
class EntityAdapterTest extends TestCase
23
{
24
    /**
25
     * Database parameters
26
     */
27
    private $options = [
28
        "dbhost"       => "localhost",
29
        "dbuser"       => "root",
30
        "dbpass"       => "",
31
        "dbname"       => "test",
32
        "dbchar"       => "utf8",
33
        "dbport"       => "3306",
34
        "auto_connect" => false,
35
        "driver"       => 'Mysqli'  # needed for the DriverFactory
36
    ];
37
38
    /*
39
    |--------------------------------------------------------------------------
40
    | Stablishing connections
41
    |--------------------------------------------------------------------------
42
    |
43
    | The following tests are related to the connection mechanisms and its
44
    | exceptions and returned values.
45
    |
46
    */
47
48
    /**
49
     * Tests if we can connect to the database server through a EntityAdapter
50
     *
51
     * @return null
52
     */
53
    public function testCompositionWithTableGateway()
54
    {
55
        $entity = new User();
56
        $gateway = new TableGateway($entity, ["eadapter" => $this->options]);
57
        $entityAdapter = new EntityAdapter($gateway);
58
59
        $mysqliObject = $entityAdapter->getTableGateway()->getDb()->connect();
60
61
        $this->assertInstanceOf('\mysqli', $mysqliObject);
62
        $this->assertTrue($entityAdapter->getTableGateway()->getDb()->isConnected());
63
    }
64
65
    /**
66
     * Tests if we can execute DDL statements through the TableGateway accesor
67
     *
68
     * @return null
69
     */
70
    public function testCanExecuteStatementThroughTableGateway()
71
    {
72
        $options = $this->options;
73
        $options["auto_connect"] = true;
74
75
        $entity = new User();
76
        $gateway = new UserGateway($entity, "eadapter");
77
        $entityAdapter = new EntityAdapter($gateway);
78
79
        $result = $entityAdapter->getTableGateway()->create();
0 ignored issues
show
introduced by
The method create() does not exist on Drone\Db\TableGateway\TableGateway. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $result = $entityAdapter->getTableGateway()->/** @scrutinizer ignore-call */ create();
Loading history...
80
81
        # mysqli
82
        $this->assertTrue(is_object($result));
83
84
        # properties modified by execute() method
85
        $this->assertEquals(0, $gateway->getDb()->getNumRows());
86
        $this->assertEquals(0, $gateway->getDb()->getNumFields());
87
        $this->assertEquals(0, $gateway->getDb()->getRowsAffected());
88
    }
89
90
    /*
91
    |--------------------------------------------------------------------------
92
    | ORM TOOL (Data mapper)
93
    |--------------------------------------------------------------------------
94
    |
95
    | The following tests are related to the object relational mapping.
96
    |
97
    */
98
99
    /**
100
     * Tests if we can execute INSERT statements through the EntityAdapter
101
     *
102
     * @return null
103
     */
104
    public function testINSERTING()
105
    {
106
        $options = $this->options;
107
        $options["auto_connect"] = true;
108
109
        $entity = new User();
110
        $gateway = new TableGateway($entity, "eadapter");
111
        $entityAdapter = new EntityAdapter($gateway);
112
113
        $firstEntity = new User();
114
        $firstEntity->exchangeArray([
115
            "ID" => 1, "USERNAME" => "Dennis.Ritchie"
116
        ]);
117
118
        $secondEntity = new User();
119
        $secondEntity->exchangeArray([
120
            "ID" => 2, "USERNAME" => "Bjarne.Stroustrup"
121
        ]);
122
123
        $result = $entityAdapter->insert($firstEntity);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
124
        $result = $entityAdapter->insert($secondEntity);
125
126
        $this->assertTrue(is_object($result));
127
128
        # properties modified by execute() method
129
        $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumRows());
130
        $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumFields());
131
132
        # here 1 is the latest affected row, could be 2 if auto_commit were false
133
        $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getRowsAffected());
134
    }
135
136
    /**
137
     * Tests if we can execute UPDATE statements through the EntityAdapter
138
     *
139
     * @return null
140
     */
141
    public function testUPDATING()
142
    {
143
        $options = $this->options;
144
        $options["auto_connect"] = true;
145
146
        $entity = new User();
147
        $gateway = new TableGateway($entity, "eadapter");
148
        $entityAdapter = new EntityAdapter($gateway);
149
150
        $firstEntity = new User(["ID" => 1]);
151
152
        # tell to entity what changed
153
        $firstEntity->exchangeArray([
154
            "USERNAME" => "Rasmus.Lerdorf"
155
        ]);
156
157
        $result = $entityAdapter->update($firstEntity, ["ID" => 1]);
158
159
        $this->assertTrue(is_object($result));
160
161
        # properties modified by execute() method
162
        $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumRows());
163
        $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumFields());
164
        $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getRowsAffected());
165
    }
166
167
168
    /**
169
     * Tests if we can execute DELETE statements through the EntityAdapter
170
     *
171
     * @return null
172
     */
173
    public function testDELETING()
174
    {
175
        $options = $this->options;
176
        $options["auto_connect"] = true;
177
178
        $entity = new User();
179
        $gateway = new TableGateway($entity, "eadapter");
180
        $entityAdapter = new EntityAdapter($gateway);
181
182
        # same behaviour to tablegateway
183
        $result = $entityAdapter->delete(["ID" => 2]);
184
185
        $this->assertTrue(is_object($result));
186
187
        # properties modified by execute() method
188
        $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumRows());
189
        $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumFields());
190
        $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getRowsAffected());
191
    }
192
193
    /**
194
     * Tests if we can execute SELECT statements through the EntityAdapter
195
     *
196
     * @return null
197
     */
198
    public function testSELECTING()
199
    {
200
        $options = $this->options;
201
        $options["auto_connect"] = true;
202
203
        $entity = new User();
204
        $gateway = new TableGateway($entity, "eadapter");
205
        $entityAdapter = new EntityAdapter($gateway);
206
207
        $rows = $entityAdapter->select([
208
            "ID" => 1
209
        ]);
210
211
        $this->assertTrue(is_array($rows));
212
213
        # get only the first
214
        $user = array_shift($rows);
215
216
        $this->assertInstanceOf('DroneTest\Util\User', $user);
217
218
        $this->assertObjectHasAttribute("ID", $user);
219
        $this->assertObjectHasAttribute("USERNAME", $user);
220
221
        # properties modified by execute() method
222
        $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getNumRows());
223
        $this->assertEquals(2, $entityAdapter->getTableGateway()->getDb()->getNumFields());
224
        $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getRowsAffected());
225
        $this->endTests();
226
    }
227
228
    /**
229
     * Function to leave all in order, you can execute tests again without problems.
230
     *
231
     * @return null
232
     */
233
    private function endTests()
234
    {
235
        $options = $this->options;
236
        $options["auto_connect"] = true;
237
238
        $entity = new User();
239
        $gateway = new UserGateway($entity, "eadapter");
240
241
        # remove all work
242
        $gateway->drop();
243
    }
244
}
245
246
class User extends Entity
247
{
248
    public $ID;
249
    public $USERNAME;
250
251
    public function __construct($data = [])
252
    {
253
        parent::__construct($data);
254
        $this->setTableName("USER");
255
   }
256
}
257
258
class UserGateway extends TableGateway
259
{
260
    public function create()
261
    {
262
        $sql = "CREATE TABLE USER (ID INTEGER(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, USERNAME VARCHAR(30))";
263
        return $this->getDb()->execute($sql);
264
    }
265
266
    public function drop()
267
    {
268
        $sql = "DROP TABLE USER";
269
        return $this->getDb()->execute($sql);
270
    }
271
}