Skip to content

Commit

Permalink
Drop support for PHP 8.0 and doctrine/dbal 2.x (#39)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hugo-goncalves-kununu authored Feb 27, 2024
1 parent b8d56ca commit a1f549d
Show file tree
Hide file tree
Showing 64 changed files with 631 additions and 432 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
php-version:
- 8.0
- 8.1
dependencies:
- lowest
- highest
Expand Down Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions docs/FixtureTypes/directory-loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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\
Expand Down
36 changes: 18 additions & 18 deletions docs/FixtureTypes/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
Expand All @@ -348,9 +348,9 @@ By default, when loading fixtures the Elasticsearch index is purged. If you want
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Executor\ElasticSearchExecutor;
use Kununu\DataFixtures\Executor\ElasticsearchExecutor;

$executor = new ElasticSearchExecutor($client, 'my_index', $purger);
$executor = new ElasticsearchExecutor($client, 'my_index', $purger);

// If you want you can `append` the fixtures instead of purging the Elasticsearch index
$executor->execute($loader->getFixtures(), true);
Expand Down
45 changes: 22 additions & 23 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/9.6/ -->
<!-- https://docs.phpunit.de/en/10.5/ -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" bootstrap="vendor/autoload.php"
colors="true" beStrictAboutChangesToGlobalState="true">
<coverage>
<include>
<directory>src</directory>
</include>
<report>
<clover outputFile="tests/.results/tests-clover.xml"/>
<html outputDirectory="tests/.results/html/"/>
</report>
</coverage>
<testsuites>
<testsuite name="DataFixtures Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
<logging>
<junit outputFile="tests/.results/tests-junit.xml"/>
</logging>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php"
colors="true" beStrictAboutChangesToGlobalState="true" testdox="true">
<coverage>
<report>
<clover outputFile="tests/.results/tests-clover.xml"/>
<html outputDirectory="tests/.results/html/"/>
</report>
</coverage>
<testsuites>
<testsuite name="DataFixtures Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="tests/.results/tests-junit.xml"/>
</logging>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Adapter/ConnectionSqlFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/DirectoryLoader/DirectoryFileSearchTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
26 changes: 0 additions & 26 deletions src/Adapter/ElasticSearchFileFixture.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Adapter/ElasticSearchFixture.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/Adapter/ElasticsearchFileFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace Kununu\DataFixtures\Adapter;

use Elasticsearch\Client;
use Kununu\DataFixtures\Exception\LoadFailedException;
use stdClass;

abstract class ElasticsearchFileFixture extends AbstractFileLoaderFixture implements ElasticsearchFixtureInterface
{
use ElasticsearchFixtureTrait;

private const INDEX = 'index';
private const UPDATE = 'update';
private const ERROR = 'error';

public function load(Client $elasticSearch, string $indexName, bool $throwOnFail = true): void
{
parent::loadFiles(fn(array $documents) => $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);
}
}
}
Loading

0 comments on commit a1f549d

Please sign in to comment.