Skip to content

Commit 2bd8962

Browse files
Zales0123stloyd
andauthored
Check identity of the Container parameter (#128)
* Check identity of the Container parameter --------- Co-authored-by: Joseph Bielawski <[email protected]>
1 parent 23e0c46 commit 2bd8962

6 files changed

+98
-3
lines changed

PhpUnit/AbstractContainerBuilderTestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ final protected function assertContainerBuilderHasParameter(
132132
);
133133
}
134134

135+
/**
136+
* Assert that the ContainerBuilder for this test has a parameter and that its value and type is identical to expected.
137+
*
138+
* @param mixed $expectedParameterValue
139+
*/
140+
final protected function assertContainerBuilderHasExactParameter(
141+
string $parameterName,
142+
$expectedParameterValue = null
143+
): void {
144+
$checkParameterValue = (func_num_args() > 1);
145+
146+
self::assertThat(
147+
$this->container,
148+
new ContainerHasParameterConstraint($parameterName, $expectedParameterValue, $checkParameterValue, true)
149+
);
150+
}
151+
135152
/**
136153
* Assert that the ContainerBuilder for this test has a service definition with the given id, which has an argument
137154
* at the given index, and its value is the given value.

PhpUnit/ContainerHasParameterConstraint.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@
44

55
use PHPUnit\Framework\Constraint\Constraint;
66
use PHPUnit\Framework\Constraint\IsEqual;
7+
use PHPUnit\Framework\Constraint\IsIdentical;
78
use Symfony\Component\DependencyInjection\ContainerInterface;
89

910
final class ContainerHasParameterConstraint extends Constraint
1011
{
1112
private $parameterName;
1213
private $expectedParameterValue;
1314
private $checkParameterValue;
15+
private $strict;
1416

1517
public function __construct(
1618
string $parameterName,
1719
$expectedParameterValue = null,
18-
bool $checkParameterValue = false
20+
bool $checkParameterValue = false,
21+
bool $strict = false
1922
) {
2023
$this->parameterName = $parameterName;
2124
$this->expectedParameterValue = $expectedParameterValue;
2225
$this->checkParameterValue = $checkParameterValue;
26+
$this->strict = $strict;
2327
}
2428

2529
public function toString(): string
@@ -69,7 +73,7 @@ private function evaluateParameterValue(ContainerInterface $container, bool $ret
6973
{
7074
$actualValue = $container->getParameter($this->parameterName);
7175

72-
$constraint = new IsEqual($this->expectedParameterValue);
76+
$constraint = $this->strict ? new IsIdentical($this->expectedParameterValue) : new IsEqual($this->expectedParameterValue);
7377

7478
if (!$constraint->evaluate($actualValue, '', true)) {
7579
if ($returnResult) {

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ These are the available semantic assertions for each of the test cases shown abo
307307
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter.</dd>
308308
<dt><code>assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)</code></dt>
309309
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter and that its value is the given value.</dd>
310+
<dt><code>assertContainerBuilderHasExactParameter($parameterName)</code></dt>
311+
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter.</dd>
312+
<dt><code>assertContainerBuilderHasExactParameter($parameterName, $expectedParameterValue)</code></dt>
313+
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter and that its value is the given value, as well as its type matches given value type.</dd>
310314
<dt><code>assertContainerBuilderHasServiceDefinitionWithArgument($serviceId, $argumentIndex)</code></dt>
311315
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id, which has an argument at
312316
the given index.</dd>

Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function load(array $config, ContainerBuilder $container): void
1919

2020
// set a parameter manually
2121
$container->setParameter('manual_parameter', 'parameter value');
22+
$container->setParameter('manual_number_parameter', 123123);
23+
$container->setParameter('manual_array_parameter', ['key1' => 'value1', 'key2' => 'value2']);
2224

2325
// manually add a service definition
2426
$definition = new Definition('stdClass');

Tests/PhpUnit/AbstractExtensionTestCaseTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function if_load_is_successful_it_does_not_fail(): void
3333
// Just check parameter exists, value will not be checked.
3434
$this->assertContainerBuilderHasParameter('manual_parameter');
3535

36+
// manually defined number parameter
37+
$this->assertContainerBuilderHasExactParameter('manual_number_parameter', 123123);
38+
// Just check parameter exists, value will not be checked.
39+
$this->assertContainerBuilderHasExactParameter('manual_number_parameter');
40+
41+
// manually defined array parameter
42+
$this->assertContainerBuilderHasExactParameter('manual_array_parameter', ['key1' => 'value1', 'key2' => 'value2']);
43+
// Just check parameter exists, value will not be checked.
44+
$this->assertContainerBuilderHasExactParameter('manual_array_parameter');
45+
3646
// manually defined service
3747
$this->assertContainerBuilderHasService('manual_service_id', 'stdClass');
3848
// Just check service exists, class will not be checked.
@@ -153,6 +163,32 @@ public function if_parameter_exists_but_has_wrong_value_it_fails(): void
153163
$this->assertContainerBuilderHasParameter('manual_parameter', 'wrong');
154164
}
155165

166+
/**
167+
* @test
168+
*/
169+
public function if_parameter_exists_and_has_good_value_but_has_wrong_type_it_fails(): void
170+
{
171+
$this->load();
172+
173+
$this->expectException(ExpectationFailedException::class);
174+
$this->expectExceptionMessage('parameter value');
175+
176+
$this->assertContainerBuilderHasExactParameter('manual_number_parameter', '123123');
177+
}
178+
179+
/**
180+
* @test
181+
*/
182+
public function if_parameter_exists_but_has_wrong_order_it_fails(): void
183+
{
184+
$this->load();
185+
186+
$this->expectException(ExpectationFailedException::class);
187+
$this->expectExceptionMessage('parameter value');
188+
189+
$this->assertContainerBuilderHasExactParameter('manual_array_parameter', ['key2' => 'value2', 'key1' => 'value1']);
190+
}
191+
156192
/**
157193
* @test
158194
*/

Tests/PhpUnit/ContainerHasParameterConstraintTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,25 @@ public function match(
2626
$this->assertSame($expectedToMatch, $constraint->evaluate($container, '', true));
2727
}
2828

29-
public static function containerBuilderProvider()
29+
/**
30+
* @test
31+
*
32+
* @dataProvider typeAwareContainerBuilderProvider
33+
*/
34+
public function matchWithType(
35+
array $containerParameters,
36+
$parameterName,
37+
$parameterValue,
38+
$checkParameterValue,
39+
$expectedToMatch
40+
): void {
41+
$container = $this->createMockContainerWithParameters($containerParameters);
42+
$constraint = new ContainerHasParameterConstraint($parameterName, $parameterValue, $checkParameterValue, true);
43+
44+
$this->assertSame($expectedToMatch, $constraint->evaluate($container, '', true));
45+
}
46+
47+
public static function containerBuilderProvider(): array
3048
{
3149
$parameterName = 'parameter_name';
3250
$parameterValue = 'some value';
@@ -44,6 +62,20 @@ public static function containerBuilderProvider()
4462
];
4563
}
4664

65+
public static function typeAwareContainerBuilderProvider(): array
66+
{
67+
$parameterName = 'parameter_name';
68+
$parameterValue = '123123';
69+
$wrongParameterValue = 123123;
70+
71+
return [
72+
// the container has the parameter but the type don't match
73+
[[$parameterName => $parameterValue], $parameterName, $wrongParameterValue, true, false],
74+
// the container has the parameter and the value matches
75+
[[$parameterName => $parameterValue], $parameterName, $parameterValue, true, true],
76+
];
77+
}
78+
4779
private function createMockContainerWithParameters(array $parameters)
4880
{
4981
$container = $this->createMock(ContainerInterface::class);

0 commit comments

Comments
 (0)