diff --git a/.gitignore b/.gitignore index 524a168..6bfd688 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ tests/Logs/ tests/Unit/Data/destination.* tests/Unit/Data/JSONFilesDestination/*.json src/Examples/**/cache/** +*.cache \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 932ac1b..96a65ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: php dist: trusty php: - - '5.6' - - '7.0' - '7.1' - '7.2' + - '7.3' install: - composer update script: diff --git a/README.md b/README.md index 64cc7c1..6a84704 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,8 @@ See the sections below for more information on the available source and destinat ## Migrations -Each UXDM migration requires a source object and at least one destination object. These determine where and how data is read and written. The UXDM package comes with a variety of source and destination objects, including the following. +Each UXDM migration requires a source object and at least one destination object. These determine where and how data is read and written. +The UXDM package works with a variety of source and destination objects, including the following. * PDO (PHP Database Object) Source & Destination * Eloquent (as used in Laravel) Source & Destination @@ -66,6 +67,8 @@ Each UXDM migration requires a source object and at least one destination object * WordPress User Source * Debug Output Destination +Some of these are built-in to the core UXDM package, while others are available as separate packages. + Source and destination objects can be used in any combination. Data can be migrated from a CSV and inserted into a database, just as easily as data can be migrated from a database to a CSV. You can also use similar source and destination objects in the same migration. For example, a common use of UXDM is to use a PDO source and PDO destination to transfer data from one database to another. @@ -94,6 +97,41 @@ $migrator->setSource($pdoSource) This migration will move the `id`, `email` and `name` fields from the the `users` table in the `old-test` database, to the `new_users` table in the `new-test` database, replacing any existing records with the same `id` (the key field). +### Source data validation + +You can use UXDM to validate the source data. If validation fails part way through a migration, the migration will +halt and a `ValidationException` will be thrown. + +The code below shows how to validate various fields. + +```php +$pdoSource = new PDOSource(new PDO('mysql:dbname=old-test;host=127.0.0.1', 'root', 'password123'), 'users'); + +$pdoDestination = new PDODestination(new PDO('mysql:dbname=new-test;host=127.0.0.1', 'root', 'password456'), 'new_users'); + +$migrator = new Migrator; +$migrator->setSource($pdoSource) + ->setDestination($pdoDestination) + ->setFieldsToMigrate(['id', 'email', 'name']) + ->setValidationRules([ + 'id' => [new Required(), new IsNumeric()], + 'email' => [new Required(), new IsString(), new IsEmail()], + 'name' => [new Required(), new IsString()], + ]) + ->setKeyFields(['id']) + ->withProgressBar() + ->migrate(); +``` + +This migration will validate the source data matches the defined validation rules. + +* 'id' must be present, and numeric. +* 'email' must be present, a string, and a correctly formatted email address. +* 'name' must be present, and a string. + +UXDM uses the [Omega Validator](https://github.com/DivineOmega/omega-validator) package. +See its documentation for all available validation rules. + ### Mapping field names from source to destination This examples shows how UXDM can map field names from source to destination. diff --git a/composer.json b/composer.json index d3c6395..3724e86 100644 --- a/composer.json +++ b/composer.json @@ -8,11 +8,6 @@ "email": "jordan@hall05.co.uk" } ], - "require-dev": { - "phpunit/phpunit": "^5.7", - "fzaninotto/faker": "^1.6", - "php-coveralls/php-coveralls": "^2.0" - }, "autoload": { "psr-4": { "DivineOmega\\uxdm\\": "src/" @@ -25,14 +20,19 @@ }, "license": "LGPL-3.0-only", "require": { + "php": ">=7.1", + "ext-pdo": "*", + "ext-dom": "*", + "ext-json": "*", "psr/cache": "^1.0", - "cache/array-adapter": "^1.0", - "illuminate/support": "^5.1", - "divineomega/array_undot": "^2.0", + "divineomega/array_undot": "^4.1.0", "divineomega/php-cli-progress-bar": "^2.0", - "illuminate/database": "^5.1", - "doctrine/orm": "^2.5", - "symfony/property-access": "^3.4", - "dompdf/dompdf": "^0.8.2" + "divineomega/omega-validator": "^1.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0||^8.0", + "fzaninotto/faker": "^1.6", + "php-coveralls/php-coveralls": "^2.0", + "cache/array-adapter": "^1.0" } } diff --git a/docs/destinations/EloquentDestination.md b/docs/destinations/EloquentDestination.md index 121da3f..e4ec329 100644 --- a/docs/destinations/EloquentDestination.md +++ b/docs/destinations/EloquentDestination.md @@ -3,30 +3,5 @@ The UXDM Eloquent destination allows you to migrate data into an Eloquent model. This can be handy if you need to migrate data into a system using the Eloquent ORM, such as a Laravel project. -## Creating - -To create a new Eloquent destination, you must provide it with the class name of Eloquent model you wish to use. - -The following example creates a Eloquent destination object, using an Eloquent model called `User` in the `App` namespace. - -```php -$eloquentDestination = new EloquentDestination(\App\User::class); -``` - -## Assigning to migrator - -To use the Eloquent destination as part of a UXDM migration, you must assign it to the migrator. This process is the same for most destinations. - -```php -$migrator = new Migrator; -$migrator->setDestination($eloquentDestination); -``` - -Alternatively, you can add multiple destinations, as shown below. You can also specify the fields you wish to send to each destination by -passing an array of field names as the second parameter. - -```php -$migrator = new Migrator; -$migrator->addDestination($eloquentDestination, ['field1', 'field2']); -$migrator->addDestination($otherDestination, ['field3', 'field2']); -``` \ No newline at end of file +For installation and usage documentation, +see the [UXDM Eloquent repository](https://github.com/DivineOmega/uxdm-eloquent). \ No newline at end of file diff --git a/docs/destinations/PDFDestination.md b/docs/destinations/PDFDestination.md index f0333d7..56a09ed 100644 --- a/docs/destinations/PDFDestination.md +++ b/docs/destinations/PDFDestination.md @@ -2,70 +2,5 @@ The UXDM PDF destination allows you to migrate data into a PDF table. -## Creating - -To create a new PDF destination, you must provide it with the file path -of PDF file you wish to export data to. The first row of the PDF table -will contain the field names. - -The following example creates a PDF destination object, using a PDF -file called `users.pdf` in the same directory. - -```php -$pdfFile = __DIR__.'/users.pdf'; -$pdfDestination = new PDFDestination($pdfFile); -``` - -## Custom Paper Size & Orientation - -You can alter the PDF destination to output PDFs with a custom paper -size and orientation. To do this, use the `setPaperSize` and -`setPaperOrientation` methods of the PDF destination object, as shown -below. - -```php -$destination->setPaperSize('A5'); -$destination->setPaperOrientation('landscape'); -``` - -## HTML Prefix & Suffix - -Internally, the PDF destination renders a basic HTML table into a PDF. -If you wish to style the table, add a heading, or extra content, you can -add a HTML prefix or HTML suffix. - -An example of how to do this is shown below. - -```php -$htmlPrefix = '

My Report

- '; -$htmlSuffix = '

Created by UXDM

'; - -$destination->setHtmlPrefix($htmlPrefix); -$destination->setHtmlSuffix($htmlSuffix); -``` - -## Assigning to migrator - -To use the PDF destination as part of a UXDM migration, you must assign -it to the migrator. This process is the same for most destinations. - -```php -$migrator = new Migrator; -$migrator->setDestination($pdfDestination); -``` - -Alternatively, you can add multiple destinations, as shown below. You -can also specify the fields you wish to send to each destination by -passing an array of field names as the second parameter. - -```php -$migrator = new Migrator; -$migrator->addDestination($pdfDestination, ['field1', 'field2']); -$migrator->addDestination($otherDestination, ['field3', 'field2']); -``` +For installation and usage documentation, +see the [UXDM PDF Destination repository](https://github.com/DivineOmega/uxdm-pdf-destination). \ No newline at end of file diff --git a/docs/sources/EloquentSource.md b/docs/sources/EloquentSource.md index cda3882..636ec03 100644 --- a/docs/sources/EloquentSource.md +++ b/docs/sources/EloquentSource.md @@ -3,29 +3,5 @@ The UXDM Eloquent source allows you to source data from an Eloquent model. This can be handy if you need to migrate data from a system using the Eloquent ORM, such as a Laravel project. -## Creating - -To create a new Eloquent source, you must provide it with the class name of Eloquent model you wish to use. - -The following example creates a Eloquent source object, using an Eloquent model called `User` in the `App` namespace. - -```php -$eloquentSource = new EloquentSource(\App\User::class); -``` - -You can also pass a query callback as a second parameter to restrict the results returned, as shown below. - -```php -$eloquentSource = new EloquentSource(\App\User::class, function($query) { - $query->where('id', 1); -}); -``` - -## Assigning to migrator - -To use the Eloquent source as part of a UXDM migration, you must assign it to the migrator. This process is the same for most sources. - -```php -$migrator = new Migrator; -$migrator->setSource($eloquentSource); -``` +For installation and usage documentation, +see the [UXDM Eloquent repository](https://github.com/DivineOmega/uxdm-eloquent). diff --git a/docs/sources/XMLSource.md b/docs/sources/XMLSource.md index 51be041..bea38f5 100644 --- a/docs/sources/XMLSource.md +++ b/docs/sources/XMLSource.md @@ -54,7 +54,6 @@ For example, consider the following snippet from an XML sitemap. diff --git a/src/Examples/PDOtoPDOMigration/Example.php b/src/Examples/PDOtoPDOMigration/Example.php index 7c16338..a30fede 100644 --- a/src/Examples/PDOtoPDOMigration/Example.php +++ b/src/Examples/PDOtoPDOMigration/Example.php @@ -43,6 +43,8 @@ return true; } } + + return false; }) ->withProgressBar() ->migrate(); diff --git a/src/Examples/XMLtoCSVMigration/source.xml b/src/Examples/XMLtoCSVMigration/source.xml index 4ea2e82..34e80c7 100644 --- a/src/Examples/XMLtoCSVMigration/source.xml +++ b/src/Examples/XMLtoCSVMigration/source.xml @@ -2,7 +2,6 @@ diff --git a/src/Interfaces/DestinationInterface.php b/src/Interfaces/DestinationInterface.php index 22d390d..785219b 100644 --- a/src/Interfaces/DestinationInterface.php +++ b/src/Interfaces/DestinationInterface.php @@ -4,7 +4,7 @@ interface DestinationInterface { - public function putDataRows(array $dataRows); + public function putDataRows(array $dataRows): void; - public function finishMigration(); + public function finishMigration(): void; } diff --git a/src/Interfaces/SourceInterface.php b/src/Interfaces/SourceInterface.php index 6675a1d..2a4d0a3 100644 --- a/src/Interfaces/SourceInterface.php +++ b/src/Interfaces/SourceInterface.php @@ -4,11 +4,11 @@ interface SourceInterface { - public function getDataRows($page = 1, $fieldsToRetrieve = []); + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array; - public function countDataRows(); + public function countDataRows(): int; - public function countPages(); + public function countPages(): int; - public function getFields(); + public function getFields(): array; } diff --git a/src/Objects/DataRow.php b/src/Objects/DataRow.php index 47f2081..7dca418 100755 --- a/src/Objects/DataRow.php +++ b/src/Objects/DataRow.php @@ -2,7 +2,9 @@ namespace DivineOmega\uxdm\Objects; +use DivineOmega\OmegaValidator\Validator; use DivineOmega\uxdm\Objects\Exceptions\NoDataItemsInDataRowException; +use DivineOmega\uxdm\Objects\Exceptions\ValidationException; class DataRow { @@ -28,7 +30,17 @@ public function getDataItems() return $this->dataItems; } - public function getDataItemByFieldName($fieldName) + public function toArray() + { + $array = []; + foreach ($this->dataItems as $dataItem) { + $array[$dataItem->fieldName] = $dataItem->value; + } + + return $array; + } + + public function getDataItemByFieldName(string $fieldName) { foreach ($this->dataItems as $dataItem) { if ($dataItem->fieldName == $fieldName) { @@ -50,9 +62,9 @@ public function getKeyDataItems() return $keyDataItems; } - public function prepare(array $keyFields, array $fieldMap, callable $dataItemManipulator) + public function prepare(array $validationRules, array $keyFields, array $fieldMap, callable $dataItemManipulator) { - $this->validate(); + $this->validate($validationRules); $this->setKeyFields($keyFields); $this->mapFields($fieldMap); @@ -61,11 +73,20 @@ public function prepare(array $keyFields, array $fieldMap, callable $dataItemMan } } - private function validate() + private function validate(array $validationRules) { if (!$this->dataItems) { throw new NoDataItemsInDataRowException('Data row contains no data items. The specified source may be producing an invalid data row.'); } + + if ($validationRules) { + $validator = new Validator($this->toArray(), $validationRules); + if ($validator->fails()) { + $messages = print_r($validator->messages(), true); + + throw new ValidationException($messages); + } + } } private function setKeyFields(array $keyFields) diff --git a/src/Objects/Destinations/AssociativeArrayDestination.php b/src/Objects/Destinations/AssociativeArrayDestination.php index 84bdaf0..568ef65 100644 --- a/src/Objects/Destinations/AssociativeArrayDestination.php +++ b/src/Objects/Destinations/AssociativeArrayDestination.php @@ -13,7 +13,7 @@ public function __construct(array &$array) $this->array = &$array; } - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { foreach ($dataRows as $dataRow) { $dataItems = $dataRow->getDataItems(); @@ -28,7 +28,7 @@ public function putDataRows(array $dataRows) } } - public function finishMigration() + public function finishMigration(): void { } } diff --git a/src/Objects/Destinations/CSVDestination.php b/src/Objects/Destinations/CSVDestination.php index 77ade44..d4b9d86 100644 --- a/src/Objects/Destinations/CSVDestination.php +++ b/src/Objects/Destinations/CSVDestination.php @@ -14,7 +14,7 @@ public function __construct($file) $this->file = $file; } - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { if ($this->rowNum === 0) { $fh = fopen($this->file, 'w'); @@ -45,7 +45,7 @@ public function putDataRows(array $dataRows) fclose($fh); } - public function finishMigration() + public function finishMigration(): void { } } diff --git a/src/Objects/Destinations/DebugOutputDestination.php b/src/Objects/Destinations/DebugOutputDestination.php index 298a5a3..6e556d6 100644 --- a/src/Objects/Destinations/DebugOutputDestination.php +++ b/src/Objects/Destinations/DebugOutputDestination.php @@ -6,12 +6,12 @@ class DebugOutputDestination implements DestinationInterface { - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { var_dump($dataRows); } - public function finishMigration() + public function finishMigration(): void { } } diff --git a/src/Objects/Destinations/DoctrineDestination.php b/src/Objects/Destinations/DoctrineDestination.php deleted file mode 100644 index bfa603f..0000000 --- a/src/Objects/Destinations/DoctrineDestination.php +++ /dev/null @@ -1,93 +0,0 @@ -entityManager = $entityManager; - $this->entityRepository = $this->entityManager->getRepository($entityClassName); - $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); - } - - private function alreadyExists(array $keyDataItems) - { - $criteria = []; - - foreach ($keyDataItems as $keyDataItem) { - $criteria[$keyDataItem->fieldName] = $keyDataItem->value; - } - - if (method_exists($this->entityRepository, 'count')) { - return $this->entityRepository->count($criteria) > 0; - } else { - return $this->entityRepository->findOneBy($criteria) !== null; - } - } - - private function insertDataRow(DataRow $dataRow) - { - $dataItems = $dataRow->getDataItems(); - - $className = $this->entityRepository->getClassName(); - $newRecord = new $className(); - - foreach ($dataItems as $dataItem) { - $this->propertyAccessor->setValue($newRecord, $dataItem->fieldName, $dataItem->value); - } - - $this->entityManager->persist($newRecord); - $this->entityManager->flush(); - } - - private function updateDataRow(DataRow $dataRow) - { - $dataItems = $dataRow->getDataItems(); - $keyDataItems = $dataRow->getKeyDataItems(); - - $criteria = []; - - foreach ($keyDataItems as $keyDataItem) { - $criteria[$keyDataItem->fieldName] = $keyDataItem->value; - } - - $record = $this->entityRepository->findOneBy($criteria); - - foreach ($dataItems as $dataItem) { - $this->propertyAccessor->setValue($record, $dataItem->fieldName, $dataItem->value); - } - - $this->entityManager->flush(); - } - - public function putDataRows(array $dataRows) - { - foreach ($dataRows as $dataRow) { - $keyDataItems = $dataRow->getKeyDataItems(); - - if (!$keyDataItems) { - $this->insertDataRow($dataRow); - continue; - } - - if ($this->alreadyExists($keyDataItems)) { - $this->updateDataRow($dataRow); - } else { - $this->insertDataRow($dataRow); - } - } - } - - public function finishMigration() - { - } -} diff --git a/src/Objects/Destinations/EloquentDestination.php b/src/Objects/Destinations/EloquentDestination.php deleted file mode 100644 index 0a6c8a0..0000000 --- a/src/Objects/Destinations/EloquentDestination.php +++ /dev/null @@ -1,81 +0,0 @@ -model = new $eloquentModelClassName(); - } - - private function alreadyExists(array $keyDataItems) - { - $count = $this->model->where(function ($query) use ($keyDataItems) { - foreach ($keyDataItems as $keyDataItem) { - $query->where($keyDataItem->fieldName, $keyDataItem->value); - } - })->count(); - - return $count > 0; - } - - private function getAssocArrayFromDataRow(DataRow $dataRow) - { - $dataArray = []; - $dataItems = $dataRow->getDataItems(); - - foreach ($dataItems as $dataItem) { - $dataArray[$dataItem->fieldName] = $dataItem->value; - } - - return $dataArray; - } - - private function insertDataRow(DataRow $dataRow) - { - $this->model->create($this->getAssocArrayFromDataRow($dataRow)); - } - - private function updateDataRow(DataRow $dataRow) - { - $keyDataItems = $dataRow->getKeyDataItems(); - - $record = $this->model->where(function ($query) use ($keyDataItems) { - foreach ($keyDataItems as $keyDataItem) { - $query->where($keyDataItem->fieldName, $keyDataItem->value); - } - })->update($this->getAssocArrayFromDataRow($dataRow)); - } - - public function putDataRows(array $dataRows) - { - $this->model->unguard(); - - foreach ($dataRows as $dataRow) { - $keyDataItems = $dataRow->getKeyDataItems(); - - if (!$keyDataItems) { - $this->insertDataRow($dataRow); - continue; - } - - if ($this->alreadyExists($keyDataItems)) { - $this->updateDataRow($dataRow); - } else { - $this->insertDataRow($dataRow); - } - } - - $this->model->reguard(); - } - - public function finishMigration() - { - } -} diff --git a/src/Objects/Destinations/HtmlDestination.php b/src/Objects/Destinations/HtmlDestination.php index eb623bd..8c5cf56 100644 --- a/src/Objects/Destinations/HtmlDestination.php +++ b/src/Objects/Destinations/HtmlDestination.php @@ -14,7 +14,7 @@ public function __construct($file) $this->file = $file; } - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { if ($this->rowNum === 0) { $fh = fopen($this->file, 'w'); @@ -48,7 +48,7 @@ public function putDataRows(array $dataRows) } } - public function finishMigration() + public function finishMigration(): void { $fh = fopen($this->file, 'a'); fwrite($fh, ''); diff --git a/src/Objects/Destinations/JSONFilesDestination.php b/src/Objects/Destinations/JSONFilesDestination.php index a488972..953c930 100644 --- a/src/Objects/Destinations/JSONFilesDestination.php +++ b/src/Objects/Destinations/JSONFilesDestination.php @@ -14,7 +14,7 @@ public function __construct($directory) $this->directory = realpath($directory); } - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { foreach ($dataRows as $dataRow) { $dataItems = $dataRow->getDataItems(); @@ -35,7 +35,7 @@ public function putDataRows(array $dataRows) } } - public function finishMigration() + public function finishMigration(): void { } } diff --git a/src/Objects/Destinations/MarkdownDestination.php b/src/Objects/Destinations/MarkdownDestination.php index 3cd99bb..5c26e7a 100644 --- a/src/Objects/Destinations/MarkdownDestination.php +++ b/src/Objects/Destinations/MarkdownDestination.php @@ -14,7 +14,7 @@ public function __construct($file) $this->file = $file; } - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { if ($this->rowNum === 0) { $fh = fopen($this->file, 'w'); @@ -46,7 +46,7 @@ public function putDataRows(array $dataRows) fclose($fh); } - public function finishMigration() + public function finishMigration(): void { } } diff --git a/src/Objects/Destinations/NullDestination.php b/src/Objects/Destinations/NullDestination.php index 0e7bf4f..e4de557 100644 --- a/src/Objects/Destinations/NullDestination.php +++ b/src/Objects/Destinations/NullDestination.php @@ -6,11 +6,11 @@ class NullDestination implements DestinationInterface { - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { } - public function finishMigration() + public function finishMigration(): void { } } diff --git a/src/Objects/Destinations/PDFDestination.php b/src/Objects/Destinations/PDFDestination.php deleted file mode 100644 index f51905f..0000000 --- a/src/Objects/Destinations/PDFDestination.php +++ /dev/null @@ -1,88 +0,0 @@ -file = $file; - } - - public function setHtmlPrefix($htmlPrefix) - { - $this->htmlPrefix = $htmlPrefix; - } - - public function setHtmlSuffix($htmlSuffix) - { - $this->htmlSuffix = $htmlSuffix; - } - - public function setPaperSize($paperSize) - { - $this->paperSize = $paperSize; - } - - public function setPaperOrientation($paperOrientation) - { - $this->paperOrientation = $paperOrientation; - } - - public function putDataRows(array $dataRows) - { - foreach ($dataRows as $dataRow) { - $dataItems = $dataRow->getDataItems(); - - if ($this->rowNum === 0) { - $fieldNames = []; - foreach ($dataItems as $dataItem) { - $fieldNames[] = htmlentities($dataItem->fieldName); - } - $this->html .= ''; - $this->html .= ''; - } - - $values = []; - foreach ($dataItems as $dataItem) { - $values[] = htmlentities($dataItem->value); - } - $this->html .= ''; - - $this->rowNum++; - } - } - - public function finishMigration() - { - $this->html .= '
'; - $this->html .= implode('', $fieldNames); - $this->html .= '
'; - $this->html .= implode('', $values); - $this->html .= '
'; - - $htmlToRender = $this->htmlPrefix. - $this->html. - $this->htmlSuffix; - - $dompdf = new Dompdf(); - $dompdf->loadHtml($htmlToRender); - $dompdf->setPaper($this->paperSize, $this->paperOrientation); - - $dompdf->render(); - $pdfContent = $dompdf->output(); - - file_put_contents($this->file, $pdfContent); - } -} diff --git a/src/Objects/Destinations/PDODestination.php b/src/Objects/Destinations/PDODestination.php index ce941ad..9bb92f9 100644 --- a/src/Objects/Destinations/PDODestination.php +++ b/src/Objects/Destinations/PDODestination.php @@ -77,7 +77,7 @@ private function insertDataRow(DataRow $dataRow) $sql .= ') values ('; - foreach ($dataItems as $dataItem) { + for ($i = 0; $i < count($dataItems); $i++) { $sql .= '? , '; } $sql = substr($sql, 0, -2); @@ -132,7 +132,7 @@ private function updateDataRow(DataRow $dataRow) $stmt->execute(); } - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { if ($this->transactions) { $this->pdo->beginTransaction(); @@ -166,7 +166,7 @@ public function putDataRows(array $dataRows) } } - public function finishMigration() + public function finishMigration(): void { } } diff --git a/src/Objects/Destinations/XMLDestination.php b/src/Objects/Destinations/XMLDestination.php index 484cc66..cd6ce64 100644 --- a/src/Objects/Destinations/XMLDestination.php +++ b/src/Objects/Destinations/XMLDestination.php @@ -10,7 +10,6 @@ class XMLDestination implements DestinationInterface { private $file; private $domDoc; - private $domNode; public function __construct($file, DOMDocument $domDoc, DOMElement $rootElement, $rowElementName = 'row') { @@ -20,7 +19,7 @@ public function __construct($file, DOMDocument $domDoc, DOMElement $rootElement, $this->rowElementName = $rowElementName; } - public function putDataRows(array $dataRows) + public function putDataRows(array $dataRows): void { foreach ($dataRows as $dataRow) { $dataItems = $dataRow->getDataItems(); @@ -34,7 +33,7 @@ public function putDataRows(array $dataRows) } } - public function finishMigration() + public function finishMigration(): void { file_put_contents($this->file, $this->domDoc->saveXML()); } diff --git a/src/Objects/Exceptions/ValidationException.php b/src/Objects/Exceptions/ValidationException.php new file mode 100644 index 0000000..8aaabb8 --- /dev/null +++ b/src/Objects/Exceptions/ValidationException.php @@ -0,0 +1,9 @@ +validationRules = $rules; + + return $this; + } + + public function setSourceCache(CacheItemPoolInterface $sourceCachePool, string $sourceCacheKey, int $sourceCacheExpiresAfter = 60 * 60 * 24) { $this->sourceCachePool = $sourceCachePool; $this->sourceCacheKey = $sourceCacheKey; @@ -180,16 +188,21 @@ public function migrate() } foreach ($dataRows as $key => $dataRow) { - $dataRow->prepare($this->keyFields, $this->fieldMap, $dataItemManipulator ? $dataItemManipulator : $nullDataItemManipulation); + $dataRow->prepare( + $this->validationRules, + $this->keyFields, + $this->fieldMap, + $dataItemManipulator ? $dataItemManipulator : $nullDataItemManipulation + ); } - if ($dataRowManipulator !== null) { + if (is_callable($dataRowManipulator)) { foreach ($dataRows as $dataRow) { $dataRowManipulator($dataRow); } } - if ($skipIfTrueCheck !== null) { + if (is_callable($skipIfTrueCheck)) { foreach ($dataRows as $key => $dataRow) { if ($skipIfTrueCheck($dataRow)) { unset($dataRows[$key]); diff --git a/src/Objects/Sources/AssociativeArraySource.php b/src/Objects/Sources/AssociativeArraySource.php index 8b84a3f..5d366fc 100644 --- a/src/Objects/Sources/AssociativeArraySource.php +++ b/src/Objects/Sources/AssociativeArraySource.php @@ -21,7 +21,7 @@ public function __construct(array &$array) } } - public function getDataRows($page = 1, $fieldsToRetrieve = []) + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array { $offset = 0 + (($page - 1) * $this->perPage); @@ -44,17 +44,17 @@ public function getDataRows($page = 1, $fieldsToRetrieve = []) return $dataRows; } - public function getFields() + public function getFields(): array { return $this->fields; } - public function countDataRows() + public function countDataRows(): int { return count($this->array); } - public function countPages() + public function countPages(): int { return ceil($this->countDataRows() / $this->perPage); } diff --git a/src/Objects/Sources/CSVSource.php b/src/Objects/Sources/CSVSource.php index 06a0b68..7f2b6bc 100755 --- a/src/Objects/Sources/CSVSource.php +++ b/src/Objects/Sources/CSVSource.php @@ -41,7 +41,7 @@ private function getCSVLines($offset, $amount) return $lines; } - public function getDataRows($page = 1, $fieldsToRetrieve = []) + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array { $offset = 1 + (($page - 1) * $this->perPage); @@ -64,12 +64,12 @@ public function getDataRows($page = 1, $fieldsToRetrieve = []) return $dataRows; } - public function getFields() + public function getFields(): array { return $this->fields; } - public function countDataRows() + public function countDataRows(): int { $file = new \SplFileObject($this->file, 'r'); $file->seek(PHP_INT_MAX); @@ -77,7 +77,7 @@ public function countDataRows() return $file->key(); } - public function countPages() + public function countPages(): int { return ceil($this->countDataRows() / $this->perPage); } diff --git a/src/Objects/Sources/EloquentSource.php b/src/Objects/Sources/EloquentSource.php deleted file mode 100755 index 1644d29..0000000 --- a/src/Objects/Sources/EloquentSource.php +++ /dev/null @@ -1,67 +0,0 @@ -model = new $eloquentModelClassName(); - $this->queryCallback = $queryCallback; - - $this->fields = array_keys($this->model->first()->getAttributes()); - } - - public function getDataRows($page = 1, $fieldsToRetrieve = []) - { - $offset = ($page - 1) * $this->perPage; - - $query = $this->model->offset($offset)->limit($this->perPage); - - if (is_callable($this->queryCallback)) { - $queryCallback = $this->queryCallback; - $queryCallback($query); - } - - $records = $query->get(); - - $dataRows = []; - - foreach ($records as $record) { - $attributes = array_dot($record->toArray()); - $dataRow = new DataRow(); - foreach ($fieldsToRetrieve as $key) { - if (in_array($key, $fieldsToRetrieve) && array_key_exists($key, $attributes)) { - $dataRow->addDataItem(new DataItem($key, $attributes[$key])); - } - } - $dataRows[] = $dataRow; - } - - return $dataRows; - } - - public function getFields() - { - return $this->fields; - } - - public function countDataRows() - { - return $this->model->count(); - } - - public function countPages() - { - return ceil($this->countDataRows() / $this->perPage); - } -} diff --git a/src/Objects/Sources/JSONFilesSource.php b/src/Objects/Sources/JSONFilesSource.php index d7c7204..a9bd983 100644 --- a/src/Objects/Sources/JSONFilesSource.php +++ b/src/Objects/Sources/JSONFilesSource.php @@ -29,7 +29,7 @@ public function __construct(array $files = []) $this->fields = array_unique($this->fields); } - public function getDataRows($page = 1, $fieldsToRetrieve = []) + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array { $offset = 0 + (($page - 1) * $this->perPage); @@ -59,17 +59,17 @@ public function getDataRows($page = 1, $fieldsToRetrieve = []) return $dataRows; } - public function getFields() + public function getFields(): array { return $this->fields; } - public function countDataRows() + public function countDataRows(): int { return count($this->files); } - public function countPages() + public function countPages(): int { return ceil($this->countDataRows() / $this->perPage); } diff --git a/src/Objects/Sources/PDOSource.php b/src/Objects/Sources/PDOSource.php index 9cf7d44..5516838 100644 --- a/src/Objects/Sources/PDOSource.php +++ b/src/Objects/Sources/PDOSource.php @@ -32,7 +32,7 @@ private function getTableFields() $sql = $this->getSQL(['*']); $stmt = $this->pdo->prepare($sql); - $this->bindLimitParameters($stmt, 0, 1); + $this->bindLimitParameters($stmt, 0); $stmt->execute(); @@ -97,7 +97,7 @@ private function bindLimitParameters(PDOStatement $stmt, $offset) $stmt->bindValue(2, $this->perPage, PDO::PARAM_INT); } - public function getDataRows($page = 1, $fieldsToRetrieve = []) + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array { $offset = (($page - 1) * $this->perPage); @@ -123,12 +123,12 @@ public function getDataRows($page = 1, $fieldsToRetrieve = []) return $dataRows; } - public function getFields() + public function getFields(): array { return $this->fields; } - public function countDataRows() + public function countDataRows(): int { $sql = $this->getSQL([]); $fromPos = stripos($sql, 'from'); @@ -145,7 +145,7 @@ public function countDataRows() return $row['countDataRows']; } - public function countPages() + public function countPages(): int { return ceil($this->countDataRows() / $this->perPage); } diff --git a/src/Objects/Sources/WordPressPostSource.php b/src/Objects/Sources/WordPressPostSource.php index 0f21df3..e4a5e44 100644 --- a/src/Objects/Sources/WordPressPostSource.php +++ b/src/Objects/Sources/WordPressPostSource.php @@ -106,7 +106,7 @@ private function bindLimitParameters(PDOStatement $stmt, $offset, $perPage) $stmt->bindValue(2, $perPage, PDO::PARAM_INT); } - public function getDataRows($page = 1, $fieldsToRetrieve = []) + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array { $offset = (($page - 1) * $this->perPage); @@ -143,12 +143,12 @@ public function getDataRows($page = 1, $fieldsToRetrieve = []) return $dataRows; } - public function getFields() + public function getFields(): array { return $this->fields; } - public function countDataRows() + public function countDataRows(): int { $sql = $this->getPostSQL([]); $fromPos = stripos($sql, 'from'); @@ -165,7 +165,7 @@ public function countDataRows() return $countRow['count']; } - public function countPages() + public function countPages(): int { return ceil($this->countDataRows() / $this->perPage); } diff --git a/src/Objects/Sources/WordPressUserSource.php b/src/Objects/Sources/WordPressUserSource.php index 10f1853..63b03a1 100644 --- a/src/Objects/Sources/WordPressUserSource.php +++ b/src/Objects/Sources/WordPressUserSource.php @@ -104,7 +104,7 @@ private function bindLimitParameters(PDOStatement $stmt, $offset, $perPage) $stmt->bindValue(2, $perPage, PDO::PARAM_INT); } - public function getDataRows($page = 1, $fieldsToRetrieve = []) + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array { $offset = (($page - 1) * $this->perPage); @@ -141,12 +141,12 @@ public function getDataRows($page = 1, $fieldsToRetrieve = []) return $dataRows; } - public function getFields() + public function getFields(): array { return $this->fields; } - public function countDataRows() + public function countDataRows(): int { $sql = $this->getUserSQL([]); $fromPos = stripos($sql, 'from'); @@ -163,7 +163,7 @@ public function countDataRows() return $countRow['count']; } - public function countPages() + public function countPages(): int { return ceil($this->countDataRows() / $this->perPage); } diff --git a/src/Objects/Sources/XMLSource.php b/src/Objects/Sources/XMLSource.php index 9b2ae67..f0d658d 100755 --- a/src/Objects/Sources/XMLSource.php +++ b/src/Objects/Sources/XMLSource.php @@ -16,7 +16,7 @@ class XMLSource implements SourceInterface private $fields = []; private $perPage = 10; - public function __construct($file, $xpathQuery, $namespaces = []) + public function __construct($file, $xpathQuery) { $doc = new DOMDocument(); $doc->load($file); @@ -51,7 +51,7 @@ private function getXMLFields() return $fields; } - public function getDataRows($page = 1, $fieldsToRetrieve = []) + public function getDataRows(int $page = 1, array $fieldsToRetrieve = []): array { $offset = (($page - 1) * $this->perPage); @@ -80,7 +80,7 @@ public function getDataRows($page = 1, $fieldsToRetrieve = []) return $dataRows; } - public function getFields() + public function getFields(): array { if (!$this->fields) { $this->fields = array_values($this->getXMLFields()); @@ -89,14 +89,14 @@ public function getFields() return $this->fields; } - public function countDataRows() + public function countDataRows(): int { $domNodeList = $this->xpath->query($this->xpathQuery); return $domNodeList->length; } - public function countPages() + public function countPages(): int { return ceil($this->countDataRows() / $this->perPage); } diff --git a/tests/Integration/MigratorTest.php b/tests/Integration/MigratorTest.php index bd8bc86..4093b2b 100644 --- a/tests/Integration/MigratorTest.php +++ b/tests/Integration/MigratorTest.php @@ -114,9 +114,7 @@ public function testMigrator() $dataRow->addDataItem(new DataItem('md5_name', md5($dataRow->getDataItemByFieldName('name')->value))); }) ->setSkipIfTrueCheck(function ($dataRow) { - if ($dataRow->getDataItemByFieldName('name')->value == 'TIM') { - return true; - } + return $dataRow->getDataItemByFieldName('name')->value == 'TIM'; }) ->migrate(); @@ -143,9 +141,7 @@ public function testMigratorWithProgressBar() $dataRow->addDataItem(new DataItem('md5_name', md5($dataRow->getDataItemByFieldName('name')->value))); }) ->setSkipIfTrueCheck(function ($dataRow) { - if ($dataRow->getDataItemByFieldName('name')->value == 'TIM') { - return true; - } + return $dataRow->getDataItemByFieldName('name')->value == 'TIM'; }) ->withProgressBar() ->migrate(); @@ -215,9 +211,7 @@ public function testMigratorWithNoFieldsToMigrate() $dataRow->addDataItem(new DataItem('md5_name', md5($dataRow->getDataItemByFieldName('name')->value))); }) ->setSkipIfTrueCheck(function ($dataRow) { - if ($dataRow->getDataItemByFieldName('name')->value == 'TIM') { - return true; - } + return $dataRow->getDataItemByFieldName('name')->value == 'TIM'; }) ->migrate(); @@ -243,9 +237,7 @@ public function testMigratorWithMultipleDestinations() $dataRow->addDataItem(new DataItem('md5_name', md5($dataRow->getDataItemByFieldName('name')->value))); }) ->setSkipIfTrueCheck(function ($dataRow) { - if ($dataRow->getDataItemByFieldName('name')->value == 'TIM') { - return true; - } + return $dataRow->getDataItemByFieldName('name')->value == 'TIM'; }) ->migrate(); @@ -274,9 +266,7 @@ public function testMigratorWithCache() $dataRow->addDataItem(new DataItem('md5_name', md5($dataRow->getDataItemByFieldName('name')->value))); }) ->setSkipIfTrueCheck(function ($dataRow) { - if ($dataRow->getDataItemByFieldName('name')->value == 'TIM') { - return true; - } + return $dataRow->getDataItemByFieldName('name')->value == 'TIM'; }) ->migrate(); @@ -297,9 +287,7 @@ public function testMigratorWithCache() $dataRow->addDataItem(new DataItem('md5_name', md5($dataRow->getDataItemByFieldName('name')->value))); }) ->setSkipIfTrueCheck(function ($dataRow) { - if ($dataRow->getDataItemByFieldName('name')->value == 'TIM') { - return true; - } + return $dataRow->getDataItemByFieldName('name')->value == 'TIM'; }) ->migrate(); diff --git a/tests/Unit/Data/source.xml b/tests/Unit/Data/source.xml index f78adfc..5073d15 100644 --- a/tests/Unit/Data/source.xml +++ b/tests/Unit/Data/source.xml @@ -2,7 +2,6 @@ diff --git a/tests/Unit/DataRowTest.php b/tests/Unit/DataRowTest.php index 25fce78..b93b404 100644 --- a/tests/Unit/DataRowTest.php +++ b/tests/Unit/DataRowTest.php @@ -1,8 +1,12 @@ prepare($keyFields, $fieldMap, $dataItemManipulator); + $dataRow->prepare([], $keyFields, $fieldMap, $dataItemManipulator); $dataItemRetrieved = $dataRow->getDataItemByFieldName($dataItem->fieldName); @@ -130,7 +134,7 @@ public function testFieldMapPreparation() $dataItemManipulator = function () { }; - $dataRow->prepare($keyFields, $fieldMap, $dataItemManipulator); + $dataRow->prepare([], $keyFields, $fieldMap, $dataItemManipulator); $dataItemRetrieved = $dataRow->getDataItemByFieldName($dataItem->fieldName); @@ -153,7 +157,7 @@ public function testDataItemManipulatorPreparation() } }; - $dataRow->prepare($keyFields, $fieldMap, $dataItemManipulator); + $dataRow->prepare([], $keyFields, $fieldMap, $dataItemManipulator); $dataItemRetrieved = $dataRow->getDataItemByFieldName($dataItem->fieldName); @@ -171,6 +175,45 @@ public function testValidationOfEmptyDataRow() $dataItemManipulator = function () { }; - $dataRow->prepare($keyFields, $fieldMap, $dataItemManipulator); + $dataRow->prepare([], $keyFields, $fieldMap, $dataItemManipulator); + } + + public function testValidationRulesFailure() + { + $expectedExceptionMessageArray = [ + 'email' => [ + IsEmail::class => 'The email must be a valid email address.', + ], + ]; + + $this->expectException(ValidationException::class); + $this->expectExceptionMessage(print_r($expectedExceptionMessageArray, true)); + + $dataRow = new DataRow(); + $dataRow->addDataItem(new DataItem('email', 'not-an-email')); + + $rules = ['email' => [new Required(), new IsString(), new IsEmail()]]; + $keyFields = []; + $fieldMap = []; + $dataItemManipulator = function () { + }; + + $dataRow->prepare($rules, $keyFields, $fieldMap, $dataItemManipulator); + } + + public function testValidationRulesSuccess() + { + $this->expectNotToPerformAssertions(); + + $dataRow = new DataRow(); + $dataRow->addDataItem(new DataItem('email', 'test@example.com')); + + $rules = ['email' => [new Required(), new IsString(), new IsEmail()]]; + $keyFields = []; + $fieldMap = []; + $dataItemManipulator = function () { + }; + + $dataRow->prepare($rules, $keyFields, $fieldMap, $dataItemManipulator); } } diff --git a/tests/Unit/DoctrineDestinationTest.php b/tests/Unit/DoctrineDestinationTest.php deleted file mode 100644 index ebd12fb..0000000 --- a/tests/Unit/DoctrineDestinationTest.php +++ /dev/null @@ -1,115 +0,0 @@ -pdo = new PDO('sqlite:'.__DIR__.'/Data/destination.sqlite'); - - $sql = 'DROP TABLE IF EXISTS users'; - $stmt = $this->pdo->prepare($sql); - $stmt->execute(); - - $sql = 'CREATE TABLE IF NOT EXISTS users (name TEXT, value INTEGER)'; - $stmt = $this->pdo->prepare($sql); - $stmt->execute(); - - $isDevMode = true; - $config = Setup::createAnnotationMetadataConfiguration(['/tmp'], $isDevMode); - - $conn = [ - 'driver' => 'pdo_sqlite', - 'path' => __DIR__.'/Data/destination.sqlite', - ]; - - $entityManager = EntityManager::create($conn, $config); - - return new DoctrineDestination($entityManager, User::class); - } - - private function createDataRows() - { - $faker = Faker\Factory::create(); - - $dataRows = []; - - $dataRow = new DataRow(); - $dataRow->addDataItem(new DataItem('name', $faker->word, true)); - $dataRow->addDataItem(new DataItem('value', $faker->randomNumber)); - $dataRows[] = $dataRow; - - $dataRow = new DataRow(); - $dataRow->addDataItem(new DataItem('name', $faker->word, true)); - $dataRow->addDataItem(new DataItem('value', $faker->randomNumber)); - $dataRows[] = $dataRow; - - return $dataRows; - } - - private function alterDataRows(array $dataRows) - { - $faker = Faker\Factory::create(); - - foreach ($dataRows as $dataRow) { - $dataItem = $dataRow->getDataItemByFieldName('value'); - $dataItem->value = $faker->randomNumber; - } - - return $dataRows; - } - - private function getActualArray() - { - $sql = 'SELECT * FROM users'; - $stmt = $this->pdo->prepare($sql); - $stmt->execute(); - - $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); - - return $rows; - } - - private function getExpectedArray(array $dataRows) - { - $expectedArray = []; - - foreach ($dataRows as $dataRow) { - $expectedArrayRow = []; - foreach ($dataRow->getDataItems() as $dataItem) { - $expectedArrayRow[$dataItem->fieldName] = $dataItem->value; - } - $expectedArray[] = $expectedArrayRow; - } - - return $expectedArray; - } - - public function testPutDataRows() - { - $dataRows = $this->createDataRows(); - - $destination = $this->getDestination(); - - $destination->putDataRows($dataRows); - - $this->assertEquals($this->getExpectedArray($dataRows), $this->getActualArray()); - - $dataRows = $this->alterDataRows($dataRows); - - $destination->putDataRows($dataRows); - - $this->assertEquals($this->getExpectedArray($dataRows), $this->getActualArray()); - - $destination->finishMigration(); - } -} diff --git a/tests/Unit/EloquentDestinationTest.php b/tests/Unit/EloquentDestinationTest.php deleted file mode 100644 index a6453e0..0000000 --- a/tests/Unit/EloquentDestinationTest.php +++ /dev/null @@ -1,105 +0,0 @@ -pdo = new PDO('sqlite:'.__DIR__.'/Data/destination.sqlite'); - - $sql = 'DROP TABLE IF EXISTS users'; - $stmt = $this->pdo->prepare($sql); - $stmt->execute(); - - $sql = 'CREATE TABLE IF NOT EXISTS users (name TEXT, value INTEGER)'; - $stmt = $this->pdo->prepare($sql); - $stmt->execute(); - - require_once 'includes/BootDestinationEloquentDatabase.php'; - - return new EloquentDestination(User::class); - } - - private function createDataRows() - { - $faker = Faker\Factory::create(); - - $dataRows = []; - - $dataRow = new DataRow(); - $dataRow->addDataItem(new DataItem('name', $faker->word, true)); - $dataRow->addDataItem(new DataItem('value', $faker->randomNumber)); - $dataRows[] = $dataRow; - - $dataRow = new DataRow(); - $dataRow->addDataItem(new DataItem('name', $faker->word, true)); - $dataRow->addDataItem(new DataItem('value', $faker->randomNumber)); - $dataRows[] = $dataRow; - - return $dataRows; - } - - private function alterDataRows(array $dataRows) - { - $faker = Faker\Factory::create(); - - foreach ($dataRows as $dataRow) { - $dataItem = $dataRow->getDataItemByFieldName('value'); - $dataItem->value = $faker->randomNumber; - } - - return $dataRows; - } - - private function getActualArray() - { - $sql = 'SELECT * FROM users'; - $stmt = $this->pdo->prepare($sql); - $stmt->execute(); - - $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); - - return $rows; - } - - private function getExpectedArray(array $dataRows) - { - $expectedArray = []; - - foreach ($dataRows as $dataRow) { - $expectedArrayRow = []; - foreach ($dataRow->getDataItems() as $dataItem) { - $expectedArrayRow[$dataItem->fieldName] = $dataItem->value; - } - $expectedArray[] = $expectedArrayRow; - } - - return $expectedArray; - } - - public function testPutDataRows() - { - $dataRows = $this->createDataRows(); - - $destination = $this->getDestination(); - - $destination->putDataRows($dataRows); - - $this->assertEquals($this->getExpectedArray($dataRows), $this->getActualArray()); - - $dataRows = $this->alterDataRows($dataRows); - - $destination->putDataRows($dataRows); - - $this->assertEquals($this->getExpectedArray($dataRows), $this->getActualArray()); - - $destination->finishMigration(); - } -} diff --git a/tests/Unit/EloquentSourceTest.php b/tests/Unit/EloquentSourceTest.php deleted file mode 100644 index 3f7c192..0000000 --- a/tests/Unit/EloquentSourceTest.php +++ /dev/null @@ -1,128 +0,0 @@ -where('name', 'Bear'); - }); - } - - public function testGetFields() - { - $source = $this->createSource(); - - $this->assertEquals(['id', 'name', 'email'], $source->getFields()); - } - - public function testGetDataRows() - { - $source = $this->createSource(); - - $dataRows = $source->getDataRows(1, ['name', 'email']); - - $this->assertCount(2, $dataRows); - - $dataItems = $dataRows[0]->getDataItems(); - - $this->assertCount(2, $dataItems); - - $this->assertEquals('name', $dataItems[0]->fieldName); - $this->assertEquals('Tim', $dataItems[0]->value); - - $this->assertEquals('email', $dataItems[1]->fieldName); - $this->assertEquals('tim@example.com', $dataItems[1]->value); - - $dataItems = $dataRows[1]->getDataItems(); - - $this->assertCount(2, $dataItems); - - $this->assertEquals('name', $dataItems[0]->fieldName); - $this->assertEquals('Bear', $dataItems[0]->value); - - $this->assertEquals('email', $dataItems[1]->fieldName); - $this->assertEquals('bear@example.com', $dataItems[1]->value); - - $dataRows = $source->getDataRows(2, ['name', 'email']); - - $this->assertCount(0, $dataRows); - } - - public function testGetDataRowsOnlyOneField() - { - $source = $this->createSource(); - - $dataRows = $source->getDataRows(1, ['name']); - - $this->assertCount(2, $dataRows); - - $dataItems = $dataRows[0]->getDataItems(); - - $this->assertCount(1, $dataItems); - - $this->assertEquals('name', $dataItems[0]->fieldName); - $this->assertEquals('Tim', $dataItems[0]->value); - - $dataItems = $dataRows[1]->getDataItems(); - - $this->assertCount(1, $dataItems); - - $this->assertEquals('name', $dataItems[0]->fieldName); - $this->assertEquals('Bear', $dataItems[0]->value); - - $dataRows = $source->getDataRows(2, ['name']); - - $this->assertCount(0, $dataRows); - } - - public function testGetDataRowsWithQueryCallback() - { - $source = $this->createSourceWithCallback(); - - $dataRows = $source->getDataRows(1, ['name', 'email']); - - $this->assertCount(1, $dataRows); - - $dataItems = $dataRows[0]->getDataItems(); - - $this->assertCount(2, $dataItems); - - $this->assertEquals('name', $dataItems[0]->fieldName); - $this->assertEquals('Bear', $dataItems[0]->value); - - $this->assertEquals('email', $dataItems[1]->fieldName); - $this->assertEquals('bear@example.com', $dataItems[1]->value); - - $dataRows = $source->getDataRows(2, ['name', 'email']); - - $this->assertCount(0, $dataRows); - } - - public function testCountDataRows() - { - $source = $this->createSource(); - - $this->assertEquals(2, $source->countDataRows()); - } - - public function testCountPages() - { - $source = $this->createSource(); - - $this->assertEquals(1, $source->countPages()); - } -} diff --git a/tests/Unit/PDFDestinationTest.php b/tests/Unit/PDFDestinationTest.php deleted file mode 100644 index 990a776..0000000 --- a/tests/Unit/PDFDestinationTest.php +++ /dev/null @@ -1,139 +0,0 @@ -addDataItem(new DataItem('name', $faker->word)); - $dataRow->addDataItem(new DataItem('value', $faker->randomNumber)); - $dataRows[] = $dataRow; - - $dataRow = new DataRow(); - $dataRow->addDataItem(new DataItem('name', $faker->word)); - $dataRow->addDataItem(new DataItem('value', $faker->randomNumber)); - $dataRows[] = $dataRow; - - return $dataRows; - } - - private function getExpectedFileContent(array $dataRows, $paperSize, $paperOrientation, $htmlPrefix = '', $htmlSuffix = '') - { - $htmlToRender = ''; - - foreach ($dataRows as $dataRow) { - $htmlToRender .= ''; - } - - $htmlToRender .= '
namevalue
'; - $htmlToRender .= $dataRow->getDataItemByFieldName('name')->value; - $htmlToRender .= ''; - $htmlToRender .= $dataRow->getDataItemByFieldName('value')->value; - $htmlToRender .= '
'; - - $htmlToRender = $htmlPrefix. - $htmlToRender. - $htmlSuffix; - - $dompdf = new Dompdf(); - $dompdf->loadHtml($htmlToRender); - $dompdf->setPaper($paperSize, $paperOrientation); - - $dompdf->render(); - $pdfContent = $dompdf->output(); - - return $pdfContent; - } - - public function testPutDataRowsDefaultPaper() - { - $paperSize = 'A4'; - $paperOrientation = 'portrait'; - - $dataRows = $this->createDataRows(); - - $file = __DIR__.'/Data/destination.pdf'; - - $destination = new PDFDestination($file); - $destination->putDataRows($dataRows); - $destination->finishMigration(); - - $fileContent = file_get_contents($file); - - // Compare similarity, as PDF output is never identical. - $expectedFileContent = $this->getExpectedFileContent($dataRows, $paperSize, $paperOrientation); - similar_text($expectedFileContent, $fileContent, $percent); - - $this->assertGreaterThanOrEqual(92, $percent, - 'PDF destination\'s output was too different from the expected output.'); - } - - public function testPutDataRowsCustomPaper() - { - $paperSize = 'A5'; - $paperOrientation = 'landscape'; - - $dataRows = $this->createDataRows(); - - $file = __DIR__.'/Data/destination.pdf'; - - $destination = new PDFDestination($file); - $destination->setPaperSize($paperSize); - $destination->setPaperOrientation($paperOrientation); - $destination->putDataRows($dataRows); - $destination->finishMigration(); - - $fileContent = file_get_contents($file); - - // Compare similarity, as PDF output is never identical. - $expectedFileContent = $this->getExpectedFileContent($dataRows, $paperSize, $paperOrientation); - similar_text($expectedFileContent, $fileContent, $percent); - - $this->assertGreaterThanOrEqual(92, $percent, - 'PDF destination\'s output was too different from the expected output.'); - } - - public function testPutDataRowsPrefixAndSuffix() - { - $paperSize = 'A4'; - $paperOrientation = 'portrait'; - $htmlPrefix = '

My Report

- '; - $htmlSuffix = '

Created by UXDM

'; - - $dataRows = $this->createDataRows(); - - $file = __DIR__.'/Data/destination.pdf'; - - $destination = new PDFDestination($file); - $destination->setHtmlPrefix($htmlPrefix); - $destination->setHtmlSuffix($htmlSuffix); - $destination->putDataRows($dataRows); - $destination->finishMigration(); - - $fileContent = file_get_contents($file); - - // Compare similarity, as PDF output is never identical. - $expectedFileContent = $this->getExpectedFileContent($dataRows, $paperSize, $paperOrientation, - $htmlPrefix, $htmlSuffix); - similar_text($expectedFileContent, $fileContent, $percent); - - $this->assertGreaterThanOrEqual(95, $percent, - 'PDF destination\'s output was too different from the expected output.'); - } -} diff --git a/tests/Unit/WordPressUserSourceTest.php b/tests/Unit/WordPressUserSourceTest.php index 320b743..7dfb24c 100644 --- a/tests/Unit/WordPressUserSourceTest.php +++ b/tests/Unit/WordPressUserSourceTest.php @@ -7,7 +7,7 @@ final class WordPressUserSourceTest extends TestCase { private function createSource() { - return new WordPressUserSource(new PDO('sqlite:'.__DIR__.'/Data/wordpress.sqlite'), 'post'); + return new WordPressUserSource(new PDO('sqlite:'.__DIR__.'/Data/wordpress.sqlite')); } public function testGetFields() diff --git a/tests/Unit/includes/BootDestinationEloquentDatabase.php b/tests/Unit/includes/BootDestinationEloquentDatabase.php deleted file mode 100644 index 24c51a7..0000000 --- a/tests/Unit/includes/BootDestinationEloquentDatabase.php +++ /dev/null @@ -1,17 +0,0 @@ -addConnection([ - 'driver' => 'sqlite', - 'database' => __DIR__.'/../Data/destination.sqlite', - 'prefix' => '', -]); - -// Make this Capsule instance available globally via static methods -$capsule->setAsGlobal(); - -// Setup the Eloquent ORM -$capsule->bootEloquent(); diff --git a/tests/Unit/includes/BootSourceEloquentDatabase.php b/tests/Unit/includes/BootSourceEloquentDatabase.php deleted file mode 100644 index b144be5..0000000 --- a/tests/Unit/includes/BootSourceEloquentDatabase.php +++ /dev/null @@ -1,17 +0,0 @@ -addConnection([ - 'driver' => 'sqlite', - 'database' => __DIR__.'/../Data/source.sqlite', - 'prefix' => '', -]); - -// Make this Capsule instance available globally via static methods -$capsule->setAsGlobal(); - -// Setup the Eloquent ORM -$capsule->bootEloquent(); diff --git a/tests/Unit/includes/TestClasses/Eloquent/User.php b/tests/Unit/includes/TestClasses/Eloquent/User.php deleted file mode 100644 index 64defbc..0000000 --- a/tests/Unit/includes/TestClasses/Eloquent/User.php +++ /dev/null @@ -1,11 +0,0 @@ -