Skip to content

Commit

Permalink
Merge pull request #316 from alexislefebvre/chore-restore-backup-3.x
Browse files Browse the repository at this point in the history
[3.x] Revert "chore: remove backup"
  • Loading branch information
alexislefebvre authored Jul 2, 2024
2 parents 203df64 + cf1244f commit d8b429d
Show file tree
Hide file tree
Showing 34 changed files with 1,382 additions and 86 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

jobs:
tests:
name: Symfony ${{ matrix.symfony-version }} - PHP ${{ matrix.php-version }} - flags ${{ matrix.composer-flags }}
name: Symfony ${{ matrix.symfony-version }} - PHP ${{ matrix.php-version }} - flags ${{ matrix.composer-flags }} - mysqldump ${{ matrix.mysql-client }}
runs-on: ubuntu-latest

strategy:
Expand All @@ -14,6 +14,7 @@ jobs:
php-version: ['8.2']
composer-flags: ['']
symfony-version: ['^6.4']
mysql-client: [ "default-mysql-client" ]
include:
- php-version: 8.1
# Use "update" instead of "install" since it allows using the "--prefer-lowest" option
Expand All @@ -24,6 +25,10 @@ jobs:
# `theofidry/alice-data-fixtures:1.6.0` and `doctrine/dbal:^4.0` cause issues:
# Error: Call to undefined method Doctrine\DBAL\Connection::exec()
composer-flags: "require doctrine/dbal:^3.0"
- php-version: 8.2
symfony-version: "^6.4"
# add a specific job to test mysqldump from MariaDB
mysql-client: "mariadb-client"
- php-version: 8.2
symfony-version: "^7.0"
- php-version: 8.3
Expand Down Expand Up @@ -57,6 +62,18 @@ jobs:
- 27017:27017

steps:
- name: Install mysqldump
run: |
sudo apt update
sudo apt install -y -q ${{ matrix.mysql-client }}
mysqldump --version
- name: Install mongodb database tools
run: |
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian92-x86_64-100.3.1.deb
sudo apt install ./mongodb-database-tools-*.deb
rm -f mongodb-database-tools-*.deb
- name: Checkout
uses: actions/checkout@v4

Expand Down Expand Up @@ -99,7 +116,7 @@ jobs:
run: echo '127.0.0.1 mariadb postgres mongodb' | sudo tee -a /etc/hosts

- name: Run tests
# Run tests twice to ensure that tests are idempotent
# Run tests twice to ensure that tests are idempotent even if database caching is enabled
run: |
php ./vendor/bin/phpunit --testdox --process-isolation
php ./vendor/bin/phpunit --testdox --process-isolation
5 changes: 5 additions & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ Here is the full configuration with default values:
liip_test_fixtures:
keep_database_and_schema: false
cache_metadata: true
cache_db: ~
```
- `keep_database_and_schema`: pass it to `true` to avoid deleting and creating the database and schema before each test, you'll have to create the database schema before running your tests:
1. create database with `bin/console --env=test doctrine:database:create`:
2. create schema with `bin/console --env=test doctrine:schema:update --force` or `bin/console --env=test doctrine:migrations:migrate --no-interaction`
- `cache_metadata`: using the cache slightly improve the performance
- `cache_db`: an array with a storage as key and a service as value, examples :
- `sqlite: 'Liip\TestFixturesBundle\Services\DatabaseBackup\SqliteDatabaseBackup'`
- `mysql: 'Liip\TestFixturesBundle\Services\DatabaseBackup\MysqlDatabaseBackup'`
- `mongodb: 'Liip\TestFixturesBundle\Services\DatabaseBackup\MongodbDatabaseBackup'`
« [Installation](./installation.md) • [Database](./database.md) »
37 changes: 37 additions & 0 deletions doc/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Methods

It also give access to other helpers:

- `setDatabaseCacheEnabled()` accept `true` or `false` to disable the cache
- you can call `$this->databaseTool->withDatabaseCacheEnabled(false)->loadFixtures(…)` to disable the cache on-demand

- `setPurgeMode()` accept `true` or `false` to disable purging the database
- you can call `$this->databaseTool->withPurgeMode(false)->loadFixtures(…)` to disable the purging on-demand

Expand Down Expand Up @@ -106,6 +109,40 @@ Tips for Fixture Loading Tests

NB: If you have an existing Doctrine configuration which uses slaves be sure to separate out the configuration for the slaves. Further detail is provided at the bottom of this README.

2. In order to run your tests even faster, use LiipFunctionalBundle cached database.
This will create backups of the initial databases (with all fixtures loaded)
and re-load them when required.

**Attention: you need Doctrine >= 2.2 to use this feature.**

```yaml
# sf4: config/packages/test/framework.yaml
liip_test_fixtures:
cache_db:
sqlite: 'Liip\TestFixturesBundle\Services\DatabaseBackup\SqliteDatabaseBackup'
```

### Custom database cache services ([↑](#methods))

To create custom database cache service:

Create cache class, implement `\Liip\TestFixturesBundle\Services\DatabaseBackup\DatabaseBackupInterface` and add it to config

For example:
```yaml
# app/config/config_test.yml
liip_test_fixtures:
cache_db:
mysql: 'Liip\TestFixturesBundle\Services\DatabaseBackup\MysqlDatabaseBackup'
mongodb: 'Liip\TestFixturesBundle\Services\DatabaseBackup\MongodbDatabaseBackup'
db2: ...
[Other \Doctrine\DBAL\Platforms\AbstractPlatform name]: ...
```

**Attention: `Liip\TestFixturesBundle\Services\DatabaseBackup\MysqlDatabaseBackup` requires `mysql-client` installed on server.**

**Attention: `Liip\TestFixturesBundle\Services\DatabaseBackup\MongodbDatabaseBackup` requires `mongodb-clients` installed on server.**

### Load fixtures ([↑](#methods))

Load your Doctrine fixtures in your tests:
Expand Down
13 changes: 11 additions & 2 deletions doc/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

Available events:

- `LiipTestFixturesEvents::PRE_FIXTURE_BACKUP_RESTORE`: triggered before restoring the backup
- `LiipTestFixturesEvents::POST_FIXTURE_BACKUP_RESTORE`: triggered after the backup has been restored
- `LiipTestFixturesEvents::POST_FIXTURE_SETUP`: triggered before purging the database
- `LiipTestFixturesEvents::PRE_REFERENCE_SAVE`: triggered before saving the backup of fixtures
- `LiipTestFixturesEvents::POST_REFERENCE_SAVE`: triggered before the backup of fixtures has been saved

## Registering events

Expand All @@ -15,6 +19,7 @@ declare(strict_types=1);

namespace Liip\Acme\Tests\AppConfigEvents\EventListener;

use Liip\TestFixturesBundle\Event\PreFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\LiipTestFixturesEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand All @@ -23,12 +28,16 @@ class FixturesSubscriber implements EventSubscriberInterface
public static function getSubscribedEvents(): array
{
return [
LiipTestFixturesEvents::POST_FIXTURE_SETUP => 'postFixtureSetup',
LiipTestFixturesEvents::PRE_FIXTURE_BACKUP_RESTORE => 'preFixtureBackupRestore',
];
}

public function postFixtureSetup(FixtureEvent $fixtureEvent): void
public function preFixtureBackupRestore(PreFixtureBackupRestoreEvent $preFixtureBackupRestoreEvent): void
{
$manager = $preFixtureBackupRestoreEvent->getManager();
$repository = $preFixtureBackupRestoreEvent->getRepository();
$backupFilePath = $preFixtureBackupRestoreEvent->getBackupFilePath();

// your code
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->arrayNode('cache_db')
->addDefaultsIfNotSet()
->ignoreExtraKeys(false)
->children()
->scalarNode('sqlite')
->defaultNull()
->end()
->end()
->end()
->booleanNode('keep_database_and_schema')->defaultFalse()->end()
->booleanNode('cache_metadata')->defaultTrue()->end()
;
Expand Down
15 changes: 14 additions & 1 deletion src/DependencyInjection/LiipTestFixturesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('database_tools.xml');

foreach ($config as $key => $value) {
$container->setParameter($this->getAlias().'.'.$key, $value);
// If the node is an array,
// e.g. "liip_test_fixtures.cache_db.mysql",
// set the value as
// "liip_test_fixtures.cache_db.mysql"
// instead of an array "liip_test_fixtures.cache_db"
// with a "mysql" key.
if (\is_array($value)) {
foreach ($value as $key2 => $value2) {
$container->setParameter($this->getAlias().'.'.$key.
'.'.$key2, $value2);
}
} else {
$container->setParameter($this->getAlias().'.'.$key, $value);
}
}
}
}
12 changes: 10 additions & 2 deletions src/Event/FixtureEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@

namespace Liip\TestFixturesBundle\Event;

// Compatibility layer to use Contract if Symfony\Contracts\EventDispatcher\Event is not available
use Symfony\Contracts\EventDispatcher\Event;

class FixtureEvent extends Event
{
if (class_exists('\Symfony\Component\EventDispatcher\Event')) {
// Symfony < 5.0
class FixtureEvent extends \Symfony\Component\EventDispatcher\Event
{
}
} else {
class FixtureEvent extends Event
{
}
}
29 changes: 29 additions & 0 deletions src/Event/PostFixtureBackupRestoreEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

class PostFixtureBackupRestoreEvent extends FixtureEvent
{
private $backupFilePath;

public function __construct(string $backupFilePath)
{
$this->backupFilePath = $backupFilePath;
}

public function getBackupFilePath(): string
{
return $this->backupFilePath;
}
}
49 changes: 49 additions & 0 deletions src/Event/PreFixtureBackupRestoreEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

use Doctrine\Common\DataFixtures\ReferenceRepository;
use Doctrine\Persistence\ObjectManager;

class PreFixtureBackupRestoreEvent extends FixtureEvent
{
private $manager;
private $repository;
private $backupFilePath;

public function __construct(
ObjectManager $manager,
ReferenceRepository $executor,
string $backupFilePath
) {
$this->manager = $manager;
$this->repository = $executor;
$this->backupFilePath = $backupFilePath;
}

public function getManager(): ObjectManager
{
return $this->manager;
}

public function getRepository(): ReferenceRepository
{
return $this->repository;
}

public function getBackupFilePath(): string
{
return $this->backupFilePath;
}
}
49 changes: 49 additions & 0 deletions src/Event/ReferenceSaveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
use Doctrine\Persistence\ObjectManager;

class ReferenceSaveEvent extends FixtureEvent
{
private $manager;
private $executor;
private $backupFilePath;

public function __construct(
ObjectManager $manager,
AbstractExecutor $executor,
string $backupFilePath
) {
$this->manager = $manager;
$this->executor = $executor;
$this->backupFilePath = $backupFilePath;
}

public function getManager(): ObjectManager
{
return $this->manager;
}

public function getExecutor(): AbstractExecutor
{
return $this->executor;
}

public function getBackupFilePath(): string
{
return $this->backupFilePath;
}
}
15 changes: 15 additions & 0 deletions src/LiipTestFixturesEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@
namespace Liip\TestFixturesBundle;

use Liip\TestFixturesBundle\Event\FixtureEvent;
use Liip\TestFixturesBundle\Event\PostFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\PreFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\ReferenceSaveEvent;

final class LiipTestFixturesEvents
{
/** @see PreFixtureBackupRestoreEvent */
public const PRE_FIXTURE_BACKUP_RESTORE = 'liip_test_fixtures.pre_fixture_backup_restore';

/** @see FixtureEvent */
public const POST_FIXTURE_SETUP = 'liip_test_fixtures.post_fixture_setup';

/** @see PostFixtureBackupRestoreEvent */
public const POST_FIXTURE_BACKUP_RESTORE = 'liip_test_fixtures.post_fixture_backup_restore';

/** @see ReferenceSaveEvent */
public const PRE_REFERENCE_SAVE = 'liip_test_fixtures.pre_reference_save';

/** @see ReferenceSaveEvent */
public const POST_REFERENCE_SAVE = 'liip_test_fixtures.post_reference_save';
}
15 changes: 15 additions & 0 deletions src/Resources/config/database_tools.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
<argument type="service" id="doctrine_mongodb.odm.symfony.fixtures.loader" on-invalid="null"/>
</service>

<service id="Liip\TestFixturesBundle\Services\DatabaseBackup\SqliteDatabaseBackup" public="true">
<argument type="service" id="service_container" />
<argument type="service" id="Liip\TestFixturesBundle\Services\FixturesLoaderFactory" />
</service>

<service id="Liip\TestFixturesBundle\Services\DatabaseBackup\MysqlDatabaseBackup" public="true">
<argument type="service" id="service_container" />
<argument type="service" id="Liip\TestFixturesBundle\Services\FixturesLoaderFactory" />
</service>

<service id="Liip\TestFixturesBundle\Services\DatabaseBackup\MongodbDatabaseBackup" public="true">
<argument type="service" id="service_container" />
<argument type="service" id="Liip\TestFixturesBundle\Services\MongoDBFixturesLoaderFactory" />
</service>

<service id="Liip\TestFixturesBundle\Services\DatabaseTools\ORMDatabaseTool" public="false">
<argument type="service" id="service_container" />
<argument type="service" id="Liip\TestFixturesBundle\Services\FixturesLoaderFactory" />
Expand Down
Loading

0 comments on commit d8b429d

Please sign in to comment.