Test Failed
Pull Request — master (#1)
by Darío
07:17
created

EntityAdapterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
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\TableGateway\EntityAdapter;
15
use Drone\Db\TableGateway\TableGateway;
16
use PHPUnit\Framework\TestCase;
17
use Symfony\Component\Dotenv\Dotenv;
18
19
class EntityAdapterTest extends TestCase
20
{
21
    /**
22
     * Database parameters
23
     */
24
    private $options = [
25
        "dbhost"       => "localhost",
26
        "dbuser"       => "root",
27
        "dbpass"       => "",
28
        "dbname"       => "test",
29
        "dbchar"       => "utf8",
30
        "dbport"       => "3306",
31
        "auto_connect" => false,
32
        "driver"       => 'Mysqli',  # needed for the DriverFactory
33
    ];
34
35
    public function setUp()
36
    {
37
        parent::setUp();
38
39
        $dotenv = new Dotenv();
40
        $dotenv->load(__DIR__.'/../../../.env.testing');
41
42
        $this->options['dbhost'] = $_ENV['DB_HOST'];
43
        $this->options['dbuser'] = $_ENV['DB_USER'];
44
        $this->options['dbpass'] = $_ENV['DB_PASS'];
45
        $this->options['dbname'] = $_ENV['DB_NAME'];
46
    }
47
48
    /*
49
    |--------------------------------------------------------------------------
50
    | Establishing connections
51
    |--------------------------------------------------------------------------
52
    |
53
    | The following tests are related to the connection mechanisms and its
54
    | exceptions and returned values.
55
    |
56
    */
57
58
    /**
59
     * Tests if we can connect to the database server through a EntityAdapter
60
     *
61
     * @return null
62
     */
63
    public function testCompositionWithTableGateway()
64
    {
65
        $entity = new User();
66
        $gateway = new TableGateway($entity, ["eadapter" => $this->options]);
67
        $entityAdapter = new EntityAdapter($gateway);
68
69
        $mysqliObject = $entityAdapter->getTableGateway()->getDb()->connect();
70
71
        $this->assertInstanceOf('\mysqli', $mysqliObject);
72
        $this->assertTrue($entityAdapter->getTableGateway()->getDb()->isConnected());
73
    }
74
75
    /**
76
     * Tests if we can execute DDL statements through the TableGateway accesor
77
     *
78
     * @return null
79
     */
80
    public function testCanExecuteStatementThroughTableGateway()
81
    {
82
        $options = $this->options;
83
        $options["auto_connect"] = true;
84
85
        $entity = new User();
86
        $gateway = new UserGateway($entity, "eadapter");
87
        $entityAdapter = new EntityAdapter($gateway);
88
89
        $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

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