From a1f549d7ffb8694fac7ac43e014d6486fd911b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Gon=C3=A7alves?= Date: Tue, 27 Feb 2024 16:07:08 +0000 Subject: [PATCH] Drop support for PHP 8.0 and doctrine/dbal 2.x (#39) BREAKING CHANGES - Remove support for PHP 8.0 - Upgrade to only use doctrine/dbal:^3.8 - Remove code supporting version 2.x of doctrine/dbal - Use Symfony 6.4 components for development - Rename classes that had "ElasticSearch" to "Elasticsearch" - Upgrade to PHPUnit 10.5 - Fix tests - Update documentation - Upgrade CI workflow to use PHP 8.1 --- .github/workflows/continuous-integration.yml | 4 +- composer.json | 19 ++- docs/FixtureTypes/directory-loader.md | 12 +- docs/FixtureTypes/elasticsearch.md | 36 +++--- phpunit.xml.dist | 45 ++++--- src/Adapter/ConnectionSqlFixture.php | 4 +- .../DirectoryFileSearchTrait.php | 6 +- ...=> ElasticsearchArrayDirectoryFixture.php} | 4 +- ... => ElasticsearchJsonDirectoryFixture.php} | 4 +- src/Adapter/ElasticSearchFileFixture.php | 26 ---- src/Adapter/ElasticSearchFixture.php | 9 -- src/Adapter/ElasticsearchFileFixture.php | 53 ++++++++ src/Adapter/ElasticsearchFixture.php | 9 ++ ....php => ElasticsearchFixtureInterface.php} | 4 +- ...rait.php => ElasticsearchFixtureTrait.php} | 2 +- ...e.php => ElasticsearchJsonFileFixture.php} | 2 +- ...e.php => ElasticsearchPhpArrayFixture.php} | 2 +- src/Adapter/HttpClientPhpArrayFixture.php | 2 +- src/Exception/LoadFailedException.php | 21 ++++ src/Executor/ConnectionExecutor.php | 12 +- ...Executor.php => ElasticsearchExecutor.php} | 6 +- src/Loader/ElasticSearchFixturesLoader.php | 14 --- src/Loader/ElasticsearchFixturesLoader.php | 14 +++ src/Purger/ConnectionPurger.php | 18 ++- ...archPurger.php => ElasticsearchPurger.php} | 2 +- src/Tools/ConnectionToolsTrait.php | 17 --- .../AbstractElasticsearchFixtureTestCase.php | 37 ++++++ .../ConnectionSqlDirectoryFixtureTest.php | 10 +- tests/Adapter/ConnectionSqlFixtureTest.php | 10 +- ...lasticsearchArrayDirectoryFixtureTest.php} | 9 +- ...eTest.php => ElasticsearchFixtureTest.php} | 6 +- ...ElasticsearchJsonDirectoryFixtureTest.php} | 117 ++++++++++++++++-- tests/Executor/CachePoolExecutorTest.php | 10 +- tests/Executor/ConnectionExecutorTest.php | 25 ++-- ...Test.php => ElasticsearchExecutorTest.php} | 12 +- ...NonTransactionalConnectionExecutorTest.php | 19 ++- .../ElasticSearchFixturesLoaderTest.php | 36 ------ .../ElasticsearchFixturesLoaderTest.php | 36 ++++++ .../AbstractConnectionPurgerTestCase.php | 47 ++++--- tests/Purger/CachePoolPurgerTest.php | 1 + tests/Purger/ConnectionPurgerTest.php | 81 ++++++++---- ...erTest.php => ElasticsearchPurgerTest.php} | 7 +- tests/Purger/HttpClientPurgerTest.php | 1 + .../NonTransactionalConnectionPurgerTest.php | 35 ++++-- .../docs1.json | 20 --- .../docs2.json | 11 -- .../docs1.json | 20 --- .../docs3.json | 11 -- .../docs1.php | 0 .../docs2.php | 0 .../docs1.json | 20 +++ .../docs2.json | 11 ++ .../docs3.json | 0 .../docs1.json | 20 +++ .../docs2.json | 0 .../docs3.json | 11 ++ ...> ElasticsearchArrayDirectoryFixture1.php} | 4 +- ...Fixture1.php => ElasticsearchFixture1.php} | 6 +- ...Fixture2.php => ElasticsearchFixture2.php} | 6 +- ...Fixture3.php => ElasticsearchFixture3.php} | 16 ++- ...=> ElasticsearchJsonDirectoryFixture1.php} | 4 +- ...=> ElasticsearchJsonDirectoryFixture2.php} | 4 +- tests/Tools/ConnectionToolsTest.php | 38 +++--- tests/Utils/ConnectionUtilsTrait.php | 15 --- 64 files changed, 631 insertions(+), 432 deletions(-) rename src/Adapter/DirectoryLoader/{ElasticSearchArrayDirectoryFixture.php => ElasticsearchArrayDirectoryFixture.php} (59%) rename src/Adapter/DirectoryLoader/{ElasticSearchJsonDirectoryFixture.php => ElasticsearchJsonDirectoryFixture.php} (59%) delete mode 100644 src/Adapter/ElasticSearchFileFixture.php delete mode 100644 src/Adapter/ElasticSearchFixture.php create mode 100644 src/Adapter/ElasticsearchFileFixture.php create mode 100644 src/Adapter/ElasticsearchFixture.php rename src/Adapter/{ElasticSearchFixtureInterface.php => ElasticsearchFixtureInterface.php} (67%) rename src/Adapter/{ElasticSearchFixtureTrait.php => ElasticsearchFixtureTrait.php} (96%) rename src/Adapter/{ElasticSearchJsonFileFixture.php => ElasticsearchJsonFileFixture.php} (81%) rename src/Adapter/{ElasticSearchPhpArrayFixture.php => ElasticsearchPhpArrayFixture.php} (80%) create mode 100644 src/Exception/LoadFailedException.php rename src/Executor/{ElasticSearchExecutor.php => ElasticsearchExecutor.php} (79%) delete mode 100644 src/Loader/ElasticSearchFixturesLoader.php create mode 100644 src/Loader/ElasticsearchFixturesLoader.php rename src/Purger/{ElasticSearchPurger.php => ElasticsearchPurger.php} (93%) create mode 100644 tests/Adapter/AbstractElasticsearchFixtureTestCase.php rename tests/Adapter/{ElasticSearchArrayDirectoryFixtureTest.php => ElasticsearchArrayDirectoryFixtureTest.php} (90%) rename tests/Adapter/{ElasticSearchFixtureTest.php => ElasticsearchFixtureTest.php} (90%) rename tests/Adapter/{ElasticSearchJsonDirectoryFixtureTest.php => ElasticsearchJsonDirectoryFixtureTest.php} (54%) rename tests/Executor/{ElasticSearchExecutorTest.php => ElasticsearchExecutorTest.php} (82%) delete mode 100644 tests/Loader/ElasticSearchFixturesLoaderTest.php create mode 100644 tests/Loader/ElasticsearchFixturesLoaderTest.php rename tests/Purger/{ElasticSearchPurgerTest.php => ElasticsearchPurgerTest.php} (88%) delete mode 100644 tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs1.json delete mode 100644 tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs2.json delete mode 100644 tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs1.json delete mode 100644 tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs3.json rename tests/TestFixtures/Elasticsearch/{ElasticSearchArrayDirectoryFixture1 => ElasticsearchArrayDirectoryFixture1}/docs1.php (100%) rename tests/TestFixtures/Elasticsearch/{ElasticSearchArrayDirectoryFixture1 => ElasticsearchArrayDirectoryFixture1}/docs2.php (100%) create mode 100644 tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs1.json create mode 100644 tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs2.json rename tests/TestFixtures/Elasticsearch/{ElasticSearchJsonDirectoryFixture1 => ElasticsearchJsonDirectoryFixture1}/docs3.json (100%) create mode 100644 tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs1.json rename tests/TestFixtures/Elasticsearch/{ElasticSearchJsonDirectoryFixture2 => ElasticsearchJsonDirectoryFixture2}/docs2.json (100%) create mode 100644 tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs3.json rename tests/TestFixtures/{ElasticSearchArrayDirectoryFixture1.php => ElasticsearchArrayDirectoryFixture1.php} (72%) rename tests/TestFixtures/{ElasticSearchFixture1.php => ElasticsearchFixture1.php} (55%) rename tests/TestFixtures/{ElasticSearchFixture2.php => ElasticsearchFixture2.php} (55%) rename tests/TestFixtures/{ElasticSearchFixture3.php => ElasticsearchFixture3.php} (72%) rename tests/TestFixtures/{ElasticSearchJsonDirectoryFixture2.php => ElasticsearchJsonDirectoryFixture1.php} (66%) rename tests/TestFixtures/{ElasticSearchJsonDirectoryFixture1.php => ElasticsearchJsonDirectoryFixture2.php} (66%) delete mode 100644 tests/Utils/ConnectionUtilsTrait.php diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 1a23a96..e51371d 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -17,7 +17,7 @@ jobs: strategy: matrix: php-version: - - 8.0 + - 8.1 dependencies: - lowest - highest @@ -58,7 +58,7 @@ jobs: - uses: actions/download-artifact@v4 with: - name: build-8-highest-coverage + name: build-8.1-highest-coverage path: tests/.results/ - name: Fix Code Coverage Paths diff --git a/composer.json b/composer.json index 6e23f94..b9f1053 100644 --- a/composer.json +++ b/composer.json @@ -22,18 +22,17 @@ } ], "require": { - "php": ">=8.0", + "php": ">=8.1", "ext-json": "*" }, "require-dev": { - "doctrine/dbal": "^2.9 || ^3.1", - "elasticsearch/elasticsearch": "^7.0", + "doctrine/dbal": "^3.8", + "elasticsearch/elasticsearch": "^7.1", "kununu/scripts": ">=4.0", - "phpunit/phpunit": "^9.5", - "psr/cache": "^1.0", - "symfony/http-client": "^4.4 | ^5.2", - "symfony/http-foundation": "^4.4 | ^5.2", - "symfony/phpunit-bridge": "^5.2" + "phpunit/phpunit": "^10.5", + "psr/cache": "^2.0", + "symfony/http-client": "^6.4", + "symfony/http-foundation": "^6.4" }, "suggest": { "psr/cache": "Load fixtures for implementation of the PSR6 standard", @@ -54,8 +53,8 @@ } }, "scripts": { - "test": "phpunit --no-coverage tests", - "test-coverage": "XDEBUG_MODE=coverage phpunit --coverage-clover tests/.results/coverage.xml --coverage-html tests/.results/html/ tests" + "test": "phpunit --no-coverage --no-logging --no-progress", + "test-coverage": "XDEBUG_MODE=coverage phpunit" }, "scripts-descriptions": { "test": "Run all tests", diff --git a/docs/FixtureTypes/directory-loader.md b/docs/FixtureTypes/directory-loader.md index cc359f5..ab8c3e1 100644 --- a/docs/FixtureTypes/directory-loader.md +++ b/docs/FixtureTypes/directory-loader.md @@ -15,7 +15,7 @@ For any fixture extending this class: - All `*.sql` files inside that directory will be loaded when invoking the `load` method of that fixture class. - Other files will be ignored. -## ElasticSearchArrayDirectoryFixture +## ElasticsearchArrayDirectoryFixture For any fixture extending this class: @@ -26,7 +26,7 @@ For any fixture extending this class: - Each of your `*.php` file should return an array of arrays, where each entry in the main array is a representation of the Elasticsearch document. - Your fixture class must also implement the `getDocumentIdForBulkIndexation` method, take into consideration the `prepareDocument` and `getDocumentType` methods, as described in [Elasticsearch Fixtures](elasticsearch.md). -## ElasticSearchJsonDirectoryFixture +## ElasticsearchJsonDirectoryFixture For any fixture extending this class: @@ -35,7 +35,7 @@ For any fixture extending this class: - All `*.json` files inside that directory will be loaded when invoking the `load` method of that fixture class. - Other files will be ignored. - Each of your `*.json` file should be a JSON array of JSON objects, where each object in the main array is a representation of the Elasticsearch document. -- Each file will be decoded to a PHP array, and from there the workflow is the same as `ElasticSearchArrayDirectoryFixture`. +- Each file will be decoded to a PHP array, and from there the workflow is the same as `ElasticsearchArrayDirectoryFixture`. - Your fixture class must also implement the `getDocumentIdForBulkIndexation` method, take into consideration the `prepareDocument` and `getDocumentType` methods, as described in [Elasticsearch Fixtures](elasticsearch.md). ## HttpClientArrayDirectoryFixture @@ -57,7 +57,7 @@ Your fixtures should be at directory level like this: * MyConnectionFixture1.php * MyConnectionFixture2.php * MyHttpClientFixture1.php - * MyElasticSearchFixtureFromJson.php + * MyElasticsearchFixtureFromJson.php +- Sql\ | +- MyConnectionFixture1\ | | * sql-file.sql @@ -66,10 +66,10 @@ Your fixtures should be at directory level like this: | | * this-file-is-ignored-because-it-has-a-different-extension.txt | | * my-fixtures.sql +- Elasticsearch\ - | +- MyElasticSearchFixtureFromArray\ + | +- MyElasticsearchFixtureFromArray\ | | * my-elasticsearch-docs-array1.php | | * my-elasticsearch-docs-array2.php - | +- MyElasticSearchFixtureFromJson\ + | +- MyElasticsearchFixtureFromJson\ | | * my-elasticsearch-docs-json1.json | | * my-elasticsearch-docs-json2.json +- Responses\ diff --git a/docs/FixtureTypes/elasticsearch.md b/docs/FixtureTypes/elasticsearch.md index 92020d1..8cbc6b4 100644 --- a/docs/FixtureTypes/elasticsearch.md +++ b/docs/FixtureTypes/elasticsearch.md @@ -14,7 +14,7 @@ composer require elastic/elasticsearch ### 1. Create fixture classes -The first step to load Elasticsearch Fixtures is to create fixtures classes. This classes must implement the [ElasticSearchFixtureInterface](/src/Adapter/ElasticSearchFixtureInterface.php) or if you want to easily use *bulk* inserts on your fixtures you can extend the class [ElasticSearchFixture](/src/Adapter/ElasticSearchFixture.php). +The first step to load Elasticsearch Fixtures is to create fixtures classes. This classes must implement the [ElasticsearchFixtureInterface](/src/Adapter/ElasticsearchFixtureInterface.php) or if you want to easily use *bulk* inserts on your fixtures you can extend the class [ElasticsearchFixture](/src/Adapter/ElasticsearchFixture.php). #### Inserting a single document @@ -24,9 +24,9 @@ The first step to load Elasticsearch Fixtures is to create fixtures classes. Thi declare(strict_types=1); use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFixtureInterface; +use Kununu\DataFixtures\Adapter\ElasticsearchFixtureInterface; -final class MyFixture implements ElasticSearchFixtureInterface +final class MyFixture implements ElasticsearchFixtureInterface { public function load(Client $elasticSearch, string $indexName): void { @@ -48,9 +48,9 @@ final class MyFixture implements ElasticSearchFixtureInterface declare(strict_types=1); use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFixture; +use Kununu\DataFixtures\Adapter\ElasticsearchFixture; -final class MyFixture extends ElasticSearchFixture +final class MyFixture extends ElasticsearchFixture { public function load(Client $elasticSearch, string $indexName): void { @@ -118,7 +118,7 @@ final class MyFixture extends ElasticSearchFixture #### Bulk insert documents from files -To include a series of files extend `Kununu\DataFixtures\Adapter\ElasticSearchFileFixture`. +To include a series of files extend `Kununu\DataFixtures\Adapter\ElasticsearchFileFixture`. There are two options here: @@ -148,9 +148,9 @@ Example: declare(strict_types=1); use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFileFixture; +use Kununu\DataFixtures\Adapter\ElasticsearchFileFixture; -final class MyFixture extends ElasticSearchFileFixture +final class MyFixture extends ElasticsearchFileFixture { protected function fileNames(): array { @@ -213,9 +213,9 @@ Or for JSON files: declare(strict_types=1); use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFileFixture; +use Kununu\DataFixtures\Adapter\ElasticsearchFileFixture; -final class MyFixture extends ElasticSearchFileFixture +final class MyFixture extends ElasticsearchFileFixture { protected function fileNames(): array { @@ -319,17 +319,17 @@ In order to load the fixtures that you created in the previous step you will nee declare(strict_types=1); use Elasticsearch\ClientBuilder; -use Kununu\DataFixtures\Executor\ElasticSearchExecutor; -use Kununu\DataFixtures\Loader\ElasticSearchFixturesLoader; -use Kununu\DataFixtures\Purger\ElasticSearchPurger; +use Kununu\DataFixtures\Executor\ElasticsearchExecutor; +use Kununu\DataFixtures\Loader\ElasticsearchFixturesLoader; +use Kununu\DataFixtures\Purger\ElasticsearchPurger; $client = ClientBuilder::create()->build(); -$purger = new ElasticSearchPurger($client, 'my_index'); +$purger = new ElasticsearchPurger($client, 'my_index'); -$executor = new ElasticSearchExecutor($client, 'my_index', $purger); +$executor = new ElasticsearchExecutor($client, 'my_index', $purger); -$loader = new ElasticSearchFixturesLoader(); +$loader = new ElasticsearchFixturesLoader(); $loader->addFixture(new MyFixture()); $executor->execute($loader->getFixtures()); @@ -348,9 +348,9 @@ By default, when loading fixtures the Elasticsearch index is purged. If you want execute($loader->getFixtures(), true); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9477c3a..f6cfbf5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,26 +1,25 @@ - + - - - src - - - - - - - - - tests - - - - - - - - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" + colors="true" beStrictAboutChangesToGlobalState="true" testdox="true"> + + + + + + + + + tests + + + + + + + + src + + diff --git a/src/Adapter/ConnectionSqlFixture.php b/src/Adapter/ConnectionSqlFixture.php index 284725d..e563ee3 100644 --- a/src/Adapter/ConnectionSqlFixture.php +++ b/src/Adapter/ConnectionSqlFixture.php @@ -12,8 +12,8 @@ abstract class ConnectionSqlFixture extends AbstractFileLoaderFixture implements final public function load(Connection $connection): void { - parent::loadFiles(fn (?string $sql) => match (true) { - is_string($sql) => $this->executeQuery($connection, $sql), + parent::loadFiles(fn(?string $sql) => match (true) { + is_string($sql) => $connection->executeStatement($sql), default => null }); } diff --git a/src/Adapter/DirectoryLoader/DirectoryFileSearchTrait.php b/src/Adapter/DirectoryLoader/DirectoryFileSearchTrait.php index c18e009..5ba450a 100644 --- a/src/Adapter/DirectoryLoader/DirectoryFileSearchTrait.php +++ b/src/Adapter/DirectoryLoader/DirectoryFileSearchTrait.php @@ -18,9 +18,9 @@ protected function searchFileNames(string $extension, string $subDirectory): arr $files = []; if ($handle = opendir($root)) { while (false !== ($file = readdir($handle))) { - if ('.' !== $file && - '..' !== $file && - $extension === strtolower(substr($file, strrpos($file, '.') + 1)) + if ('.' !== $file + && '..' !== $file + && $extension === strtolower(substr($file, strrpos($file, '.') + 1)) ) { $files[] = sprintf('%s/%s', $root, $file); } diff --git a/src/Adapter/DirectoryLoader/ElasticSearchArrayDirectoryFixture.php b/src/Adapter/DirectoryLoader/ElasticsearchArrayDirectoryFixture.php similarity index 59% rename from src/Adapter/DirectoryLoader/ElasticSearchArrayDirectoryFixture.php rename to src/Adapter/DirectoryLoader/ElasticsearchArrayDirectoryFixture.php index 03be368..4cc2d42 100644 --- a/src/Adapter/DirectoryLoader/ElasticSearchArrayDirectoryFixture.php +++ b/src/Adapter/DirectoryLoader/ElasticsearchArrayDirectoryFixture.php @@ -3,9 +3,9 @@ namespace Kununu\DataFixtures\Adapter\DirectoryLoader; -use Kununu\DataFixtures\Adapter\ElasticSearchPhpArrayFixture; +use Kununu\DataFixtures\Adapter\ElasticsearchPhpArrayFixture; -abstract class ElasticSearchArrayDirectoryFixture extends ElasticSearchPhpArrayFixture +abstract class ElasticsearchArrayDirectoryFixture extends ElasticsearchPhpArrayFixture { use DirectoryFileSearchTrait; diff --git a/src/Adapter/DirectoryLoader/ElasticSearchJsonDirectoryFixture.php b/src/Adapter/DirectoryLoader/ElasticsearchJsonDirectoryFixture.php similarity index 59% rename from src/Adapter/DirectoryLoader/ElasticSearchJsonDirectoryFixture.php rename to src/Adapter/DirectoryLoader/ElasticsearchJsonDirectoryFixture.php index 3315607..d22ec83 100644 --- a/src/Adapter/DirectoryLoader/ElasticSearchJsonDirectoryFixture.php +++ b/src/Adapter/DirectoryLoader/ElasticsearchJsonDirectoryFixture.php @@ -3,9 +3,9 @@ namespace Kununu\DataFixtures\Adapter\DirectoryLoader; -use Kununu\DataFixtures\Adapter\ElasticSearchJsonFileFixture; +use Kununu\DataFixtures\Adapter\ElasticsearchJsonFileFixture; -abstract class ElasticSearchJsonDirectoryFixture extends ElasticSearchJsonFileFixture +abstract class ElasticsearchJsonDirectoryFixture extends ElasticsearchJsonFileFixture { use DirectoryFileSearchTrait; diff --git a/src/Adapter/ElasticSearchFileFixture.php b/src/Adapter/ElasticSearchFileFixture.php deleted file mode 100644 index 94cc403..0000000 --- a/src/Adapter/ElasticSearchFileFixture.php +++ /dev/null @@ -1,26 +0,0 @@ - match (true) { - empty($documents) => null, - default => $elasticSearch->bulk( - array_merge( - ['body' => $this->prepareBodyForBulkIndexation($indexName, $documents)], - is_string($documentType = $this->getDocumentType()) ? ['type' => $documentType] : [] - ) - ) - } - ); - } -} diff --git a/src/Adapter/ElasticSearchFixture.php b/src/Adapter/ElasticSearchFixture.php deleted file mode 100644 index 612fefd..0000000 --- a/src/Adapter/ElasticSearchFixture.php +++ /dev/null @@ -1,9 +0,0 @@ - $this->bulk($elasticSearch, $indexName, $documents, $throwOnFail)); + } + + private function bulk(Client $elasticSearch, string $indexName, array $documents, bool $throwOnFail): void + { + if (empty($documents)) { + return; + } + + $result = $elasticSearch->bulk( + array_merge( + ['body' => $this->prepareBodyForBulkIndexation($indexName, $documents)], + is_string($documentType = $this->getDocumentType()) ? ['type' => $documentType] : [] + ) + ); + + if ($throwOnFail && ($result['errors'] ?? false)) { + $errors = array_map( + fn(array $item): stdClass => (object) [ + self::INDEX => $item[self::UPDATE]['_index'], + 'id' => $item[self::UPDATE]['_id'], + 'status' => $item[self::UPDATE]['status'], + self::ERROR => $item[self::UPDATE][self::ERROR], + ], + array_filter( + $result['items'] ?? [], + fn(array $item): bool => isset($item[self::UPDATE][self::ERROR]) + ) + ); + + throw new LoadFailedException(static::class, $errors); + } + } +} diff --git a/src/Adapter/ElasticsearchFixture.php b/src/Adapter/ElasticsearchFixture.php new file mode 100644 index 0000000..8a434f7 --- /dev/null +++ b/src/Adapter/ElasticsearchFixture.php @@ -0,0 +1,9 @@ + $httpClient->addResponses($responses)); + parent::loadFiles(fn(array $responses) => $httpClient->addResponses($responses)); } protected function getFileExtension(): string diff --git a/src/Exception/LoadFailedException.php b/src/Exception/LoadFailedException.php new file mode 100644 index 0000000..ab3abcd --- /dev/null +++ b/src/Exception/LoadFailedException.php @@ -0,0 +1,21 @@ +purger->purge(); } - $this->executeQuery( - $this->connection, + $this->connection->executeStatement( $this->getDisableForeignKeysChecksStatementByDriver($this->connection->getDriver()) ); @@ -49,8 +48,7 @@ public function execute(array $fixtures, bool $append = false): void } throw $e; } finally { - $this->executeQuery( - $this->connection, + $this->connection->executeStatement( $this->getEnableForeignKeysChecksStatementByDriver($this->connection->getDriver()) ); } diff --git a/src/Executor/ElasticSearchExecutor.php b/src/Executor/ElasticsearchExecutor.php similarity index 79% rename from src/Executor/ElasticSearchExecutor.php rename to src/Executor/ElasticsearchExecutor.php index 1c82ccd..28c59a5 100644 --- a/src/Executor/ElasticSearchExecutor.php +++ b/src/Executor/ElasticsearchExecutor.php @@ -4,10 +4,10 @@ namespace Kununu\DataFixtures\Executor; use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFixtureInterface; +use Kununu\DataFixtures\Adapter\ElasticsearchFixtureInterface; use Kununu\DataFixtures\Purger\PurgerInterface; -final class ElasticSearchExecutor implements ExecutorInterface +final class ElasticsearchExecutor implements ExecutorInterface { public function __construct( private Client $elasticSearch, @@ -29,7 +29,7 @@ public function execute(array $fixtures, bool $append = false): void $this->elasticSearch->indices()->refresh(['index' => $this->indexName]); } - private function load(ElasticSearchFixtureInterface $fixture): void + private function load(ElasticsearchFixtureInterface $fixture): void { $fixture->load($this->elasticSearch, $this->indexName); } diff --git a/src/Loader/ElasticSearchFixturesLoader.php b/src/Loader/ElasticSearchFixturesLoader.php deleted file mode 100644 index 1b29d89..0000000 --- a/src/Loader/ElasticSearchFixturesLoader.php +++ /dev/null @@ -1,14 +0,0 @@ -tables = $this->getDatabaseTables($connection); + $this->tables = $this->connection->createSchemaManager()->listTableNames(); } public function purge(): void @@ -42,8 +42,7 @@ public function purge(): void } try { - $this->executeQuery( - $this->connection, + $this->connection->executeStatement( $this->getDisableForeignKeysChecksStatementByDriver($this->connection->getDriver()) ); @@ -60,8 +59,7 @@ public function purge(): void } throw $e; } finally { - $this->executeQuery( - $this->connection, + $this->connection->executeStatement( $this->getEnableForeignKeysChecksStatementByDriver($this->connection->getDriver()) ); } @@ -87,6 +85,6 @@ private function purgeTable(AbstractPlatform $platform, string $tableName): void ? sprintf('DELETE FROM %s', $this->connection->quoteIdentifier($tableName)) : $platform->getTruncateTableSQL($tableName, true); - $this->executeQuery($this->connection, $query); + $this->connection->executeStatement($query); } } diff --git a/src/Purger/ElasticSearchPurger.php b/src/Purger/ElasticsearchPurger.php similarity index 93% rename from src/Purger/ElasticSearchPurger.php rename to src/Purger/ElasticsearchPurger.php index bf2a7ba..37f4c82 100644 --- a/src/Purger/ElasticSearchPurger.php +++ b/src/Purger/ElasticsearchPurger.php @@ -6,7 +6,7 @@ use Elasticsearch\Client; use stdClass; -final class ElasticSearchPurger implements PurgerInterface +final class ElasticsearchPurger implements PurgerInterface { public function __construct(private Client $elasticSearch, private string $indexName) { diff --git a/src/Tools/ConnectionToolsTrait.php b/src/Tools/ConnectionToolsTrait.php index 51de0e1..cfc4167 100644 --- a/src/Tools/ConnectionToolsTrait.php +++ b/src/Tools/ConnectionToolsTrait.php @@ -3,7 +3,6 @@ namespace Kununu\DataFixtures\Tools; -use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractSQLiteDriver; @@ -12,22 +11,6 @@ trait ConnectionToolsTrait { - protected function getDatabaseTables(Connection $connection): array - { - // This way we support both doctrine/dbal ^2.9 and ^3.1 - $method = method_exists($connection, 'createSchemaManager') ? 'createSchemaManager' : 'getSchemaManager'; - - return $connection->$method()->listTableNames(); - } - - protected function executeQuery(Connection $connection, string $sql): int - { - // This way we support both doctrine/dbal ^2.9 and ^3.1 - $method = method_exists($connection, 'executeStatement') ? 'executeStatement' : 'exec'; - - return $connection->$method($sql); - } - protected function getDisableForeignKeysChecksStatementByDriver(Driver $driver): string { $databasePlatform = $driver->getDatabasePlatform(); diff --git a/tests/Adapter/AbstractElasticsearchFixtureTestCase.php b/tests/Adapter/AbstractElasticsearchFixtureTestCase.php new file mode 100644 index 0000000..9b65eaa --- /dev/null +++ b/tests/Adapter/AbstractElasticsearchFixtureTestCase.php @@ -0,0 +1,37 @@ +expectException(LoadFailedException::class); + } + + $this->getElasticsearchFixtureInterface()->load($this->client, self::INDEX_NAME, $throwOnFail); + } + + abstract public static function loadDataProvider(): array; + + abstract protected function getElasticsearchFixtureInterface(): ElasticsearchFixtureInterface; + + protected function setUp(): void + { + $this->client = $this->createMock(Client::class); + } +} diff --git a/tests/Adapter/ConnectionSqlDirectoryFixtureTest.php b/tests/Adapter/ConnectionSqlDirectoryFixtureTest.php index 6eeabe6..a0ddbcd 100644 --- a/tests/Adapter/ConnectionSqlDirectoryFixtureTest.php +++ b/tests/Adapter/ConnectionSqlDirectoryFixtureTest.php @@ -5,15 +5,12 @@ use Doctrine\DBAL\Connection; use Kununu\DataFixtures\Tests\TestFixtures\ConnectionSqlDirectoryFixture1; -use Kununu\DataFixtures\Tests\Utils\ConnectionUtilsTrait; use LogicException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; final class ConnectionSqlDirectoryFixtureTest extends TestCase { - use ConnectionUtilsTrait; - private MockObject|Connection $connection; public function testLoad(): void @@ -30,10 +27,11 @@ public function testLoad(): void $this->connection ->expects($this->exactly(2)) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturnCallback(fn(string $fixtureContent) => match ($fixtureContent) { - $fixture1Content, $fixture2Content => 1, - default => throw new LogicException(sprintf('Unknown fixture content "%s"', $fixtureContent)) + $fixture1Content, + $fixture2Content => 1, + default => throw new LogicException(sprintf('Unknown fixture content "%s"', $fixtureContent)) }); $fixture = new ConnectionSqlDirectoryFixture1(); diff --git a/tests/Adapter/ConnectionSqlFixtureTest.php b/tests/Adapter/ConnectionSqlFixtureTest.php index 601d33e..968e081 100644 --- a/tests/Adapter/ConnectionSqlFixtureTest.php +++ b/tests/Adapter/ConnectionSqlFixtureTest.php @@ -7,15 +7,12 @@ use Kununu\DataFixtures\Exception\InvalidFileException; use Kununu\DataFixtures\Tests\TestFixtures\ConnectionSqlFixture1; use Kununu\DataFixtures\Tests\TestFixtures\InvalidConnectionSqlFixture; -use Kununu\DataFixtures\Tests\Utils\ConnectionUtilsTrait; use LogicException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; final class ConnectionSqlFixtureTest extends TestCase { - use ConnectionUtilsTrait; - private MockObject|Connection $connection; public function testLoad(): void @@ -32,10 +29,11 @@ public function testLoad(): void $this->connection ->expects($this->exactly(2)) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturnCallback(fn(string $fixtureContent) => match ($fixtureContent) { - $fixture1Content, $fixture2Content => 1, - default => throw new LogicException(sprintf('Unknown fixture content "%s"', $fixtureContent)) + $fixture1Content, + $fixture2Content => 1, + default => throw new LogicException(sprintf('Unknown fixture content "%s"', $fixtureContent)) }); $fixture = new ConnectionSqlFixture1(); diff --git a/tests/Adapter/ElasticSearchArrayDirectoryFixtureTest.php b/tests/Adapter/ElasticsearchArrayDirectoryFixtureTest.php similarity index 90% rename from tests/Adapter/ElasticSearchArrayDirectoryFixtureTest.php rename to tests/Adapter/ElasticsearchArrayDirectoryFixtureTest.php index 5a871aa..cca7221 100644 --- a/tests/Adapter/ElasticSearchArrayDirectoryFixtureTest.php +++ b/tests/Adapter/ElasticsearchArrayDirectoryFixtureTest.php @@ -4,11 +4,11 @@ namespace Kununu\DataFixtures\Tests\Adapter; use Elasticsearch\Client; -use Kununu\DataFixtures\Tests\TestFixtures\ElasticSearchArrayDirectoryFixture1; +use Kununu\DataFixtures\Tests\TestFixtures\ElasticsearchArrayDirectoryFixture1; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -final class ElasticSearchArrayDirectoryFixtureTest extends TestCase +final class ElasticsearchArrayDirectoryFixtureTest extends TestCase { private MockObject|Client $client; @@ -80,9 +80,10 @@ public function testLoad(): void $this->callback( fn(array $bulk): bool => ($bulk == $bulk1 || $bulk == $bulk2) ) - ); + ) + ->willReturn(['errors' => false]); - (new ElasticSearchArrayDirectoryFixture1())->load($this->client, 'my_index'); + (new ElasticsearchArrayDirectoryFixture1())->load($this->client, 'my_index'); } protected function setUp(): void diff --git a/tests/Adapter/ElasticSearchFixtureTest.php b/tests/Adapter/ElasticsearchFixtureTest.php similarity index 90% rename from tests/Adapter/ElasticSearchFixtureTest.php rename to tests/Adapter/ElasticsearchFixtureTest.php index 8374d26..4495c04 100644 --- a/tests/Adapter/ElasticSearchFixtureTest.php +++ b/tests/Adapter/ElasticsearchFixtureTest.php @@ -4,11 +4,11 @@ namespace Kununu\DataFixtures\Tests\Adapter; use Elasticsearch\Client; -use Kununu\DataFixtures\Tests\TestFixtures\ElasticSearchFixture3; +use Kununu\DataFixtures\Tests\TestFixtures\ElasticsearchFixture3; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -final class ElasticSearchFixtureTest extends TestCase +final class ElasticsearchFixtureTest extends TestCase { private MockObject|Client $client; @@ -53,7 +53,7 @@ public function testLoad(): void ], ]); - (new ElasticSearchFixture3())->load($this->client, 'my_index'); + (new ElasticsearchFixture3())->load($this->client, 'my_index'); } protected function setUp(): void diff --git a/tests/Adapter/ElasticSearchJsonDirectoryFixtureTest.php b/tests/Adapter/ElasticsearchJsonDirectoryFixtureTest.php similarity index 54% rename from tests/Adapter/ElasticSearchJsonDirectoryFixtureTest.php rename to tests/Adapter/ElasticsearchJsonDirectoryFixtureTest.php index 918e104..dda4adb 100644 --- a/tests/Adapter/ElasticSearchJsonDirectoryFixtureTest.php +++ b/tests/Adapter/ElasticsearchJsonDirectoryFixtureTest.php @@ -5,16 +5,17 @@ use Elasticsearch\Client; use InvalidArgumentException; -use Kununu\DataFixtures\Tests\TestFixtures\ElasticSearchJsonDirectoryFixture1; -use Kununu\DataFixtures\Tests\TestFixtures\ElasticSearchJsonDirectoryFixture2; +use Kununu\DataFixtures\Exception\LoadFailedException; +use Kununu\DataFixtures\Tests\TestFixtures\ElasticsearchJsonDirectoryFixture1; +use Kununu\DataFixtures\Tests\TestFixtures\ElasticsearchJsonDirectoryFixture2; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -final class ElasticSearchJsonDirectoryFixtureTest extends TestCase +final class ElasticsearchJsonDirectoryFixtureTest extends TestCase { private MockObject|Client $client; - public function testLoad(): void + public function testLoadSuccess(): void { $bulk1 = [ 'body' => [ @@ -77,9 +78,106 @@ public function testLoad(): void $this->callback( fn(array $bulk): bool => ($bulk == $bulk1 || $bulk == $bulk2) ) - ); + ) + ->willReturn(['errors' => false]); - (new ElasticSearchJsonDirectoryFixture1())->load($this->client, 'my_index'); + (new ElasticsearchJsonDirectoryFixture1())->load($this->client, 'my_index'); + } + + public function testLoadWithErrors(): void + { + $bulk1 = [ + 'body' => [ + [ + 'index' => [ + '_index' => 'my_index', + '_id' => '17e05f79-2c6e-4a71-bacf-afc8fd8e5f73', + ], + ], + [ + 'uuid' => '17e05f79-2c6e-4a71-bacf-afc8fd8e5f73', + 'name' => 'Document 1', + 'attributes' => [ + 'attrib_1' => 1, + 'attrib_2' => 'active', + 'attrib_3' => true, + ], + ], + [ + 'index' => [ + '_index' => 'my_index', + '_id' => 'e3d49639-a8f7-4b21-96d7-e4a22e87e1da', + ], + ], + [ + 'uuid' => 'e3d49639-a8f7-4b21-96d7-e4a22e87e1da', + 'name' => 'Document 2', + 'attributes' => [ + 'attrib_1' => 2, + 'attrib_2' => 'inactive', + 'attrib_3' => false, + ], + ], + ], + ]; + $bulk2 = [ + 'body' => [ + [ + 'index' => [ + '_index' => 'my_index', + '_id' => 'd1cd10a0-7023-434c-8cd3-0196e1d34c2f', + ], + ], + [ + 'uuid' => 'd1cd10a0-7023-434c-8cd3-0196e1d34c2f', + 'name' => 'Document 3', + 'attributes' => [ + 'attrib_1' => 3, + 'attrib_2' => 'inactive', + 'attrib_3' => false, + ], + ], + ], + ]; + + $this->client + ->expects($this->once()) + ->method('bulk') + ->with( + $this->callback( + fn(array $bulk): bool => $bulk == $bulk1 + ) + ) + ->willReturn([ + 'errors' => true, + 'items' => [ + [ + 'update' => [ + 'error' => 'some error', + '_index' => 'my_index', + '_id' => '17e05f79-2c6e-4a71-bacf-afc8fd8e5f73', + 'status' => 'some status', + ], + ], + ], + ]); + + $this->expectException(LoadFailedException::class); + $this->expectExceptionMessage( + <<<'TEXT' +Errors: +[ + { + "index": "my_index", + "id": "17e05f79-2c6e-4a71-bacf-afc8fd8e5f73", + "status": "some status", + "error": "some error" + } +] +TEXT + ); + + (new ElasticsearchJsonDirectoryFixture1())->load($this->client, 'my_index'); } public function testLoadWithInvalidJson(): void @@ -120,17 +218,18 @@ public function testLoadWithInvalidJson(): void ], ], ], - ]); + ]) + ->willReturn(['errors' => false]); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( sprintf( 'Error decoding JSON file: "%s"', - realpath(__DIR__ . '/../TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs2.json') + realpath(__DIR__ . '/../TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs2.json') ) ); - (new ElasticSearchJsonDirectoryFixture2())->load($this->client, 'my_index'); + (new ElasticsearchJsonDirectoryFixture2())->load($this->client, 'my_index'); } protected function setUp(): void diff --git a/tests/Executor/CachePoolExecutorTest.php b/tests/Executor/CachePoolExecutorTest.php index a249c33..ac3469d 100644 --- a/tests/Executor/CachePoolExecutorTest.php +++ b/tests/Executor/CachePoolExecutorTest.php @@ -34,10 +34,16 @@ public function testThatPurgesWhenAppendIsDisabled(): void public function testThatFixturesAreLoaded(): void { $fixture1 = $this->createMock(CachePoolFixtureInterface::class); - $fixture1->expects($this->once())->method('load')->with($this->cache); + $fixture1 + ->expects($this->once()) + ->method('load') + ->with($this->cache); $fixture2 = $this->createMock(CachePoolFixtureInterface::class); - $fixture2->expects($this->once())->method('load')->with($this->cache); + $fixture2 + ->expects($this->once()) + ->method('load') + ->with($this->cache); $this->executor->execute([$fixture1, $fixture2]); } diff --git a/tests/Executor/ConnectionExecutorTest.php b/tests/Executor/ConnectionExecutorTest.php index 688aabd..3ad216a 100644 --- a/tests/Executor/ConnectionExecutorTest.php +++ b/tests/Executor/ConnectionExecutorTest.php @@ -9,14 +9,11 @@ use Kununu\DataFixtures\Adapter\ConnectionFixtureInterface; use Kununu\DataFixtures\Executor\ConnectionExecutor; use Kununu\DataFixtures\Executor\ExecutorInterface; -use Kununu\DataFixtures\Tests\Utils\ConnectionUtilsTrait; use LogicException; use PHPUnit\Framework\MockObject\MockObject; final class ConnectionExecutorTest extends AbstractExecutorTestCase { - use ConnectionUtilsTrait; - private const SQL_1 = 'SET FOREIGN_KEY_CHECKS=0'; private const SQL_2 = 'SET FOREIGN_KEY_CHECKS=1'; @@ -26,11 +23,12 @@ public function testThatExecutorIsTransactionalAndCommits(): void { $this->connection ->expects($this->exactly(2)) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturnCallback( fn(string $sql): int => match ($sql) { - self::SQL_1, self::SQL_2 => 1, - default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) + self::SQL_1, + self::SQL_2 => 1, + default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) } ); @@ -55,11 +53,12 @@ public function testThatExecutorIsTransactionalAndRollbacks(): void $this->connection ->expects($this->exactly(2)) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturnCallback( fn(string $sql): int => match ($sql) { - self::SQL_1, self::SQL_2 => 1, - default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) + self::SQL_1, + self::SQL_2 => 1, + default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) } ); @@ -87,7 +86,7 @@ public function testThatDoesNotPurgesWhenAppendIsEnabled(): void { $this->connection ->expects($this->any()) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturn(1); $this->purger @@ -101,7 +100,7 @@ public function testThatPurgesWhenAppendIsDisabled(): void { $this->connection ->expects($this->any()) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturn(1); $this->purger @@ -115,7 +114,7 @@ public function testThatFixturesAreLoaded(): void { $this->connection ->expects($this->any()) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturn(1); $fixture1 = $this->createMock(ConnectionFixtureInterface::class); @@ -139,7 +138,7 @@ protected function setUp(): void $this->connection ->expects($this->any()) ->method('getDriver') - ->willReturn($this->getMockForAbstractClass(AbstractMySQLDriver::class)); + ->willReturn($this->createMock(AbstractMySQLDriver::class)); parent::setUp(); } diff --git a/tests/Executor/ElasticSearchExecutorTest.php b/tests/Executor/ElasticsearchExecutorTest.php similarity index 82% rename from tests/Executor/ElasticSearchExecutorTest.php rename to tests/Executor/ElasticsearchExecutorTest.php index 638efcf..3ba9efe 100644 --- a/tests/Executor/ElasticSearchExecutorTest.php +++ b/tests/Executor/ElasticsearchExecutorTest.php @@ -5,12 +5,12 @@ use Elasticsearch\Client; use Elasticsearch\Namespaces\IndicesNamespace; -use Kununu\DataFixtures\Adapter\ElasticSearchFixtureInterface; -use Kununu\DataFixtures\Executor\ElasticSearchExecutor; +use Kununu\DataFixtures\Adapter\ElasticsearchFixtureInterface; +use Kununu\DataFixtures\Executor\ElasticsearchExecutor; use Kununu\DataFixtures\Executor\ExecutorInterface; use PHPUnit\Framework\MockObject\MockObject; -final class ElasticSearchExecutorTest extends AbstractExecutorTestCase +final class ElasticsearchExecutorTest extends AbstractExecutorTestCase { private const INDEX_NAME = 'my_index'; @@ -36,13 +36,13 @@ public function testThatPurgesWhenAppendIsDisabled(): void public function testThatFixturesAreLoaded(): void { - $fixture1 = $this->createMock(ElasticSearchFixtureInterface::class); + $fixture1 = $this->createMock(ElasticsearchFixtureInterface::class); $fixture1 ->expects($this->once()) ->method('load') ->with($this->client, self::INDEX_NAME); - $fixture2 = $this->createMock(ElasticSearchFixtureInterface::class); + $fixture2 = $this->createMock(ElasticsearchFixtureInterface::class); $fixture2 ->expects($this->once()) ->method('load') @@ -71,6 +71,6 @@ protected function setUp(): void protected function getExecutor(): ExecutorInterface { - return new ElasticSearchExecutor($this->client, self::INDEX_NAME, $this->purger); + return new ElasticsearchExecutor($this->client, self::INDEX_NAME, $this->purger); } } diff --git a/tests/Executor/NonTransactionalConnectionExecutorTest.php b/tests/Executor/NonTransactionalConnectionExecutorTest.php index 7ebe497..24f1bf8 100644 --- a/tests/Executor/NonTransactionalConnectionExecutorTest.php +++ b/tests/Executor/NonTransactionalConnectionExecutorTest.php @@ -9,14 +9,11 @@ use Kununu\DataFixtures\Adapter\ConnectionFixtureInterface; use Kununu\DataFixtures\Executor\ExecutorInterface; use Kununu\DataFixtures\Executor\NonTransactionalConnectionExecutor; -use Kununu\DataFixtures\Tests\Utils\ConnectionUtilsTrait; use LogicException; use PHPUnit\Framework\MockObject\MockObject; final class NonTransactionalConnectionExecutorTest extends AbstractExecutorTestCase { - use ConnectionUtilsTrait; - private const SQL_1 = 'SET FOREIGN_KEY_CHECKS=0'; private const SQL_2 = 'SET FOREIGN_KEY_CHECKS=1'; @@ -26,11 +23,12 @@ public function testThatExecutorIsNotTransactionalAndLoadsFixture(): void { $this->connection ->expects($this->exactly(2)) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturnCallback( fn(string $sql): int => match ($sql) { - self::SQL_1, self::SQL_2 => 1, - default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) + self::SQL_1, + self::SQL_2 => 1, + default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) } ); @@ -61,11 +59,12 @@ public function testThatExecutorIsNotTransactionalAndDoesNotRollbacks(): void $this->connection ->expects($this->exactly(2)) - ->method($this->getExecuteQueryMethodName($this->connection)) + ->method('executeStatement') ->willReturnCallback( fn(string $sql): int => match ($sql) { - self::SQL_1, self::SQL_2 => 1, - default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) + self::SQL_1, + self::SQL_2 => 1, + default => throw new LogicException(sprintf('Unknown SQL "%s"', $sql)) } ); @@ -101,7 +100,7 @@ protected function setUp(): void $this->connection ->expects($this->any()) ->method('getDriver') - ->willReturn($this->getMockForAbstractClass(AbstractMySQLDriver::class)); + ->willReturn($this->createMock(AbstractMySQLDriver::class)); parent::setUp(); } diff --git a/tests/Loader/ElasticSearchFixturesLoaderTest.php b/tests/Loader/ElasticSearchFixturesLoaderTest.php deleted file mode 100644 index f7ac2b4..0000000 --- a/tests/Loader/ElasticSearchFixturesLoaderTest.php +++ /dev/null @@ -1,36 +0,0 @@ - 'ElasticSearchFixture1.php', - ElasticSearchFixture2::class => 'ElasticSearchFixture2.php', - ]; - } -} diff --git a/tests/Loader/ElasticsearchFixturesLoaderTest.php b/tests/Loader/ElasticsearchFixturesLoaderTest.php new file mode 100644 index 0000000..cc4aefc --- /dev/null +++ b/tests/Loader/ElasticsearchFixturesLoaderTest.php @@ -0,0 +1,36 @@ + 'ElasticsearchFixture1.php', + ElasticsearchFixture2::class => 'ElasticsearchFixture2.php', + ]; + } +} diff --git a/tests/Purger/AbstractConnectionPurgerTestCase.php b/tests/Purger/AbstractConnectionPurgerTestCase.php index 0e74032..eb44ed2 100644 --- a/tests/Purger/AbstractConnectionPurgerTestCase.php +++ b/tests/Purger/AbstractConnectionPurgerTestCase.php @@ -7,16 +7,18 @@ use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; -use Kununu\DataFixtures\Tests\Utils\ConnectionUtilsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; abstract class AbstractConnectionPurgerTestCase extends TestCase { - use ConnectionUtilsTrait; - - protected const TABLES = ['table_1', 'table_2', 'table_3']; - protected const EXCLUDED_TABLES = ['table_4', 'table_2', 'table_5']; + protected const TABLE_1 = 'table_1'; + protected const TABLE_2 = 'table_2'; + protected const TABLE_3 = 'table_3'; + protected const TABLE_4 = 'table_4'; + protected const TABLE_5 = 'table_5'; + protected const TABLES = [self::TABLE_1, self::TABLE_2, self::TABLE_3]; + protected const EXCLUDED_TABLES = [self::TABLE_4, self::TABLE_2, self::TABLE_5]; protected function getConnectionMock(bool $withPlatform = true, array $tables = self::TABLES): MockObject|Connection { @@ -28,16 +30,15 @@ protected function getConnectionMock(bool $withPlatform = true, array $tables = ->method('listTableNames') ->willReturn($tables); - // To support doctrine/dbal ^2.9 and ^3.1 $connection ->expects($this->any()) - ->method(method_exists($connection, 'createSchemaManager') ? 'createSchemaManager' : 'getSchemaManager') + ->method('createSchemaManager') ->willReturn($schemaManager); $connection ->expects($this->any()) ->method('getDriver') - ->willReturn($this->getMockForAbstractClass(AbstractMySQLDriver::class)); + ->willReturn($this->createMock(AbstractMySQLDriver::class)); $connection ->expects($this->any()) @@ -57,47 +58,55 @@ protected function getConnectionMock(bool $withPlatform = true, array $tables = protected function getConsecutiveArgumentsForConnectionExecStatement( ?int $purgeMode = 1, ?array $tables = self::TABLES, - ?array $excludedTables = [] + ?array $excludedTables = [], + bool $itemsAsArray = true ): array { $purgeStatements = match ($purgeMode) { // PURGE_MODE_DELETE - 1 => $this->getDeleteModeConnectionWithConsecutiveArguments($tables, $excludedTables), + 1 => $this->getDeleteModeConsecutiveArguments($tables, $excludedTables, $itemsAsArray), // PURGE_MODE_TRUNCATE - 2 => $this->getTruncateModeConnectionWithConsecutiveArguments($tables, $excludedTables), + 2 => $this->getTruncateModeConsecutiveArguments($tables, $excludedTables, $itemsAsArray), default => [] }; + $disableForeignKeyChecks = 'SET FOREIGN_KEY_CHECKS=0'; + $enableForeignKeyChecks = 'SET FOREIGN_KEY_CHECKS=1'; + return array_merge( - [['SET FOREIGN_KEY_CHECKS=0']], + [$itemsAsArray ? [$disableForeignKeyChecks] : $disableForeignKeyChecks], $purgeStatements, - [['SET FOREIGN_KEY_CHECKS=1']] + [$itemsAsArray ? [$enableForeignKeyChecks] : $enableForeignKeyChecks] ); } - protected function getDeleteModeConnectionWithConsecutiveArguments( + protected function getDeleteModeConsecutiveArguments( array $tables = self::TABLES, - array $excludedTables = [] + array $excludedTables = [], + bool $itemsAsArray = true ): array { $return = []; foreach ($tables as $tableName) { if (!in_array($tableName, $excludedTables)) { - $return[] = [sprintf('DELETE FROM `%s`', $tableName)]; + $statement = sprintf('DELETE FROM `%s`', $tableName); + $return[] = $itemsAsArray ? [$statement] : $statement; } } return $return; } - protected function getTruncateModeConnectionWithConsecutiveArguments( + protected function getTruncateModeConsecutiveArguments( array $tables = self::TABLES, - array $excludedTables = [] + array $excludedTables = [], + bool $itemsAsArray = true ): array { $return = []; foreach ($tables as $tableName) { if (!in_array($tableName, $excludedTables)) { - $return[] = [sprintf('TRUNCATE `%s`', $tableName)]; + $statement = sprintf('TRUNCATE `%s`', $tableName); + $return[] = $itemsAsArray ? [$statement] : $statement; } } diff --git a/tests/Purger/CachePoolPurgerTest.php b/tests/Purger/CachePoolPurgerTest.php index 5b76e21..89cc757 100644 --- a/tests/Purger/CachePoolPurgerTest.php +++ b/tests/Purger/CachePoolPurgerTest.php @@ -38,6 +38,7 @@ public function testThatWhenCacheItemPoolFailsToPurgeThenAnExceptionIsThrown(): protected function setUp(): void { $this->cache = $this->createMock(CacheItemPoolInterface::class); + parent::setUp(); } diff --git a/tests/Purger/ConnectionPurgerTest.php b/tests/Purger/ConnectionPurgerTest.php index a7f6c7c..ee36b14 100644 --- a/tests/Purger/ConnectionPurgerTest.php +++ b/tests/Purger/ConnectionPurgerTest.php @@ -7,7 +7,6 @@ use Exception; use Kununu\DataFixtures\Exception\InvalidConnectionPurgeModeException; use Kununu\DataFixtures\Purger\ConnectionPurger; -use Kununu\DataFixtures\Purger\PurgerInterface; final class ConnectionPurgerTest extends AbstractConnectionPurgerTestCase { @@ -17,8 +16,15 @@ public function testThatPurgerIsTransactionalAndCommits(): void $connection ->expects($this->exactly(5)) - ->method($this->getExecuteQueryMethodName($connection)) - ->withConsecutive(...$this->getConsecutiveArgumentsForConnectionExecStatement()) + ->method('executeStatement') + ->with( + $this->callback( + fn(string $statement): bool => in_array( + $statement, + $this->getConsecutiveArgumentsForConnectionExecStatement(itemsAsArray: false) + ) + ) + ) ->willReturn(1); $transactionStarted = false; @@ -48,8 +54,15 @@ public function testThatPurgerIsTransactionalAndRollbacks(): void $connection ->expects($this->exactly(5)) - ->method($this->getExecuteQueryMethodName($connection)) - ->withConsecutive(...$this->getConsecutiveArgumentsForConnectionExecStatement()) + ->method('executeStatement') + ->with( + $this->callback( + fn(string $statement): bool => in_array( + $statement, + $this->getConsecutiveArgumentsForConnectionExecStatement(itemsAsArray: false) + ) + ) + ) ->willReturn(1); $transactionStarted = false; @@ -82,7 +95,7 @@ public function testThatWhenNoTablesAreProvidedNothingIsPurged(): void $connection ->expects($this->never()) - ->method($this->getExecuteQueryMethodName($connection)); + ->method('executeStatement'); $purger = new ConnectionPurger($connection); $purger->purge(); @@ -94,16 +107,21 @@ public function testThatExcludedTablesAreNotPurged(): void $connection ->expects($this->exactly(4)) - ->method($this->getExecuteQueryMethodName($connection)) - ->withConsecutive( - ...$this->getConsecutiveArgumentsForConnectionExecStatement(1, self::TABLES, self::EXCLUDED_TABLES) + ->method('executeStatement') + ->with( + $this->callback( + fn(string $statement): bool => in_array( + $statement, + $this->getConsecutiveArgumentsForConnectionExecStatement( + excludedTables: self::EXCLUDED_TABLES, + itemsAsArray: false + ) + ) + ) ) ->willReturn(1); - $purger = new ConnectionPurger( - $connection, - self::EXCLUDED_TABLES - ); + $purger = new ConnectionPurger($connection, self::EXCLUDED_TABLES); $purger->purge(); } @@ -114,8 +132,15 @@ public function testThatPurgesWithDeleteMode(): void $connection ->expects($this->exactly(5)) - ->method($this->getExecuteQueryMethodName($connection)) - ->withConsecutive(...$this->getConsecutiveArgumentsForConnectionExecStatement()) + ->method('executeStatement') + ->with( + $this->callback( + fn(string $statement): bool => in_array( + $statement, + $this->getConsecutiveArgumentsForConnectionExecStatement(itemsAsArray: false) + ) + ) + ) ->willReturn(1); $purger = new ConnectionPurger($connection); @@ -132,13 +157,13 @@ public function testThatPurgesWithTruncateMode(): void $platform ->expects($this->exactly(3)) ->method('getTruncateTableSQL') - ->withConsecutive( - ['table_1', true], - ['table_2', true], - ['table_3', true] + ->with( + $this->callback( + fn(string $table): bool => in_array($table, [self::TABLE_1, self::TABLE_2, self::TABLE_3]) + ) ) ->willReturnOnConsecutiveCalls( - ...array_map(fn($element) => $element[0], $this->getTruncateModeConnectionWithConsecutiveArguments()) + ...array_map(fn($element) => $element[0], $this->getTruncateModeConsecutiveArguments()) ); $connection @@ -148,8 +173,15 @@ public function testThatPurgesWithTruncateMode(): void $connection ->expects($this->exactly(5)) - ->method($this->getExecuteQueryMethodName($connection)) - ->withConsecutive(...$this->getConsecutiveArgumentsForConnectionExecStatement(2)) + ->method('executeStatement') + ->with( + $this->callback( + fn(string $statement): bool => in_array( + $statement, + $this->getConsecutiveArgumentsForConnectionExecStatement(2, itemsAsArray: false) + ) + ) + ) ->willReturn(1); $purger = new ConnectionPurger($connection); @@ -183,9 +215,4 @@ public function testChangePurgeModeToNotSupportedModeThrowsException(): void $purger = new ConnectionPurger($this->getConnectionMock()); $purger->setPurgeMode(10); } - - protected function getPurger(): PurgerInterface - { - // TODO: Implement getPurger() method. - } } diff --git a/tests/Purger/ElasticSearchPurgerTest.php b/tests/Purger/ElasticsearchPurgerTest.php similarity index 88% rename from tests/Purger/ElasticSearchPurgerTest.php rename to tests/Purger/ElasticsearchPurgerTest.php index dd5c805..ce1130e 100644 --- a/tests/Purger/ElasticSearchPurgerTest.php +++ b/tests/Purger/ElasticsearchPurgerTest.php @@ -5,12 +5,12 @@ use Elasticsearch\Client; use Elasticsearch\Namespaces\IndicesNamespace; -use Kununu\DataFixtures\Purger\ElasticSearchPurger; +use Kununu\DataFixtures\Purger\ElasticsearchPurger; use Kununu\DataFixtures\Purger\PurgerInterface; use PHPUnit\Framework\MockObject\MockObject; use stdClass; -final class ElasticSearchPurgerTest extends AbstractPurgerTestCase +final class ElasticsearchPurgerTest extends AbstractPurgerTestCase { private MockObject|Client $client; @@ -49,11 +49,12 @@ public function testPurge(): void protected function setUp(): void { $this->client = $this->createMock(Client::class); + parent::setUp(); } protected function getPurger(): PurgerInterface { - return new ElasticSearchPurger($this->client, 'my_index'); + return new ElasticsearchPurger($this->client, 'my_index'); } } diff --git a/tests/Purger/HttpClientPurgerTest.php b/tests/Purger/HttpClientPurgerTest.php index 2229f3d..21b776c 100644 --- a/tests/Purger/HttpClientPurgerTest.php +++ b/tests/Purger/HttpClientPurgerTest.php @@ -35,6 +35,7 @@ public function testPurgeWithNoFixturesHttpClient(): void protected function setUp(): void { $this->httpClient = $this->createMock(FixturesHttpClientInterface::class); + parent::setUp(); } diff --git a/tests/Purger/NonTransactionalConnectionPurgerTest.php b/tests/Purger/NonTransactionalConnectionPurgerTest.php index 4dcc9fe..1827d10 100644 --- a/tests/Purger/NonTransactionalConnectionPurgerTest.php +++ b/tests/Purger/NonTransactionalConnectionPurgerTest.php @@ -14,8 +14,15 @@ public function testThatPurgerIsNotTransactionalAndCommits(): void $connection ->expects($this->exactly(5)) - ->method($this->getExecuteQueryMethodName($connection)) - ->withConsecutive(...$this->getConsecutiveArgumentsForConnectionExecStatement()) + ->method('executeStatement') + ->with( + $this->callback( + fn(string $statement): bool => in_array( + $statement, + $this->getConsecutiveArgumentsForConnectionExecStatement(itemsAsArray: false) + ) + ) + ) ->willReturn(1); $connection @@ -38,12 +45,24 @@ public function testThatPurgerIsTransactionalAndRollbacks(): void $connection ->expects($this->exactly(3)) - ->method($this->getExecuteQueryMethodName($connection)) - ->withConsecutive(...$this->getConsecutiveArgumentsForConnectionExecStatement(1, ['table_1'])) - ->willReturnCallback(fn(string $sql): int => match (true) { - 'DELETE FROM `table_1`' === $sql => throw new Exception(), - default => 1 - }); + ->method('executeStatement') + ->with( + $this->callback( + fn(string $statement): bool => in_array( + $statement, + $this->getConsecutiveArgumentsForConnectionExecStatement( + tables: [self::TABLE_1], + itemsAsArray: false + ) + ) + ) + ) + ->willReturnCallback( + fn(string $sql): int => match (true) { + 'DELETE FROM `table_1`' === $sql => throw new Exception(), + default => 1 + } + ); $connection ->expects($this->never()) diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs1.json b/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs1.json deleted file mode 100644 index 7ee1d56..0000000 --- a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs1.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - { - "uuid": "17e05f79-2c6e-4a71-bacf-afc8fd8e5f73", - "name": "Document 1", - "attributes": { - "attrib_1": 1, - "attrib_2": "active", - "attrib_3": true - } - }, - { - "uuid": "e3d49639-a8f7-4b21-96d7-e4a22e87e1da", - "name": "Document 2", - "attributes": { - "attrib_1": 2, - "attrib_2": "inactive", - "attrib_3": false - } - } -] diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs2.json b/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs2.json deleted file mode 100644 index 1c75c92..0000000 --- a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs2.json +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - "uuid": "d1cd10a0-7023-434c-8cd3-0196e1d34c2f", - "name": "Document 3", - "attributes": { - "attrib_1": 3, - "attrib_2": "inactive", - "attrib_3": false - } - } -] diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs1.json b/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs1.json deleted file mode 100644 index 7ee1d56..0000000 --- a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs1.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - { - "uuid": "17e05f79-2c6e-4a71-bacf-afc8fd8e5f73", - "name": "Document 1", - "attributes": { - "attrib_1": 1, - "attrib_2": "active", - "attrib_3": true - } - }, - { - "uuid": "e3d49639-a8f7-4b21-96d7-e4a22e87e1da", - "name": "Document 2", - "attributes": { - "attrib_1": 2, - "attrib_2": "inactive", - "attrib_3": false - } - } -] diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs3.json b/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs3.json deleted file mode 100644 index 1c75c92..0000000 --- a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs3.json +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - "uuid": "d1cd10a0-7023-434c-8cd3-0196e1d34c2f", - "name": "Document 3", - "attributes": { - "attrib_1": 3, - "attrib_2": "inactive", - "attrib_3": false - } - } -] diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchArrayDirectoryFixture1/docs1.php b/tests/TestFixtures/Elasticsearch/ElasticsearchArrayDirectoryFixture1/docs1.php similarity index 100% rename from tests/TestFixtures/Elasticsearch/ElasticSearchArrayDirectoryFixture1/docs1.php rename to tests/TestFixtures/Elasticsearch/ElasticsearchArrayDirectoryFixture1/docs1.php diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchArrayDirectoryFixture1/docs2.php b/tests/TestFixtures/Elasticsearch/ElasticsearchArrayDirectoryFixture1/docs2.php similarity index 100% rename from tests/TestFixtures/Elasticsearch/ElasticSearchArrayDirectoryFixture1/docs2.php rename to tests/TestFixtures/Elasticsearch/ElasticsearchArrayDirectoryFixture1/docs2.php diff --git a/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs1.json b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs1.json new file mode 100644 index 0000000..e974e7f --- /dev/null +++ b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs1.json @@ -0,0 +1,20 @@ +[ + { + "uuid": "17e05f79-2c6e-4a71-bacf-afc8fd8e5f73", + "name": "Document 1", + "attributes": { + "attrib_1": 1, + "attrib_2": "active", + "attrib_3": true + } + }, + { + "uuid": "e3d49639-a8f7-4b21-96d7-e4a22e87e1da", + "name": "Document 2", + "attributes": { + "attrib_1": 2, + "attrib_2": "inactive", + "attrib_3": false + } + } +] diff --git a/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs2.json b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs2.json new file mode 100644 index 0000000..4526f2a --- /dev/null +++ b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs2.json @@ -0,0 +1,11 @@ +[ + { + "uuid": "d1cd10a0-7023-434c-8cd3-0196e1d34c2f", + "name": "Document 3", + "attributes": { + "attrib_1": 3, + "attrib_2": "inactive", + "attrib_3": false + } + } +] diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs3.json b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs3.json similarity index 100% rename from tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture1/docs3.json rename to tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture1/docs3.json diff --git a/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs1.json b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs1.json new file mode 100644 index 0000000..e974e7f --- /dev/null +++ b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs1.json @@ -0,0 +1,20 @@ +[ + { + "uuid": "17e05f79-2c6e-4a71-bacf-afc8fd8e5f73", + "name": "Document 1", + "attributes": { + "attrib_1": 1, + "attrib_2": "active", + "attrib_3": true + } + }, + { + "uuid": "e3d49639-a8f7-4b21-96d7-e4a22e87e1da", + "name": "Document 2", + "attributes": { + "attrib_1": 2, + "attrib_2": "inactive", + "attrib_3": false + } + } +] diff --git a/tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs2.json b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs2.json similarity index 100% rename from tests/TestFixtures/Elasticsearch/ElasticSearchJsonDirectoryFixture2/docs2.json rename to tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs2.json diff --git a/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs3.json b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs3.json new file mode 100644 index 0000000..4526f2a --- /dev/null +++ b/tests/TestFixtures/Elasticsearch/ElasticsearchJsonDirectoryFixture2/docs3.json @@ -0,0 +1,11 @@ +[ + { + "uuid": "d1cd10a0-7023-434c-8cd3-0196e1d34c2f", + "name": "Document 3", + "attributes": { + "attrib_1": 3, + "attrib_2": "inactive", + "attrib_3": false + } + } +] diff --git a/tests/TestFixtures/ElasticSearchArrayDirectoryFixture1.php b/tests/TestFixtures/ElasticsearchArrayDirectoryFixture1.php similarity index 72% rename from tests/TestFixtures/ElasticSearchArrayDirectoryFixture1.php rename to tests/TestFixtures/ElasticsearchArrayDirectoryFixture1.php index a8a55f3..47a1ffe 100644 --- a/tests/TestFixtures/ElasticSearchArrayDirectoryFixture1.php +++ b/tests/TestFixtures/ElasticsearchArrayDirectoryFixture1.php @@ -3,9 +3,9 @@ namespace Kununu\DataFixtures\Tests\TestFixtures; -use Kununu\DataFixtures\Adapter\DirectoryLoader\ElasticSearchArrayDirectoryFixture; +use Kununu\DataFixtures\Adapter\DirectoryLoader\ElasticsearchArrayDirectoryFixture; -final class ElasticSearchArrayDirectoryFixture1 extends ElasticSearchArrayDirectoryFixture +final class ElasticsearchArrayDirectoryFixture1 extends ElasticsearchArrayDirectoryFixture { protected function getDocumentIdForBulkIndexation(array $document): mixed { diff --git a/tests/TestFixtures/ElasticSearchFixture1.php b/tests/TestFixtures/ElasticsearchFixture1.php similarity index 55% rename from tests/TestFixtures/ElasticSearchFixture1.php rename to tests/TestFixtures/ElasticsearchFixture1.php index 65579da..9a2a341 100644 --- a/tests/TestFixtures/ElasticSearchFixture1.php +++ b/tests/TestFixtures/ElasticsearchFixture1.php @@ -4,11 +4,11 @@ namespace Kununu\DataFixtures\Tests\TestFixtures; use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFixtureInterface; +use Kununu\DataFixtures\Adapter\ElasticsearchFixtureInterface; -final class ElasticSearchFixture1 implements ElasticSearchFixtureInterface +final class ElasticsearchFixture1 implements ElasticsearchFixtureInterface { - public function load(Client $elasticSearch, string $indexName): void + public function load(Client $elasticSearch, string $indexName, bool $throwOnFail = true): void { } } diff --git a/tests/TestFixtures/ElasticSearchFixture2.php b/tests/TestFixtures/ElasticsearchFixture2.php similarity index 55% rename from tests/TestFixtures/ElasticSearchFixture2.php rename to tests/TestFixtures/ElasticsearchFixture2.php index 3c1426d..6655f29 100644 --- a/tests/TestFixtures/ElasticSearchFixture2.php +++ b/tests/TestFixtures/ElasticsearchFixture2.php @@ -4,11 +4,11 @@ namespace Kununu\DataFixtures\Tests\TestFixtures; use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFixtureInterface; +use Kununu\DataFixtures\Adapter\ElasticsearchFixtureInterface; -final class ElasticSearchFixture2 implements ElasticSearchFixtureInterface +final class ElasticsearchFixture2 implements ElasticsearchFixtureInterface { - public function load(Client $elasticSearch, string $indexName): void + public function load(Client $elasticSearch, string $indexName, bool $throwOnFail = true): void { } } diff --git a/tests/TestFixtures/ElasticSearchFixture3.php b/tests/TestFixtures/ElasticsearchFixture3.php similarity index 72% rename from tests/TestFixtures/ElasticSearchFixture3.php rename to tests/TestFixtures/ElasticsearchFixture3.php index 7934d48..c4177b9 100644 --- a/tests/TestFixtures/ElasticSearchFixture3.php +++ b/tests/TestFixtures/ElasticsearchFixture3.php @@ -4,9 +4,9 @@ namespace Kununu\DataFixtures\Tests\TestFixtures; use Elasticsearch\Client; -use Kununu\DataFixtures\Adapter\ElasticSearchFixture; +use Kununu\DataFixtures\Adapter\ElasticsearchFixture; -final class ElasticSearchFixture3 extends ElasticSearchFixture +final class ElasticsearchFixture3 extends ElasticsearchFixture { public const DOCUMENTS = [ [ @@ -29,14 +29,12 @@ final class ElasticSearchFixture3 extends ElasticSearchFixture ], ]; - public function load(Client $elasticSearch, string $indexName): void + public function load(Client $elasticSearch, string $indexName, bool $throwOnFail = true): void { - $elasticSearch->bulk( - [ - 'type' => '_doc', - 'body' => $this->prepareBodyForBulkIndexation($indexName, self::DOCUMENTS), - ] - ); + $elasticSearch->bulk([ + 'type' => '_doc', + 'body' => $this->prepareBodyForBulkIndexation($indexName, self::DOCUMENTS), + ]); } protected function getDocumentIdForBulkIndexation(array $document): mixed diff --git a/tests/TestFixtures/ElasticSearchJsonDirectoryFixture2.php b/tests/TestFixtures/ElasticsearchJsonDirectoryFixture1.php similarity index 66% rename from tests/TestFixtures/ElasticSearchJsonDirectoryFixture2.php rename to tests/TestFixtures/ElasticsearchJsonDirectoryFixture1.php index 94bac44..13df4d6 100644 --- a/tests/TestFixtures/ElasticSearchJsonDirectoryFixture2.php +++ b/tests/TestFixtures/ElasticsearchJsonDirectoryFixture1.php @@ -3,9 +3,9 @@ namespace Kununu\DataFixtures\Tests\TestFixtures; -use Kununu\DataFixtures\Adapter\DirectoryLoader\ElasticSearchJsonDirectoryFixture; +use Kununu\DataFixtures\Adapter\DirectoryLoader\ElasticsearchJsonDirectoryFixture; -final class ElasticSearchJsonDirectoryFixture2 extends ElasticSearchJsonDirectoryFixture +final class ElasticsearchJsonDirectoryFixture1 extends ElasticsearchJsonDirectoryFixture { protected function getDocumentIdForBulkIndexation(array $document): mixed { diff --git a/tests/TestFixtures/ElasticSearchJsonDirectoryFixture1.php b/tests/TestFixtures/ElasticsearchJsonDirectoryFixture2.php similarity index 66% rename from tests/TestFixtures/ElasticSearchJsonDirectoryFixture1.php rename to tests/TestFixtures/ElasticsearchJsonDirectoryFixture2.php index a45b053..b05cc0e 100644 --- a/tests/TestFixtures/ElasticSearchJsonDirectoryFixture1.php +++ b/tests/TestFixtures/ElasticsearchJsonDirectoryFixture2.php @@ -3,9 +3,9 @@ namespace Kununu\DataFixtures\Tests\TestFixtures; -use Kununu\DataFixtures\Adapter\DirectoryLoader\ElasticSearchJsonDirectoryFixture; +use Kununu\DataFixtures\Adapter\DirectoryLoader\ElasticsearchJsonDirectoryFixture; -final class ElasticSearchJsonDirectoryFixture1 extends ElasticSearchJsonDirectoryFixture +final class ElasticsearchJsonDirectoryFixture2 extends ElasticsearchJsonDirectoryFixture { protected function getDocumentIdForBulkIndexation(array $document): mixed { diff --git a/tests/Tools/ConnectionToolsTest.php b/tests/Tools/ConnectionToolsTest.php index 5d1609f..5257a61 100644 --- a/tests/Tools/ConnectionToolsTest.php +++ b/tests/Tools/ConnectionToolsTest.php @@ -7,7 +7,10 @@ use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractSQLiteDriver; use Doctrine\DBAL\Logging\Middleware; +use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; +use Doctrine\DBAL\Platforms\SqlitePlatform; use Kununu\DataFixtures\Tools\ConnectionToolsTrait; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; @@ -16,7 +19,7 @@ final class ConnectionToolsTest extends TestCase { use ConnectionToolsTrait; - /** @dataProvider providerMySQLDrivers */ + #[DataProvider('mysqlDataProvider')] public function testGetDisableForeignKeyChecksForMySQL(Driver|string $driver): void { $this->assertEquals( @@ -25,7 +28,7 @@ public function testGetDisableForeignKeyChecksForMySQL(Driver|string $driver): v ); } - /** @dataProvider providerMySQLDrivers */ + #[DataProvider('mysqlDataProvider')] public function testGetEnableForeignKeyChecksForMySQL(Driver|string $driver): void { $this->assertEquals( @@ -34,7 +37,7 @@ public function testGetEnableForeignKeyChecksForMySQL(Driver|string $driver): vo ); } - /** @dataProvider providerSQLiteDrivers */ + #[DataProvider('sqliteDataProvider')] public function testGetDisableForeignKeyChecksForSqlite(Driver|string $driver): void { $this->assertEquals( @@ -43,7 +46,7 @@ public function testGetDisableForeignKeyChecksForSqlite(Driver|string $driver): ); } - /** @dataProvider providerSQLiteDrivers */ + #[DataProvider('sqliteDataProvider')] public function testGetEnableForeignKeyChecksForSqlite(Driver|string $driver): void { $this->assertEquals( @@ -69,9 +72,9 @@ public function testGetDisableForeignKeyChecksForUnknownDriver(): void } /** @return array */ - public static function providerMySQLDrivers(): array + public static function mysqlDataProvider(): array { - if (self::dbalSupportsAbstractMySQLPlatform()) { + if (class_exists(AbstractMySQLPlatform::class)) { $abstractMySQLDriver = new class() extends AbstractMySQLDriver { public function connect(array $params) { @@ -80,24 +83,22 @@ public function connect(array $params) }; return [ - 'abstract mysql driver' => [$abstractMySQLDriver], - 'abstract mysql driver wrapped in logging middleware' => [ + 'abstract_mysql_driver' => [$abstractMySQLDriver], + 'abstract_mysql_driver_wrapped_in_logging_middleware' => [ (new Middleware(new NullLogger()))->wrap($abstractMySQLDriver), ], ]; } return [ - 'abstract mysql driver' => [AbstractMySQLDriver::class], + 'abstract_mysql_driver' => [AbstractMySQLDriver::class], ]; } - /** - * @return array - */ - public static function providerSQLiteDrivers(): array + /** @return array */ + public static function sqliteDataProvider(): array { - if (self::dbalSupportsAbstractMySQLPlatform()) { + if (class_exists(SqlitePlatform::class)) { $abstractSQLiteDriver = new class() extends AbstractSQLiteDriver { public function connect(array $params) { @@ -106,8 +107,8 @@ public function connect(array $params) }; return [ - 'abstract sqlite driver' => [$abstractSQLiteDriver], - 'abstract sqlite driver wrapped in logging middleware' => [ + 'abstract_sqlite_driver' => [$abstractSQLiteDriver], + 'abstract_sqlite_driver wrapped_in_logging_middleware' => [ (new Middleware(new NullLogger()))->wrap($abstractSQLiteDriver), ], ]; @@ -122,9 +123,4 @@ private function getDriver(Driver|string $driver): MockObject|Driver { return is_string($driver) ? $this->createMock($driver) : $driver; } - - private static function dbalSupportsAbstractMySQLPlatform(): bool - { - return class_exists('\Doctrine\DBAL\Platforms\AbstractMySQLPlatform'); - } } diff --git a/tests/Utils/ConnectionUtilsTrait.php b/tests/Utils/ConnectionUtilsTrait.php deleted file mode 100644 index ae09b7c..0000000 --- a/tests/Utils/ConnectionUtilsTrait.php +++ /dev/null @@ -1,15 +0,0 @@ -