Skip to content

Commit

Permalink
Check that events are really called,
Browse files Browse the repository at this point in the history
split test and Kernel
  • Loading branch information
alexislefebvre committed Jan 8, 2021
1 parent 8357dbb commit 6483024
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 41 deletions.
1 change: 0 additions & 1 deletion doc/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ class AccountControllerTest extends WebTestCase
if (!empty($metadatas)) {
$schemaTool->createSchema($metadatas);
}
$this->postFixtureSetup();
$fixtures = array(
'Acme\MyBundle\DataFixtures\ORM\LoadUserData',
Expand Down
6 changes: 3 additions & 3 deletions doc/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down
4 changes: 0 additions & 4 deletions tests/AppConfig/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
32 changes: 32 additions & 0 deletions tests/AppConfigEvents/AppConfigEventsKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 6 additions & 0 deletions tests/AppConfigEvents/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# inherits configuration from ../AppConfig/config.yml

services:
'Liip\Acme\Tests\AppConfigEvents\EventListener\FixturesSubscriber':
tags:
- { name: kernel.event_subscriber }
128 changes: 128 additions & 0 deletions tests/Test/ConfigEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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\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],
];
}
}
31 changes: 0 additions & 31 deletions tests/Test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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]);
}
}

0 comments on commit 6483024

Please sign in to comment.