Skip to content

Commit d76c06d

Browse files
committed
Made values optional for some assertContainerBuilderHas* assertions.
1 parent 3b4b4c6 commit d76c06d

10 files changed

+80
-35
lines changed

PhpUnit/AbstractContainerBuilderTestCase.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ protected function compile()
7676
* @param $serviceId
7777
* @param $expectedClass
7878
*/
79-
protected function assertContainerBuilderHasService($serviceId, $expectedClass)
79+
protected function assertContainerBuilderHasService($serviceId, $expectedClass = null)
8080
{
81+
$checkExpectedClass = (func_num_args() > 1);
82+
8183
self::assertThat(
8284
$this->container,
83-
new ContainerBuilderHasServiceDefinitionConstraint($serviceId, $expectedClass)
85+
new ContainerBuilderHasServiceDefinitionConstraint($serviceId, $expectedClass, $checkExpectedClass)
8486
);
8587
}
8688

@@ -103,7 +105,7 @@ protected function assertContainerBuilderHasSyntheticService($serviceId)
103105
* @param $aliasId
104106
* @param $expectedServiceId
105107
*/
106-
protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId)
108+
protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId = null)
107109
{
108110
self::assertThat(
109111
$this->container,
@@ -117,11 +119,13 @@ protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId)
117119
* @param $parameterName
118120
* @param $expectedParameterValue
119121
*/
120-
protected function assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)
122+
protected function assertContainerBuilderHasParameter($parameterName, $expectedParameterValue = null)
121123
{
124+
$checkParameterValue = (func_num_args() > 1);
125+
122126
self::assertThat(
123127
$this->container,
124-
new ContainerHasParameterConstraint($parameterName, $expectedParameterValue)
128+
new ContainerHasParameterConstraint($parameterName, $expectedParameterValue, $checkParameterValue)
125129
);
126130
}
127131

@@ -136,13 +140,14 @@ protected function assertContainerBuilderHasParameter($parameterName, $expectedP
136140
protected function assertContainerBuilderHasServiceDefinitionWithArgument(
137141
$serviceId,
138142
$argumentIndex,
139-
$expectedValue
143+
$expectedValue = null
140144
) {
141145
$definition = $this->container->findDefinition($serviceId);
146+
$checkValue = (func_num_args() > 2);
142147

143148
self::assertThat(
144149
$definition,
145-
new DefinitionHasArgumentConstraint($argumentIndex, $expectedValue)
150+
new DefinitionHasArgumentConstraint($argumentIndex, $expectedValue, $checkValue)
146151
);
147152
}
148153

PhpUnit/ContainerBuilderHasAliasConstraint.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class ContainerBuilderHasAliasConstraint extends \PHPUnit_Framework_Constraint
1111
private $expectedServiceId;
1212
protected $exporter;
1313

14-
public function __construct($aliasId, $expectedServiceId)
14+
public function __construct($aliasId, $expectedServiceId = null)
1515
{
1616
if (!is_string($aliasId)) {
1717
throw new \InvalidArgumentException('The $aliasId argument should be a string');
1818
}
1919

20-
if (!is_string($expectedServiceId)) {
20+
if ($expectedServiceId !== null && !is_string($expectedServiceId)) {
2121
throw new \InvalidArgumentException('The $expectedServiceId argument should be a string');
2222
}
2323

@@ -43,7 +43,7 @@ public function evaluate($other, $description = '', $returnResult = false)
4343
return false;
4444
}
4545

46-
if (!$this->evaluateServiceId($other, $returnResult)) {
46+
if ($this->expectedServiceId !== null && !$this->evaluateServiceId($other, $returnResult)) {
4747
return false;
4848
}
4949

PhpUnit/ContainerBuilderHasServiceDefinitionConstraint.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ class ContainerBuilderHasServiceDefinitionConstraint extends \PHPUnit_Framework_
99
{
1010
private $serviceId;
1111
private $expectedClass;
12+
private $checkExpectedClass;
1213
protected $exporter;
1314

14-
public function __construct($serviceId, $expectedClass)
15+
public function __construct($serviceId, $expectedClass = null, $checkExpectedClass = true)
1516
{
1617
if (!is_string($serviceId)) {
1718
throw new \InvalidArgumentException('The $serviceId argument should be a string');
1819
}
1920

20-
if (!is_string($expectedClass)) {
21+
if ($checkExpectedClass && !is_string($expectedClass)) {
2122
throw new \InvalidArgumentException('The $expectedClass argument should be a string');
2223
}
2324

2425
$this->serviceId = $serviceId;
2526
$this->expectedClass = $expectedClass;
27+
$this->checkExpectedClass = $checkExpectedClass;
2628
$this->exporter = new Exporter;
2729
}
2830

@@ -47,7 +49,7 @@ public function evaluate($other, $description = '', $returnResult = false)
4749
return false;
4850
}
4951

50-
if (!$this->evaluateClass($other, $returnResult)) {
52+
if ($this->checkExpectedClass && !$this->evaluateClass($other, $returnResult)) {
5153
return false;
5254
}
5355

PhpUnit/ContainerHasParameterConstraint.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ class ContainerHasParameterConstraint extends \PHPUnit_Framework_Constraint
99
{
1010
private $parameterName;
1111
private $expectedParameterValue;
12+
private $checkParameterValue;
1213
protected $exporter;
1314

14-
public function __construct($parameterName, $expectedParameterValue)
15+
public function __construct($parameterName, $expectedParameterValue = null, $checkParameterValue = false)
1516
{
1617
$this->parameterName = $parameterName;
1718
$this->expectedParameterValue = $expectedParameterValue;
19+
$this->checkParameterValue = $checkParameterValue;
1820
$this->exporter = new Exporter;
1921
}
2022

@@ -38,7 +40,7 @@ public function evaluate($other, $description = '', $returnResult = false)
3840
return false;
3941
}
4042

41-
if (!$this->evaluateParameterValue($other, $returnResult)) {
43+
if ($this->checkParameterValue && !$this->evaluateParameterValue($other, $returnResult)) {
4244
return false;
4345
}
4446

PhpUnit/DefinitionHasArgumentConstraint.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ class DefinitionHasArgumentConstraint extends \PHPUnit_Framework_Constraint
1010
{
1111
private $argumentIndex;
1212
private $expectedValue;
13+
private $checkExpectedValue;
1314
protected $exporter;
1415

15-
public function __construct($argumentIndex, $expectedValue)
16+
public function __construct($argumentIndex, $expectedValue, $checkExpectedValue = true)
1617
{
1718
$this->argumentIndex = (integer)$argumentIndex;
1819
$this->expectedValue = $expectedValue;
20+
$this->checkExpectedValue = $checkExpectedValue;
1921
$this->exporter = new Exporter;
2022
}
2123

@@ -39,7 +41,7 @@ public function evaluate($other, $description = '', $returnResult = false)
3941
return false;
4042
}
4143

42-
if (!$this->evaluateArgumentValue($other, $returnResult)) {
44+
if ($this->checkExpectedValue && !$this->evaluateArgumentValue($other, $returnResult)) {
4345
return false;
4446
}
4547

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,23 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
289289
These are the available semantic assertions for each of the test cases shown above:
290290

291291
<dl>
292+
<dt><code>assertContainerBuilderHasService($serviceId)</code></dt>
293+
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id.</dd>
292294
<dt><code>assertContainerBuilderHasService($serviceId, $expectedClass)</code></dt>
293295
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id and class.</dd>
294296
<dt><code>assertContainerBuilderHasSyntheticService($serviceId)</code></dt>
295297
<dd>Assert that the <code>ContainerBuilder</code> for this test has a synthetic service with the given id.</dd>
298+
<dt><code>assertContainerBuilderHasAlias($aliasId)</code></dt>
299+
<dd>Assert that the <code>ContainerBuilder</code> for this test has an alias.</dd>
296300
<dt><code>assertContainerBuilderHasAlias($aliasId, $expectedServiceId)</code></dt>
297301
<dd>Assert that the <code>ContainerBuilder</code> for this test has an alias and that it is an alias for the given service id.</dd>
302+
<dt><code>assertContainerBuilderHasParameter($parameterName)</code></dt>
303+
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter.</dd>
298304
<dt><code>assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)</code></dt>
299305
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter and that its value is the given value.</dd>
306+
<dt><code>assertContainerBuilderHasServiceDefinitionWithArgument($serviceId, $argumentIndex)</code></dt>
307+
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id, which has an argument at
308+
the given index.</dd>
300309
<dt><code>assertContainerBuilderHasServiceDefinitionWithArgument($serviceId, $argumentIndex, $expectedValue)</code></dt>
301310
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id, which has an argument at
302311
the given index, and its value is the given value.</dd>

Tests/PhpUnit/AbstractExtensionTestCaseTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,25 @@ public function if_load_is_successful_it_does_not_fail()
2929

3030
// manually defined parameter
3131
$this->assertContainerBuilderHasParameter('manual_parameter', 'parameter value');
32+
// Just check parameter exists, value will not be checked.
33+
$this->assertContainerBuilderHasParameter('manual_parameter');
3234

3335
// manually defined service
3436
$this->assertContainerBuilderHasService('manual_service_id', 'stdClass');
37+
// Just check service exists, class will not be checked.
38+
$this->assertContainerBuilderHasService('manual_service_id');
3539

3640
// manually created alias
3741
$this->assertContainerBuilderHasAlias('manual_alias', 'service_id');
42+
// Just check alias exists, service_id will not be checked.
43+
$this->assertContainerBuilderHasAlias('manual_alias');
3844

39-
//
45+
// manually overwritten argument
4046
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 1, 'argument value');
47+
48+
// check for existence of manually created arguments, not checking values.
49+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 0);
50+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 1);
4151
}
4252

4353
/**

Tests/PhpUnit/ContainerBuilderHasAliasConstraintTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public function containerBuilderProvider()
3636
// the container has the alias, but for another service
3737
array($emptyContainerBuilder, $aliasId, $wrongServiceId, false),
3838
// the container has the alias for the right service id
39-
array($builderWithAlias, $aliasId, $rightServiceId, true)
39+
array($builderWithAlias, $aliasId, $rightServiceId, true),
40+
// service id is optional
41+
array($builderWithAlias, $aliasId, null, true),
4042
);
4143
}
4244

Tests/PhpUnit/ContainerBuilderHasServiceDefinitionConstraintTest.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ class ContainerBuilderHasServiceDefinitionConstraintTest extends \PHPUnit_Framew
1212
* @test
1313
* @dataProvider containerBuilderProvider
1414
*/
15-
public function match(ContainerBuilder $containerBuilder, $serviceId, $expectedClass, $shouldMatch)
16-
{
17-
$constraint = new ContainerBuilderHasServiceDefinitionConstraint($serviceId, $expectedClass);
15+
public function match(
16+
ContainerBuilder $containerBuilder,
17+
$serviceId,
18+
$expectedClass,
19+
$checkExpectedClass,
20+
$shouldMatch
21+
) {
22+
$constraint = new ContainerBuilderHasServiceDefinitionConstraint($serviceId, $expectedClass, $checkExpectedClass);
1823

1924
$this->assertSame($shouldMatch, $constraint->evaluate($containerBuilder, '', true));
2025
}
@@ -45,17 +50,19 @@ public function containerBuilderProvider()
4550

4651
return array(
4752
// the container does not have the service definition
48-
array($emptyContainerBuilder, $serviceId, $rightClass, false),
53+
array($emptyContainerBuilder, $serviceId, $rightClass, true, false),
4954
// the container has a service definition, but with the wrong class
50-
array($containerBuilderWithServiceDefinition, $serviceId, $wrongClass, false),
55+
array($containerBuilderWithServiceDefinition, $serviceId, $wrongClass, true, false),
5156
// the container has a service definition with the right class
52-
array($containerBuilderWithServiceDefinition, $serviceId, $rightClass, true),
57+
array($containerBuilderWithServiceDefinition, $serviceId, $rightClass, true, true),
5358
// the container has a service definition with the right class, but it's a parameter
54-
array($containerBuilderWithServiceDefinitionWithParameterClass, $serviceId, $rightClass, true),
59+
array($containerBuilderWithServiceDefinitionWithParameterClass, $serviceId, $rightClass, true, true),
5560
// the container has an alias, but with the wrong class
56-
array($containerBuilderWithAlias, $aliasId, $wrongClass, false),
61+
array($containerBuilderWithAlias, $aliasId, $wrongClass, true, false),
5762
// the container has an alias with the right class
58-
array($containerBuilderWithAlias, $aliasId, $rightClass, true)
63+
array($containerBuilderWithAlias, $aliasId, $rightClass, true, true),
64+
// giving a class is optional
65+
array($containerBuilderWithAlias, $aliasId, null, false, true),
5966
);
6067
}
6168

Tests/PhpUnit/ContainerHasParameterConstraintTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Matthias\SymfonyDependencyInjectionTest\Tests\PhpUnit\DependencyInjection;
44

55
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerHasParameterConstraint;
6-
use Symfony\Component\DependencyInjection\ContainerBuilder;
76
use Symfony\Component\DependencyInjection\ContainerInterface;
87

98
class ContainerHasParameterConstraintTest extends \PHPUnit_Framework_TestCase
@@ -12,9 +11,14 @@ class ContainerHasParameterConstraintTest extends \PHPUnit_Framework_TestCase
1211
* @test
1312
* @dataProvider containerBuilderProvider
1413
*/
15-
public function match(ContainerInterface $container, $parameterName, $parameterValue, $expectedToMatch)
16-
{
17-
$constraint = new ContainerHasParameterConstraint($parameterName, $parameterValue);
14+
public function match(
15+
ContainerInterface $container,
16+
$parameterName,
17+
$parameterValue,
18+
$checkParameterValue,
19+
$expectedToMatch
20+
) {
21+
$constraint = new ContainerHasParameterConstraint($parameterName, $parameterValue, $checkParameterValue);
1822

1923
$this->assertSame($expectedToMatch, $constraint->evaluate($container, '', true));
2024
}
@@ -35,11 +39,13 @@ public function containerBuilderProvider()
3539

3640
return array(
3741
// the container does not have the parameter
38-
array($emptyContainer, $parameterName, $parameterValue, false),
42+
array($emptyContainer, $parameterName, $parameterValue, true, false),
3943
// the container has the parameter but the values don't match
40-
array($containerWithParameter, $parameterName, $wrongParameterValue, false),
44+
array($containerWithParameter, $parameterName, $wrongParameterValue, true, false),
4145
// the container has the parameter and the value matches
42-
array($containerWithParameter, $parameterName, $parameterValue, true),
46+
array($containerWithParameter, $parameterName, $parameterValue, true, true),
47+
// the container has the parameter and the value is optional
48+
array($containerWithParameter, $parameterName, null, false, true),
4349
);
4450
}
4551

0 commit comments

Comments
 (0)