Pleets /
dronephp
| 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 | "dbchar" => "utf8", |
||
| 26 | "dbport" => "3306", |
||
| 27 | "auto_connect" => false, |
||
| 28 | "driver" => 'Mysqli', # needed for the DriverFactory |
||
| 29 | ]; |
||
| 30 | |||
| 31 | public function setUp() |
||
| 32 | { |
||
| 33 | parent::setUp(); |
||
| 34 | |||
| 35 | $dotenv = new Dotenv(); |
||
| 36 | $dotenv->load(__DIR__.'/../../../.env.testing'); |
||
| 37 | |||
| 38 | $this->options['dbhost'] = $_ENV['DB_HOST']; |
||
| 39 | $this->options['dbuser'] = $_ENV['DB_USER']; |
||
| 40 | $this->options['dbpass'] = $_ENV['DB_PASS']; |
||
| 41 | $this->options['dbname'] = $_ENV['DB_NAME']; |
||
| 42 | } |
||
| 43 | |||
| 44 | /* |
||
| 45 | |-------------------------------------------------------------------------- |
||
| 46 | | Establishing connections |
||
| 47 | |-------------------------------------------------------------------------- |
||
| 48 | | |
||
| 49 | | The following tests are related to the connection mechanisms and its |
||
| 50 | | exceptions and returned values. |
||
| 51 | | |
||
| 52 | */ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Tests if we can connect to the database server through a EntityAdapter |
||
| 56 | * |
||
| 57 | * @return null |
||
| 58 | */ |
||
| 59 | public function testCompositionWithTableGateway() |
||
| 60 | { |
||
| 61 | $entity = new User(); |
||
| 62 | $gateway = new TableGateway($entity, ["eadapter" => $this->options]); |
||
| 63 | $entityAdapter = new EntityAdapter($gateway); |
||
| 64 | |||
| 65 | $mysqliObject = $entityAdapter->getTableGateway()->getDb()->connect(); |
||
| 66 | |||
| 67 | $this->assertInstanceOf('\mysqli', $mysqliObject); |
||
| 68 | $this->assertTrue($entityAdapter->getTableGateway()->getDb()->isConnected()); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Tests if we can execute DDL statements through the TableGateway accesor |
||
| 73 | * |
||
| 74 | * @return null |
||
| 75 | */ |
||
| 76 | public function testCanExecuteStatementThroughTableGateway() |
||
| 77 | { |
||
| 78 | $options = $this->options; |
||
| 79 | $options["auto_connect"] = true; |
||
| 80 | |||
| 81 | $entity = new User(); |
||
| 82 | $gateway = new UserGateway($entity, "eadapter"); |
||
| 83 | $entityAdapter = new EntityAdapter($gateway); |
||
| 84 | |||
| 85 | $result = $entityAdapter->getTableGateway()->create(); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 86 | |||
| 87 | # mysqli |
||
| 88 | $this->assertTrue(is_object($result)); |
||
| 89 | |||
| 90 | # properties modified by execute() method |
||
| 91 | $this->assertEquals(0, $gateway->getDb()->getNumRows()); |
||
| 92 | $this->assertEquals(0, $gateway->getDb()->getNumFields()); |
||
| 93 | $this->assertEquals(0, $gateway->getDb()->getRowsAffected()); |
||
| 94 | } |
||
| 95 | |||
| 96 | /* |
||
| 97 | |-------------------------------------------------------------------------- |
||
| 98 | | ORM TOOL (Data mapper) |
||
| 99 | |-------------------------------------------------------------------------- |
||
| 100 | | |
||
| 101 | | The following tests are related to the object relational mapping. |
||
| 102 | | |
||
| 103 | */ |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Tests if we can execute INSERT statements through the EntityAdapter |
||
| 107 | * |
||
| 108 | * @return null |
||
| 109 | */ |
||
| 110 | public function testINSERTING() |
||
| 111 | { |
||
| 112 | $options = $this->options; |
||
| 113 | $options["auto_connect"] = true; |
||
| 114 | |||
| 115 | $entity = new User(); |
||
| 116 | $gateway = new TableGateway($entity, "eadapter"); |
||
| 117 | $entityAdapter = new EntityAdapter($gateway); |
||
| 118 | |||
| 119 | $firstEntity = new User(); |
||
| 120 | $firstEntity->exchangeArray([ |
||
| 121 | "ID" => 1, "USERNAME" => "Dennis.Ritchie", |
||
| 122 | ]); |
||
| 123 | |||
| 124 | $secondEntity = new User(); |
||
| 125 | $secondEntity->exchangeArray([ |
||
| 126 | "ID" => 2, "USERNAME" => "Bjarne.Stroustrup", |
||
| 127 | ]); |
||
| 128 | |||
| 129 | $result = $entityAdapter->insert($firstEntity); |
||
|
0 ignored issues
–
show
|
|||
| 130 | $result = $entityAdapter->insert($secondEntity); |
||
| 131 | |||
| 132 | $this->assertTrue(is_object($result)); |
||
| 133 | |||
| 134 | # properties modified by execute() method |
||
| 135 | $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumRows()); |
||
| 136 | $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumFields()); |
||
| 137 | |||
| 138 | # here 1 is the latest affected row, could be 2 if auto_commit were false |
||
| 139 | $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getRowsAffected()); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Tests if we can execute UPDATE statements through the EntityAdapter |
||
| 144 | * |
||
| 145 | * @return null |
||
| 146 | */ |
||
| 147 | public function testUPDATING() |
||
| 148 | { |
||
| 149 | $options = $this->options; |
||
| 150 | $options["auto_connect"] = true; |
||
| 151 | |||
| 152 | $entity = new User(); |
||
| 153 | $gateway = new TableGateway($entity, "eadapter"); |
||
| 154 | $entityAdapter = new EntityAdapter($gateway); |
||
| 155 | |||
| 156 | $firstEntity = new User(["ID" => 1]); |
||
| 157 | |||
| 158 | # tell to entity what changed |
||
| 159 | $firstEntity->exchangeArray([ |
||
| 160 | "USERNAME" => "Rasmus.Lerdorf", |
||
| 161 | ]); |
||
| 162 | |||
| 163 | $result = $entityAdapter->update($firstEntity, ["ID" => 1]); |
||
| 164 | |||
| 165 | $this->assertTrue(is_object($result)); |
||
| 166 | |||
| 167 | # properties modified by execute() method |
||
| 168 | $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumRows()); |
||
| 169 | $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumFields()); |
||
| 170 | $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getRowsAffected()); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Tests if we can execute DELETE statements through the EntityAdapter |
||
| 175 | * |
||
| 176 | * @return null |
||
| 177 | */ |
||
| 178 | public function testDELETING() |
||
| 179 | { |
||
| 180 | $options = $this->options; |
||
| 181 | $options["auto_connect"] = true; |
||
| 182 | |||
| 183 | $entity = new User(); |
||
| 184 | $gateway = new TableGateway($entity, "eadapter"); |
||
| 185 | $entityAdapter = new EntityAdapter($gateway); |
||
| 186 | |||
| 187 | # same behaviour to tablegateway |
||
| 188 | $result = $entityAdapter->delete(["ID" => 2]); |
||
| 189 | |||
| 190 | $this->assertTrue(is_object($result)); |
||
| 191 | |||
| 192 | # properties modified by execute() method |
||
| 193 | $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumRows()); |
||
| 194 | $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getNumFields()); |
||
| 195 | $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getRowsAffected()); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Tests if we can execute SELECT statements through the EntityAdapter |
||
| 200 | * |
||
| 201 | * @return null |
||
| 202 | */ |
||
| 203 | public function testSELECTING() |
||
| 204 | { |
||
| 205 | $options = $this->options; |
||
| 206 | $options["auto_connect"] = true; |
||
| 207 | |||
| 208 | $entity = new User(); |
||
| 209 | $gateway = new TableGateway($entity, "eadapter"); |
||
| 210 | $entityAdapter = new EntityAdapter($gateway); |
||
| 211 | |||
| 212 | $rows = $entityAdapter->select([ |
||
| 213 | "ID" => 1, |
||
| 214 | ]); |
||
| 215 | |||
| 216 | $this->assertTrue(is_array($rows)); |
||
| 217 | |||
| 218 | # get only the first |
||
| 219 | $user = array_shift($rows); |
||
| 220 | |||
| 221 | $this->assertInstanceOf('DroneTest\Util\User', $user); |
||
| 222 | |||
| 223 | $this->assertObjectHasAttribute("ID", $user); |
||
| 224 | $this->assertObjectHasAttribute("USERNAME", $user); |
||
| 225 | |||
| 226 | # properties modified by execute() method |
||
| 227 | $this->assertEquals(1, $entityAdapter->getTableGateway()->getDb()->getNumRows()); |
||
| 228 | $this->assertEquals(2, $entityAdapter->getTableGateway()->getDb()->getNumFields()); |
||
| 229 | $this->assertEquals(0, $entityAdapter->getTableGateway()->getDb()->getRowsAffected()); |
||
| 230 | $this->endTests(); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Function to leave all in order, you can execute tests again without problems. |
||
| 235 | * |
||
| 236 | * @return null |
||
| 237 | */ |
||
| 238 | private function endTests() |
||
| 239 | { |
||
| 240 | $options = $this->options; |
||
| 241 | $options["auto_connect"] = true; |
||
| 242 | |||
| 243 | $entity = new User(); |
||
| 244 | $gateway = new UserGateway($entity, "eadapter"); |
||
| 245 | |||
| 246 | # remove all work |
||
| 247 | $gateway->drop(); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | class User extends Entity |
||
| 252 | { |
||
| 253 | public $ID; |
||
| 254 | public $USERNAME; |
||
| 255 | |||
| 256 | public function __construct($data = []) |
||
| 257 | { |
||
| 258 | parent::__construct($data); |
||
| 259 | $this->setTableName("USER"); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | class UserGateway extends TableGateway |
||
| 264 | { |
||
| 265 | public function create() |
||
| 266 | { |
||
| 267 | $sql = "CREATE TABLE USER (ID INTEGER(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, USERNAME VARCHAR(30))"; |
||
| 268 | |||
| 269 | return $this->getDb()->execute($sql); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function drop() |
||
| 273 | { |
||
| 274 | $sql = "DROP TABLE USER"; |
||
| 275 | |||
| 276 | return $this->getDb()->execute($sql); |
||
| 277 | } |
||
| 278 | } |
||
| 279 |