Skip to content

Commit

Permalink
Fix Elasticsearch fixtures doc type to support older versions of Elas…
Browse files Browse the repository at this point in the history
…ticsearch in bulk insert mode (#28)

* Fix Elasticsearch fixtures doc type to support older versions of Elasticsearch in bulk insert mode
* Add PHP 8.0 to CI matrix
  • Loading branch information
hugo-goncalves-kununu authored Jan 6, 2023
1 parent 9de6bbf commit 617c954
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- 7.2
- 7.3
- 7.4
- 8.0
dependencies:
- lowest
- highest
Expand All @@ -35,7 +36,7 @@ jobs:
coverage: xdebug

- name: Install Composer Dependencies
uses: ramsey/composer-install@v1
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependencies }}
composer-options: "--prefer-stable"
Expand Down
29 changes: 23 additions & 6 deletions docs/FixtureTypes/cache-pool-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ composer require psr/cache
The first step to load Cache Pool Fixtures is to create fixtures classes. This classes must implement the [CachePoolFixtureInterface](/src/Adapter/CachePoolFixtureInterface.php).

```php
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Adapter\CachePoolFixtureInterface;
use Psr\Cache\CacheItemPoolInterface;

Expand All @@ -36,16 +39,25 @@ final class MyFixture implements CachePoolFixtureInterface
In order to load the fixtures that you created in the previous step you will need to configure the CachePoolExecutor.

```php
$memcached = new \Memcached();
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Executor\CachePoolExecutor;
use Kununu\DataFixtures\Loader\CachePoolFixturesLoader;
use Kununu\DataFixtures\Purger\CachePoolPurger;
use Memcached;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;

$memcached = new Memcached();
$memcached->addServer('localhost', 11211);

$cache = new Symfony\Component\Cache\Adapter\MemcachedAdapter($memcached);
$cache = new MemcachedAdapter($memcached);

$purger = new Kununu\DataFixtures\Purger\CachePoolPurger($cache);
$purger = new CachePoolPurger($cache);

$executor = new Kununu\DataFixtures\Executor\CachePoolExecutor($cache, $purger);
$executor = new CachePoolExecutor($cache, $purger);

$loader = new Kununu\DataFixtures\Loader\CachePoolFixturesLoader();
$loader = new CachePoolFixturesLoader();
$loader->addFixture(new MyFixture());

$executor->execute($loader->getFixtures());
Expand All @@ -60,7 +72,12 @@ If you want to know more options on how you can load fixtures in the Loader chec
By default, when loading fixtures the cache storage is purged. If you want to change this behavior and instead append the fixtures, you can pass *true* as second argument to the CachePoolExecutor.

```php
$executor = new Kununu\DataFixtures\Executor\CachePoolExecutor($cache, $purger);
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Executor\CachePoolExecutor;

$executor = new CachePoolExecutor($cache, $purger);

// If you want you can `append` the fixtures instead of purging the cache storage
$executor->execute($loader->getFixtures(), true);
Expand Down
4 changes: 2 additions & 2 deletions docs/FixtureTypes/directory-loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ For any fixture extending this class:
- All `*.php` files inside that directory will be loaded when invoking the `load` method of that fixture class.
- Other files will be ignored.
- 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 as described in [Elasticsearch Fixtures](elasticsearch.md).
- 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

Expand All @@ -36,7 +36,7 @@ For any fixture extending this 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`.
- Your fixture class must also implement the `getDocumentIdForBulkIndexation` method as described in [Elasticsearch Fixtures](elasticsearch.md).
- 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 Down
51 changes: 40 additions & 11 deletions docs/FixtureTypes/doctrine-dbal-connection-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ The first step to load *Connection Fixtures* is to create fixtures classes.
This classes must implement the [ConnectionFixtureInterface](/src/Adapter/ConnectionFixtureInterface.php) or extend the class [ConnectionSqlFixture](/src/Adapter/ConnectionSqlFixture.php) which allows you to define fixtures using *Sql* files.

```php
<?php
declare(strict_types=1);

use Doctrine\DBAL\Connection;
use Kununu\DataFixtures\Adapter\ConnectionFixtureInterface;

Expand All @@ -34,6 +37,9 @@ final class MyFixture implements ConnectionFixtureInterface
```

```php
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Adapter\ConnectionSqlFixture;

final class MyFixtureSql extends ConnectionSqlFixture
Expand Down Expand Up @@ -66,19 +72,28 @@ INSERT INTO `database`.`table` (`id`, `name`, `description`) VALUES ('4', 'name4
In order to load the fixtures that you created in the previous step you will need to configure the *Connection Executor*.

```php
$config = new Doctrine\DBAL\Configuration();
<?php
declare(strict_types=1);

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Kununu\DataFixtures\Executor\ConnectionExecutor;
use Kununu\DataFixtures\Loader\ConnectionFixturesLoader;
use Kununu\DataFixtures\Purger\ConnectionPurger;

$config = new Configuration();

$connectionParams = [
'url' => 'mysql://username:password@localhost/test_database'
];

$conn = Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
$conn = DriverManager::getConnection($connectionParams, $config);

$purger = new Kununu\DataFixtures\Purger\ConnectionPurger($conn);
$purger = new ConnectionPurger($conn);

$executor = new Kununu\DataFixtures\Executor\ConnectionExecutor($conn, $purger);
$executor = new ConnectionExecutor($conn, $purger);

$loader = new Kununu\DataFixtures\Loader\ConnectionFixturesLoader();
$loader = new ConnectionFixturesLoader();
$loader->addFixture(new MyFixtureSql());
$loader->addFixture(new MyFixture());

Expand All @@ -92,7 +107,12 @@ If you want to know more options on how you can load fixtures in the Loader chec
By default, when loading fixtures the database is purged. If you want to change this behavior and instead append the fixtures, you can pass *true* as second argument to the ConnectionExecutor.

```php
$executor = new Kununu\DataFixtures\Executor\ConnectionExecutor($conn, $purger);
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Executor\ConnectionExecutor;

$executor = new ConnectionExecutor($conn, $purger);

// If you want you can `append` the fixtures instead of purging the database
$executor->execute($loader->getFixtures(), true);
Expand All @@ -104,10 +124,16 @@ When you do not append fixtures all tables from the database are purged. Still,
You can specify the tables being excluded from being purged by passing them as second argument to the Purger.

```php
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Executor\ConnectionExecutor;
use Kununu\DataFixtures\Purger\ConnectionPurger;

$excludedTables = ['country_codes', 'doctrine_migrations'];
$purger = new Kununu\DataFixtures\Purger\ConnectionPurger($conn, $excludedTables);
$purger = new ConnectionPurger($conn, $excludedTables);

$executor = new Kununu\DataFixtures\Executor\ConnectionExecutor($conn, $purger);
$executor = new ConnectionExecutor($conn, $purger);

$executor->execute($loader->getFixtures());
```
Expand All @@ -118,13 +144,16 @@ The Purger allows you to change the *Sql* statement used to purge the tables.
By default, the Purger will run a *DELETE* statement to purge the tables but you can change it to use a *TRUNCATE* statement instead.

```php
...
$purger = new Kununu\DataFixtures\Purger\ConnectionPurger($conn, $excludedTables);
<?php
declare(strict_types=1);

use Kununu\DataFixtures\Purger\ConnectionPurger;

$purger = new ConnectionPurger($conn, $excludedTables);

// If you want you can change the Purge Mode
$purger->setPurgeMode(1); // PURGE_MODE_DELETE
$purger->setPurgeMode(2); // PURGE_MODE_TRUNCATE

```

## Notes
Expand Down
Loading

0 comments on commit 617c954

Please sign in to comment.