Permalink
Browse files

Merge pull request Sylius#9789 from adrienlucas/autoconfigure-fixtures

[FixtureBundle] Add autoconfiguration for instance of FixtureInterface
  • Loading branch information...
lchrusciel committed Oct 6, 2018
2 parents 3d5e445 + d5e5699 commit 298b77bde5304427ab6b6f4ce556d0fc04f6d459
@@ -34,7 +34,7 @@ persist some entities in the database, upload some files, dispatch some events o
priority: 0 # The higher priority is, the sooner the fixture will be executed
options: ~ # Fixture options
They implement the ``Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface`` and need to be registered under
They implement the ``Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface`` and are auto-configured with
the ``sylius_fixtures.fixture`` tag in order to be used in suite configuration.
.. note::
@@ -94,7 +94,7 @@ They implement at least one of four interfaces:
The former interface extends the ``ConfigurationInterface``, which is widely known from ``Configuration`` classes
placed under ``DependencyInjection`` directory in Symfony bundles.
In order to be used in suite configuration, they need to be registered under the ``sylius_fixtures.listener``.
In order to be used in suite configuration, they need to be registered under the ``sylius_fixtures.listener`` (if service auto-configuration is not enabled).
Disabling listeners / fixtures in consecutive configurations
------------------------------------------------------------
@@ -42,7 +42,7 @@ to skip the configuration part for now:
}
}
The next step is to register this fixture:
The next step is to register this fixture (if the autowiring and auto-configuration are not enabled) :
.. code-block:: xml
@@ -28,7 +28,7 @@ Let's create a listener that removes the directory before loading the fixtures.
}
}
The next step is to register this listener:
The next step is to register this listener (if the autowiring and auto-configuration are not enabled) :
.. code-block:: xml
@@ -19,6 +19,8 @@
final class FixtureRegistryPass implements CompilerPassInterface
{
public const FIXTURE_SERVICE_TAG = 'sylius_fixtures.fixture';
/**
* {@inheritdoc}
*/
@@ -30,7 +32,7 @@ public function process(ContainerBuilder $container): void
$fixtureRegistry = $container->findDefinition('sylius_fixtures.fixture_registry');
$taggedServices = $container->findTaggedServiceIds('sylius_fixtures.fixture');
$taggedServices = $container->findTaggedServiceIds(self::FIXTURE_SERVICE_TAG);
foreach (array_keys($taggedServices) as $id) {
$fixtureRegistry->addMethodCall('addFixture', [new Reference($id)]);
}
@@ -19,6 +19,8 @@
final class ListenerRegistryPass implements CompilerPassInterface
{
public const LISTENER_SERVICE_TAG = 'sylius_fixtures.listener';
/**
* {@inheritdoc}
*/
@@ -30,7 +32,7 @@ public function process(ContainerBuilder $container): void
$listenerRegistry = $container->findDefinition('sylius_fixtures.listener_registry');
$taggedServices = $container->findTaggedServiceIds('sylius_fixtures.listener');
$taggedServices = $container->findTaggedServiceIds(self::LISTENER_SERVICE_TAG);
foreach (array_keys($taggedServices) as $id) {
$listenerRegistry->addMethodCall('addListener', [new Reference($id)]);
}
@@ -13,6 +13,10 @@
namespace Sylius\Bundle\FixturesBundle\DependencyInjection;
use Sylius\Bundle\FixturesBundle\DependencyInjection\Compiler\FixtureRegistryPass;
use Sylius\Bundle\FixturesBundle\DependencyInjection\Compiler\ListenerRegistryPass;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Bundle\FixturesBundle\Listener\ListenerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
@@ -32,6 +36,11 @@ public function load(array $config, ContainerBuilder $container): void
$loader->load('services.xml');
$this->registerSuites($config, $container);
$container->registerForAutoconfiguration(FixtureInterface::class)
->addTag(FixtureRegistryPass::FIXTURE_SERVICE_TAG);
$container->registerForAutoconfiguration(ListenerInterface::class)
->addTag(ListenerRegistryPass::LISTENER_SERVICE_TAG);
}
/**
@@ -27,7 +27,7 @@
public function it_registers_fixtures(): void
{
$this->setDefinition('sylius_fixtures.fixture_registry', new Definition());
$this->setDefinition('acme.fixture', (new Definition())->addTag('sylius_fixtures.fixture'));
$this->setDefinition('acme.fixture', (new Definition())->addTag(FixtureRegistryPass::FIXTURE_SERVICE_TAG));
$this->compile();
@@ -27,7 +27,7 @@
public function it_registers_listeners(): void
{
$this->setDefinition('sylius_fixtures.listener_registry', new Definition());
$this->setDefinition('acme.listener', (new Definition())->addTag('sylius_fixtures.listener'));
$this->setDefinition('acme.listener', (new Definition())->addTag(ListenerRegistryPass::LISTENER_SERVICE_TAG));
$this->compile();
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\FixturesBundle\Tests\DependencyInjection;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
class DummyFixture implements FixtureInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
}
/**
* {@inheritdoc}
*/
public function load(array $options): void
{
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return '';
}
}
@@ -0,0 +1,34 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\FixturesBundle\Tests\DependencyInjection;
use Sylius\Bundle\FixturesBundle\Listener\ListenerInterface;
class DummyListener implements ListenerInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return '';
}
}
@@ -14,7 +14,10 @@
namespace Sylius\Bundle\FixturesBundle\Tests\DependencyInjection;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Sylius\Bundle\FixturesBundle\DependencyInjection\Compiler\FixtureRegistryPass;
use Sylius\Bundle\FixturesBundle\DependencyInjection\Compiler\ListenerRegistryPass;
use Sylius\Bundle\FixturesBundle\DependencyInjection\SyliusFixturesExtension;
use Symfony\Component\DependencyInjection\Definition;
final class SyliusFixturesExtensionTest extends AbstractExtensionTestCase
{
@@ -42,6 +45,39 @@ public function it_registers_configured_suites(): void
static::assertSame('suite_name', $suiteMethodCall[1][0]);
}
/**
* @test
*/
public function it_autoconfigures_fixtures_and_listeners(): void
{
$this->container->setDefinition(
'acme.fixture_autoconfigured',
(new Definition())
->setClass(DummyFixture::class)
->setAutoconfigured(true)
);
$this->container->setDefinition(
'acme.listener_autoconfigured',
(new Definition())
->setClass(DummyListener::class)
->setAutoconfigured(true)
);
$this->load();
$this->compile();
$this->assertContainerBuilderHasServiceDefinitionWithTag(
'acme.fixture_autoconfigured',
FixtureRegistryPass::FIXTURE_SERVICE_TAG
);
$this->assertContainerBuilderHasServiceDefinitionWithTag(
'acme.listener_autoconfigured',
ListenerRegistryPass::LISTENER_SERVICE_TAG
);
}
/**
* {@inheritdoc}
*/

0 comments on commit 298b77b

Please sign in to comment.