diff --git a/doc/database.md b/doc/database.md index ec69e859..b0615b6e 100644 --- a/doc/database.md +++ b/doc/database.md @@ -288,7 +288,6 @@ class AccountControllerTest extends WebTestCase if (!empty($metadatas)) { $schemaTool->createSchema($metadatas); } - $this->postFixtureSetup(); $fixtures = array( 'Acme\MyBundle\DataFixtures\ORM\LoadUserData', diff --git a/doc/examples.md b/doc/examples.md index 186ac1e0..a2fc1a89 100644 --- a/doc/examples.md +++ b/doc/examples.md @@ -13,9 +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 interact during fixtures loading: - - [subscriber](../tests/AppConfig/EventListener/FixturesSubscriber.php) - - [service declaration to put in your test configuration](../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 --------------- diff --git a/tests/AppConfig/config.yml b/tests/AppConfig/config.yml index 0127af07..7e7c7256 100644 --- a/tests/AppConfig/config.yml +++ b/tests/AppConfig/config.yml @@ -17,7 +17,3 @@ services: faker.provider.foo: class: Liip\Acme\Tests\AppConfig\DataFixtures\Faker\Provider\FooProvider tags: [ { name: nelmio_alice.faker.provider } ] - - 'Liip\Acme\Tests\AppConfig\EventListener\FixturesSubscriber': - tags: - - { name: kernel.event_subscriber } diff --git a/tests/AppConfigEvents/AppConfigEventsKernel.php b/tests/AppConfigEvents/AppConfigEventsKernel.php new file mode 100644 index 00000000..34beaf80 --- /dev/null +++ b/tests/AppConfigEvents/AppConfigEventsKernel.php @@ -0,0 +1,32 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Liip\Acme\Tests\AppConfigEvents; + +use Liip\Acme\Tests\AppConfig\AppConfigKernel; +use Symfony\Component\Config\Loader\LoaderInterface; + +class AppConfigEventsKernel extends AppConfigKernel +{ + /** + * Load the config.yml from the current directory. + */ + public function registerContainerConfiguration(LoaderInterface $loader): void + { + // Load the default file. + parent::registerContainerConfiguration($loader); + + // Load the file with the FixturesSubscriber service + $loader->load(__DIR__ . '/config.yml'); + } +} diff --git a/tests/AppConfig/EventListener/FixturesSubscriber.php b/tests/AppConfigEvents/EventListener/FixturesSubscriber.php similarity index 93% rename from tests/AppConfig/EventListener/FixturesSubscriber.php rename to tests/AppConfigEvents/EventListener/FixturesSubscriber.php index 1529770f..04374b11 100644 --- a/tests/AppConfig/EventListener/FixturesSubscriber.php +++ b/tests/AppConfigEvents/EventListener/FixturesSubscriber.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Liip\Acme\Tests\AppConfig\EventListener; +namespace Liip\Acme\Tests\AppConfigEvents\EventListener; use Liip\TestFixturesBundle\Event\FixtureBackupEvent; use Liip\TestFixturesBundle\Event\FixtureEvent; use Liip\TestFixturesBundle\LiipTestFixturesEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -final class FixturesSubscriber implements EventSubscriberInterface +class FixturesSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { diff --git a/tests/AppConfigEvents/config.yml b/tests/AppConfigEvents/config.yml new file mode 100644 index 00000000..7b5dff06 --- /dev/null +++ b/tests/AppConfigEvents/config.yml @@ -0,0 +1,6 @@ +# inherits configuration from ../AppConfig/config.yml + +services: + 'Liip\Acme\Tests\AppConfigEvents\EventListener\FixturesSubscriber': + tags: + - { name: kernel.event_subscriber } diff --git a/tests/Test/ConfigEventsTest.php b/tests/Test/ConfigEventsTest.php new file mode 100644 index 00000000..78526165 --- /dev/null +++ b/tests/Test/ConfigEventsTest.php @@ -0,0 +1,128 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Liip\Acme\Tests\Test; + +use Liip\Acme\Tests\AppConfigEvents\AppConfigEventsKernel; +use Liip\Acme\Tests\AppConfigEvents\EventListener\FixturesSubscriber; +use Liip\TestFixturesBundle\Annotations\DisableDatabaseCache; +use Liip\TestFixturesBundle\LiipTestFixturesEvents; +use Liip\TestFixturesBundle\Test\FixturesTrait; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; + +/** + * Tests that configuration has been loaded and users can be logged in. + * + * Use Tests/AppConfig/AppConfigEventsKernel.php instead of + * Tests/App/AppKernel.php. + * So it must be loaded in a separate process. + * + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + */ +class ConfigEventsTest extends KernelTestCase +{ + use FixturesTrait; + + protected static function getKernelClass(): string + { + return AppConfigEventsKernel::class; + } + + /** + * Check that events have been registered, they don't do anything but will + * be called during tests and that ensure that the examples are working. + */ + public function testLoadEmptyFixturesAndCheckEvents(): void + { + $fixtures = $this->loadFixtures([]); + + $this->assertInstanceOf( + 'Doctrine\Common\DataFixtures\Executor\ORMExecutor', + $fixtures + ); + + $eventDispatcher = $this->getContainer()->get('event_dispatcher'); + + $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::PRE_FIXTURE_BACKUP_RESTORE); + $this->assertSame('preFixtureBackupRestore', $event[0][1]); + + $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::POST_FIXTURE_SETUP); + $this->assertSame('postFixtureSetup', $event[0][1]); + + $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::POST_FIXTURE_BACKUP_RESTORE); + $this->assertSame('postFixtureBackupRestore', $event[0][1]); + + $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::PRE_REFERENCE_SAVE); + $this->assertSame('preReferenceSave', $event[0][1]); + + $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::POST_REFERENCE_SAVE); + $this->assertSame('postReferenceSave', $event[0][1]); + } + + /** + * Check that events are called. + * + * We disable the cache to ensure that all the code is executed. + * + * @dataProvider fixturesEventsProvider + */ + public function testLoadEmptyFixturesAndCheckEventsAreCalled(string $eventName, string $methodName, int $numberOfInvocations): void + { + // Create the mock and declare that the method must be called (or not) + $mock = $this->getMockBuilder(FixturesSubscriber::class)->getMock(); + + $mock->expects($this->exactly($numberOfInvocations)) + ->method($methodName); + + // Register to the event + $eventDispatcher = $this->getContainer()->get('event_dispatcher'); + $eventDispatcher->addListener( + $eventName, + [$mock, $methodName] + ); + + // By loading fixtures, the events will be called (or not) + $fixtures = $this->loadFixtures([]); + + $this->assertInstanceOf( + 'Doctrine\Common\DataFixtures\Executor\ORMExecutor', + $fixtures + ); + } + + /** + * We disable the cache to ensure that other events are called. + * + * @DisableDatabaseCache() + * + * @dataProvider fixturesEventsProvider + */ + public function testLoadEmptyFixturesAndCheckEventsAreCalledWithoutCache(string $eventName, string $methodName, int $numberOfInvocations): void + { + // Swap 0 → 1 and 1 → 0 + $numberOfInvocations = (int) (!$numberOfInvocations); + + $this->testLoadEmptyFixturesAndCheckEventsAreCalled($eventName, $methodName, $numberOfInvocations); + } + + public function fixturesEventsProvider(): array { + return [ + [LiipTestFixturesEvents::PRE_FIXTURE_BACKUP_RESTORE, 'preFixtureBackupRestore', 1], + [LiipTestFixturesEvents::POST_FIXTURE_SETUP, 'postFixtureSetup', 0], + [LiipTestFixturesEvents::POST_FIXTURE_BACKUP_RESTORE, 'postFixtureBackupRestore', 1], + [LiipTestFixturesEvents::PRE_REFERENCE_SAVE, 'preReferenceSave', 0], + [LiipTestFixturesEvents::POST_REFERENCE_SAVE, 'postReferenceSave', 0], + ]; + } +} diff --git a/tests/Test/ConfigTest.php b/tests/Test/ConfigTest.php index 909d7aa1..55a5922a 100644 --- a/tests/Test/ConfigTest.php +++ b/tests/Test/ConfigTest.php @@ -25,7 +25,6 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence use Liip\Acme\Tests\App\Entity\User; use Liip\Acme\Tests\AppConfig\AppConfigKernel; use Liip\TestFixturesBundle\Annotations\DisableDatabaseCache; -use Liip\TestFixturesBundle\LiipTestFixturesEvents; use Liip\TestFixturesBundle\Test\FixturesTrait; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -219,34 +218,4 @@ public function testBackupIsRefreshed(): void // Check that random data has been changed, to ensure that backup was not used. $this->assertNotSame($user1Salt, $user1->getSalt()); } - - /** - * Check that events have been registered, they will be called. - */ - public function testLoadEmptyFixturesAndCheckEvents(): void - { - $fixtures = $this->loadFixtures([]); - - $this->assertInstanceOf( - 'Doctrine\Common\DataFixtures\Executor\ORMExecutor', - $fixtures - ); - - $eventDispatcher = $this->getContainer()->get('event_dispatcher'); - - $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::PRE_FIXTURE_BACKUP_RESTORE); - $this->assertSame('preFixtureBackupRestore', $event[0][1]); - - $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::POST_FIXTURE_SETUP); - $this->assertSame('postFixtureSetup', $event[0][1]); - - $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::POST_FIXTURE_BACKUP_RESTORE); - $this->assertSame('postFixtureBackupRestore', $event[0][1]); - - $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::PRE_REFERENCE_SAVE); - $this->assertSame('preReferenceSave', $event[0][1]); - - $event = $eventDispatcher->getListeners(LiipTestFixturesEvents::POST_REFERENCE_SAVE); - $this->assertSame('postReferenceSave', $event[0][1]); - } }