Skip to content

Commit ecacd83

Browse files
committed
Added support for prependable container extension test.
1 parent e490424 commit ecacd83

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

PhpUnit/AbstractExtensionTestCase.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;
44

55
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
6+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
67

78
abstract class AbstractExtensionTestCase extends AbstractContainerBuilderTestCase
89
{
10+
/**
11+
* @var bool denotes if load() method has been invoked.
12+
*/
13+
private $loadMethodInvoked;
14+
915
/**
1016
* Return an array of container extensions you need to be registered for each test (usually just the container
1117
* extension you are testing.
@@ -35,11 +41,35 @@ protected function setUp()
3541
{
3642
parent::setUp();
3743

44+
$this->loadMethodInvoked = false;
45+
3846
foreach ($this->getContainerExtensions() as $extension) {
3947
$this->container->registerExtension($extension);
4048
}
4149
}
4250

51+
/**
52+
* Call this method (optionally) form within your test after you have (optionally) modified the ContainerBuilder
53+
* for this test ($this->container), but BEFORE you call load method ($this->load()) - if your extension(s) implements
54+
* \Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface
55+
*
56+
* @see http://symfony.com/doc/current/bundles/prepend_extension.html
57+
* @see \Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface
58+
*/
59+
protected function prepend()
60+
{
61+
if ($this->loadMethodInvoked) {
62+
throw new \LogicException(sprintf('Method "prepend()" from "%s" must not be invoked after "load()" method.', PrependExtensionInterface::class));
63+
}
64+
65+
foreach ($this->container->getExtensions() as $extension) {
66+
67+
if ($extension instanceof PrependExtensionInterface) {
68+
$extension->prepend($this->container);
69+
}
70+
}
71+
}
72+
4373
/**
4474
* Call this method from within your test after you have (optionally) modified the ContainerBuilder for this test
4575
* ($this->container).
@@ -48,6 +78,8 @@ protected function setUp()
4878
*/
4979
protected function load(array $configurationValues = array())
5080
{
81+
$this->loadMethodInvoked = true;
82+
5183
$configs = array($this->getMinimalConfiguration(), $configurationValues);
5284

5385
foreach ($this->container->getExtensions() as $extension) {

Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
use Symfony\Component\DependencyInjection\Definition;
66
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
77
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
89
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
910
use Symfony\Component\Config\FileLocator;
1011

11-
class MatthiasDependencyInjectionTestExtension implements ExtensionInterface
12+
class MatthiasDependencyInjectionTestExtension implements ExtensionInterface, PrependExtensionInterface
1213
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function prepend(ContainerBuilder $container)
18+
{
19+
$container->setParameter('prepend_extension_interface.successfully_invoked', 'prepended value');
20+
}
21+
1322
public function load(array $config, ContainerBuilder $container)
1423
{
1524
// load some service definitions

Tests/PhpUnit/AbstractExtensionTestCaseTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ public function if_load_is_successful_it_does_not_fail()
4949
// check for existence of manually created arguments, not checking values.
5050
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 0);
5151
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 1);
52+
53+
// manually defined parameter in prepend service
54+
$this->assertContainerBuilderHasParameter('manual_parameter', 'parameter value');
55+
56+
// prepend method is not invoked before load method, it must be invoked manually
57+
$this->assertFalse($this->container->hasParameter('prepend_extension_interface.successfully_invoked'));
58+
}
59+
60+
/**
61+
* @test
62+
*/
63+
public function if_prepend_is_invoked_before_load_it_does_not_fail()
64+
{
65+
$this->prepend();
66+
$this->load();
67+
68+
// prepend method modifies container builder
69+
$this->assertContainerBuilderHasParameter('prepend_extension_interface.successfully_invoked', 'prepended value');
70+
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function if_prepend_is_invoked_after_load_it_fails()
76+
{
77+
$this->expectException(\LogicException::class);
78+
79+
$this->load();
80+
$this->prepend();
5281
}
5382

5483
/**

0 commit comments

Comments
 (0)