Skip to content

Commit

Permalink
Merge pull request #81 from liip/add-events
Browse files Browse the repository at this point in the history
Add events
  • Loading branch information
alexislefebvre committed Feb 22, 2021
2 parents 2fe3393 + 7dbf177 commit da57b0b
Show file tree
Hide file tree
Showing 24 changed files with 532 additions and 110 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ on: [push, pull_request]

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

strategy:
fail-fast: false
matrix:
php-version: ['7.2', '7.3', '7.4', '8.0']
composer-flags: ['']
symfony-version: ['']
include:
- php-version: 7.2
composer-flags: "--prefer-lowest"
- php-version: 7.2
symfony-version: "^3.4"
- php-version: 7.2
symfony-version: "^4.4"
- php-version: 7.3
Expand Down Expand Up @@ -72,14 +67,8 @@ jobs:
run: composer require --no-update symfony/framework-bundle=${{ matrix.symfony-version }}

- name: Install Composer dependencies
if: matrix.composer-flags == ''
run: composer install

- name: Install Composer dependencies with options
if: matrix.composer-flags != ''
# Use "update" instead of "install" since it allows using the "--prefer-lowest" option
run: composer update ${{ matrix.composer-flags }}

- name: Run tests
# In phpunit.xml.dist, tests annotated with "@group mysql" are excluded, revert this
# Run tests twice to ensure that tests are idempotent even if database caching is enabled
Expand Down
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ addons:

matrix:
include:
- php: 7.2
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 7.2
env: SYMFONY_VERSION="^3.4"
- php: 7.2
env: SYMFONY_VERSION="^4.4"
- php: 7.3
Expand All @@ -35,8 +31,7 @@ before_install:
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/framework-bundle=$SYMFONY_VERSION; fi

install:
# use "update" instead of "install" since it allows using the "--prefer-lowest" option
- composer update $COMPOSER_FLAGS
- composer install

# In phpunit.xml.dist, tests annotated with "@group mysql" are excluded,
# revert this.
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

See https://github.com/liip/LiipTestFixturesBundle/releases

## 2.x.0 (TBA)

- Removed callback functions passed from test classes to the fixtures service, they have been replaced by events, see these examples:
- [declare subscriber(s)](../tests/AppConfigEvents/EventListener/FixturesSubscriber.php)
- [service declaration to put in your test configuration](../tests/AppConfigEvents/config.yml)

## 1.1.0 (TBA)

- Added parameter `liip_test_fixtures.keep_database_and_schema` to avoid issue with [DAMADoctrineTestBundle](https://github.com/dmaicher/doctrine-test-bundle)
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"require": {
"php": "^7.2 || ^8.0",
"doctrine/common": "^2.13 || ^3.0",
"symfony/framework-bundle": "^3.4 || ^4.4 || ^5.0",
"symfony/yaml": "^3.4 || ^4.4 || ^5.0"
"symfony/event-dispatcher": "^4.4 || ^5.0",
"symfony/event-dispatcher-contracts": "^1 || ^2",
"symfony/framework-bundle": "^4.4 || ^5.0",
"symfony/yaml": "^4.4 || ^5.0"
},
"require-dev": {
"doctrine/data-fixtures": "^1.3",
Expand All @@ -32,7 +34,7 @@
"phpunit/phpunit": "^7.5 || ^8.0",
"symfony/monolog-bridge": ">=3",
"symfony/monolog-bundle": "^3.2",
"symfony/phpunit-bridge": "^3.4 || ^4.4 || ^5.0",
"symfony/phpunit-bridge": "^4.4 || ^5.0",
"theofidry/alice-data-fixtures": "^1.0.1"
},
"suggest": {
Expand All @@ -57,7 +59,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "2.x-dev"
}
}
}
13 changes: 0 additions & 13 deletions doc/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ Tips for Fixture Loading Tests
test-environment to use a SQLite-database. This will make your tests run
faster and will create a fresh, predictable database for every test you run.

* For symfony 3: add those lines to `app/config/config_test.yml`:
```yaml
# app/config/config_test.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlite
path: "%kernel.cache_dir%/test.db"
```

* For symfony 4 : create file if it doesn't exists `config/packages/test/doctrine.yaml`, and if it does append those lines:
```yaml
# config/packages/test/doctrine.yaml
Expand Down Expand Up @@ -288,7 +276,6 @@ class AccountControllerTest extends WebTestCase
if (!empty($metadatas)) {
$schemaTool->createSchema($metadatas);
}
$this->postFixtureSetup();
$fixtures = array(
'Acme\MyBundle\DataFixtures\ORM\LoadUserData',
Expand Down
3 changes: 3 additions & 0 deletions doc/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ The bundle's internal tests show several ways to load fixtures:
- [fixture to load](../tests/App/DataFixtures/ORM/user_with_custom_provider.yml)
- [custom provider](../tests/AppConfig/DataFixtures/Faker/Provider/FooProvider.php)
- [service declaration](../tests/AppConfig/config.yml)
- using events to perform actions during fixtures loading:
- [declare subscriber(s)](../tests/AppConfigEvents/EventListener/FixturesSubscriber.php)
- [service declaration to put in your test configuration](../tests/AppConfigEvents/config.yml)

Functional test
---------------
Expand Down
10 changes: 2 additions & 8 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder(): TreeBuilder
{
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$treeBuilder = new TreeBuilder('liip_test_fixtures');
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('liip_test_fixtures', 'array');
}
$treeBuilder = new TreeBuilder('liip_test_fixtures');
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
Expand Down
28 changes: 28 additions & 0 deletions src/Event/FixtureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

// Compatibility layer to use Contract if Symfony\Contracts\EventDispatcher\Event is not available
use Symfony\Contracts\EventDispatcher\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 <smith@pooteeweet.org>
*
* 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 <smith@pooteeweet.org>
*
* 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 <smith@pooteeweet.org>
*
* 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;
}
}
37 changes: 37 additions & 0 deletions src/LiipTestFixturesEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

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 */
const PRE_FIXTURE_BACKUP_RESTORE = 'liip_test_fixtures.pre_fixture_backup_restore';

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

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

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

/** @see ReferenceSaveEvent */
const POST_REFERENCE_SAVE = 'liip_test_fixtures.post_reference_save';
}
7 changes: 3 additions & 4 deletions src/Services/DatabaseToolCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Doctrine\Common\Annotations\AnnotationReader;
use Liip\TestFixturesBundle\Annotations\DisableDatabaseCache;
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
use ReflectionMethod;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -43,7 +43,7 @@ public function add(AbstractDatabaseTool $databaseTool): void
$this->items[$databaseTool->getType()][$databaseTool->getDriverName()] = $databaseTool;
}

public function get($omName = null, $registryName = 'doctrine', int $purgeMode = null, KernelTestCase $testCase = null): AbstractDatabaseTool
public function get($omName = null, $registryName = 'doctrine', int $purgeMode = null): AbstractDatabaseTool
{
/** @var ManagerRegistry $registry */
$registry = $this->container->get($registryName);
Expand All @@ -56,7 +56,6 @@ public function get($omName = null, $registryName = 'doctrine', int $purgeMode =
$databaseTool->setRegistry($registry);
$databaseTool->setObjectManagerName($omName);
$databaseTool->setPurgeMode($purgeMode);
$databaseTool->setTestCase($testCase);

$databaseTool->setDatabaseCacheEnabled($this->isCacheEnabled());

Expand All @@ -68,7 +67,7 @@ public function isCacheEnabled(): bool
foreach (debug_backtrace() as $step) {
if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation
$annotations = $this->annotationReader->getMethodAnnotations(
new \ReflectionMethod($step['class'], $step['function'])
new ReflectionMethod($step['class'], $step['function'])
);

foreach ($annotations as $annotationClass) {
Expand Down
Loading

0 comments on commit da57b0b

Please sign in to comment.