From b4784bfba6fa2cdd9745dca5faa57bf37e572f8a Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 23 Mar 2020 01:50:58 +0800 Subject: [PATCH] Test improvement --- .travis.yml | 6 +-- composer.json | 4 +- tests/Unit/EloquentDestinationTest.php | 59 ++++++++++++++++++++++++-- tests/Unit/EloquentSourceTest.php | 2 + 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 96a65ec..ceb6e00 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,12 @@ language: php -dist: trusty php: - '7.1' - '7.2' - '7.3' + - '7.4' install: - - composer update + - composer install script: - ./vendor/bin/phpunit --coverage-clover ./tests/Logs/clover.xml after_script: - - php vendor/bin/php-coveralls -v \ No newline at end of file + - php vendor/bin/php-coveralls -v diff --git a/composer.json b/composer.json index f1e99d1..5bf7cb5 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,9 @@ "require-dev": { "phpunit/phpunit": "^7.0||^8.0", "fzaninotto/faker": "^1.6", - "php-coveralls/php-coveralls": "^2.0" + "php-coveralls/php-coveralls": "^2.0", + "ext-sqlite3": "*", + "ext-pdo_sqlite": "*" }, "require": { "divineomega/uxdm": "^3.0.0", diff --git a/tests/Unit/EloquentDestinationTest.php b/tests/Unit/EloquentDestinationTest.php index 656556e..d60bb03 100644 --- a/tests/Unit/EloquentDestinationTest.php +++ b/tests/Unit/EloquentDestinationTest.php @@ -1,5 +1,7 @@ pdo = new PDO('sqlite:'.__DIR__.'/Data/destination.sqlite'); + $this->pdo = new \PDO('sqlite:'.__DIR__.'/Data/destination.sqlite'); $sql = 'DROP TABLE IF EXISTS users'; $stmt = $this->pdo->prepare($sql); @@ -29,7 +31,7 @@ private function getDestination() private function createDataRows() { - $faker = Faker\Factory::create(); + $faker = \Faker\Factory::create(); $dataRows = []; @@ -46,9 +48,21 @@ private function createDataRows() return $dataRows; } + private function createEmptyDataRows() + { + $dataRows = []; + + $dataRow = new DataRow(); + $dataRow->addDataItem(new DataItem('name')); + $dataRow->addDataItem(new DataItem('value')); + $dataRows[] = $dataRow; + + return $dataRows; + } + private function alterDataRows(array $dataRows) { - $faker = Faker\Factory::create(); + $faker = \Faker\Factory::create(); foreach ($dataRows as $dataRow) { $dataItem = $dataRow->getDataItemByFieldName('value'); @@ -64,11 +78,36 @@ private function getActualArray() $stmt = $this->pdo->prepare($sql); $stmt->execute(); - $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); return $rows; } + private function getActualEmptyArray() + { + return [ + [ + 'name' => '', + 'value' => '', + ], + ]; + } + + private function getActualValueArray(array $dataRows) + { + $expectedArray = []; + + foreach ($dataRows as $dataRow) { + $expectedArrayRow = []; + foreach ($dataRow->getDataItems() as $dataItem) { + $expectedArrayRow[$dataItem->fieldName] = $dataItem->value; + } + $expectedArray[] = $expectedArrayRow; + } + + return $expectedArray; + } + private function getExpectedArray(array $dataRows) { $expectedArray = []; @@ -100,6 +139,18 @@ public function testPutDataRows() $this->assertEquals($this->getExpectedArray($dataRows), $this->getActualArray()); + $dataRows = $this->createEmptyDataRows(); + + $destination->putDataRows($dataRows); + + $this->assertEquals($this->getExpectedArray($dataRows), $this->getActualEmptyArray()); + + $dataRows = $this->alterDataRows($dataRows); + + $destination->putDataRows($dataRows); + + $this->assertEquals($this->getExpectedArray($dataRows), $this->getActualValueArray($dataRows)); + $destination->finishMigration(); } } diff --git a/tests/Unit/EloquentSourceTest.php b/tests/Unit/EloquentSourceTest.php index 3f7c192..5eb3218 100644 --- a/tests/Unit/EloquentSourceTest.php +++ b/tests/Unit/EloquentSourceTest.php @@ -1,5 +1,7 @@