-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check that events are really called,
split test and Kernel
- Loading branch information
1 parent
8357dbb
commit 6483024
Showing
8 changed files
with
171 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters