Skip to content

Commit 51d22dd

Browse files
committed
Implemented support for PrependExtensionInterface in AbstractExtensionTestCase::load()
1 parent ecacd83 commit 51d22dd

File tree

3 files changed

+51
-53
lines changed

3 files changed

+51
-53
lines changed

PhpUnit/AbstractExtensionTestCase.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,28 @@ protected function setUp()
4848
}
4949
}
5050

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-
7351
/**
7452
* Call this method from within your test after you have (optionally) modified the ContainerBuilder for this test
7553
* ($this->container).
7654
*
77-
* @param array $specificConfiguration
55+
* If your extension(s) implements \Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface, you may
56+
* set $withPrependInvocation to TRUE to invoke prepend() method prior to load() method of your extension.
57+
*
58+
* @param array $configurationValues
59+
* @param bool $withPrependInvocation
7860
*/
79-
protected function load(array $configurationValues = array())
61+
protected function load(array $configurationValues = array(), $withPrependInvocation = false)
8062
{
8163
$this->loadMethodInvoked = true;
8264

8365
$configs = array($this->getMinimalConfiguration(), $configurationValues);
8466

8567
foreach ($this->container->getExtensions() as $extension) {
68+
69+
if ($withPrependInvocation && $extension instanceof PrependExtensionInterface) {
70+
$extension->prepend($this->container);
71+
}
72+
8673
$extension->load($configs, $this->container);
8774
}
8875
}

Tests/PhpUnit/AbstractExtensionTestCaseTest.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,6 @@ 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();
8152
}
8253

8354
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Matthias\DependencyInjectionTests\Test\DependencyInjection;
4+
5+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
6+
use Matthias\SymfonyDependencyInjectionTest\Tests\Fixtures\MatthiasDependencyInjectionTestExtension;
7+
use PHPUnit\Framework\ExpectationFailedException;
8+
9+
class AbstractPrependExtensionTestCaseTest extends AbstractExtensionTestCase
10+
{
11+
protected function getContainerExtensions()
12+
{
13+
return array(
14+
new MatthiasDependencyInjectionTestExtension()
15+
);
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
public function if_prepend_invoked_it_does_not_fails()
22+
{
23+
$this->load([], true);
24+
25+
$this->assertContainerBuilderHasParameter('prepend_extension_interface.successfully_invoked', 'prepended value');
26+
}
27+
28+
/**
29+
* @test
30+
*/
31+
public function if_prepend_is_not_invoked_it_does_not_fails()
32+
{
33+
$this->load();
34+
35+
$this->expectException(ExpectationFailedException::class);
36+
$this->expectExceptionMessage('prepend_extension_interface.successfully_invoked');
37+
38+
$this->assertContainerBuilderHasParameter('prepend_extension_interface.successfully_invoked', 'prepended value');
39+
}
40+
}

0 commit comments

Comments
 (0)