Skip to content

Commit f634db8

Browse files
Merge pull request #33 from uwej711/add_assert_not_has_service
add assertContainerBuilderNotHasService to test that a service is not de...
2 parents b012ab9 + 4cbbae8 commit f634db8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

PhpUnit/AbstractContainerBuilderTestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ protected function assertContainerBuilderHasService($serviceId, $expectedClass =
8686
);
8787
}
8888

89+
/**
90+
* Assert that the ContainerBuilder for this test does not have a service definition with the given id.
91+
*
92+
* @param $serviceId
93+
*/
94+
protected function assertContainerBuilderNotHasService($serviceId)
95+
{
96+
self::assertThat(
97+
$this->container,
98+
new \PHPUnit_Framework_Constraint_Not(new ContainerBuilderHasServiceDefinitionConstraint($serviceId, null, false))
99+
);
100+
}
101+
102+
89103
/**
90104
* Assert that the ContainerBuilder for this test has a synthetic service with the given id.
91105
*

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ These are the available semantic assertions for each of the test cases shown abo
293293
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id.</dd>
294294
<dt><code>assertContainerBuilderHasService($serviceId, $expectedClass)</code></dt>
295295
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id and class.</dd>
296+
<dt><code>assertContainerBuilderNotHasService($serviceId)</code></dt>
297+
<dd>Assert that the <code>ContainerBuilder</code> for this test does not have a service definition with the given id.</dd>
296298
<dt><code>assertContainerBuilderHasSyntheticService($serviceId)</code></dt>
297299
<dd>Assert that the <code>ContainerBuilder</code> for this test has a synthetic service with the given id.</dd>
298300
<dt><code>assertContainerBuilderHasAlias($aliasId)</code></dt>

Tests/PhpUnit/AbstractExtensionTestCaseTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,26 @@ public function if_definition_should_have_a_certain_arguments_for_a_method_call_
237237
array('a wrong argument')
238238
);
239239
}
240+
241+
/**
242+
* @test
243+
*/
244+
public function if_service_is_defined_it_fails()
245+
{
246+
$this->load();
247+
248+
$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException');
249+
250+
$this->assertContainerBuilderNotHasService('loaded_service_id');
251+
}
252+
253+
/**
254+
* @test
255+
*/
256+
public function if_service_is_not_defined_it_does_not_fail()
257+
{
258+
$this->load();
259+
260+
$this->assertContainerBuilderNotHasService('undefined');
261+
}
240262
}

0 commit comments

Comments
 (0)