Skip to content

Commit cba6995

Browse files
committed
Fixed #69, updated tests, updated documentation.
1 parent 55c7be9 commit cba6995

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

PhpUnit/AbstractContainerBuilderTestCase.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,17 @@ protected function assertContainerBuilderHasServiceDefinitionWithArgument(
174174
* @param string $serviceId
175175
* @param string $method
176176
* @param array $arguments
177+
* @param int|null $index
177178
*/
178179
protected function assertContainerBuilderHasServiceDefinitionWithMethodCall(
179180
$serviceId,
180181
$method,
181-
array $arguments = array()
182+
array $arguments = array(),
183+
$index = null
182184
) {
183185
$definition = $this->container->findDefinition($serviceId);
184186

185-
self::assertThat($definition, new DefinitionHasMethodCallConstraint($method, $arguments));
187+
self::assertThat($definition, new DefinitionHasMethodCallConstraint($method, $arguments, $index));
186188
}
187189

188190
/**

PhpUnit/DefinitionHasMethodCallConstraint.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ class DefinitionHasMethodCallConstraint extends Constraint
1010
{
1111
private $methodName;
1212
private $arguments;
13+
private $index;
1314

14-
public function __construct($methodName, array $arguments = array())
15+
public function __construct($methodName, array $arguments = array(), $index = null)
1516
{
17+
if ($index !== null && !is_int($index)) {
18+
throw new \RuntimeException(sprintf('Expected value of integer type for method call index, "%s" given.', is_object($index) ? get_class($index) : gettype($index)));
19+
}
20+
1621
parent::__construct();
1722

1823
$this->methodName = $methodName;
1924
$this->arguments = $arguments;
25+
$this->index = $index;
2026
}
2127

2228
public function evaluate($other, $description = '', $returnResult = false)
@@ -27,13 +33,17 @@ public function evaluate($other, $description = '', $returnResult = false)
2733
);
2834
}
2935

30-
foreach ($other->getMethodCalls() as $methodCall) {
31-
list($method, $arguments) = $methodCall;
36+
for ($i = 0; $i < $iMax = count($methodCalls = $other->getMethodCalls()); $i++) {
37+
list($method, $arguments) = $methodCalls[$i];
3238

3339
if ($method !== $this->methodName) {
3440
continue;
3541
}
3642

43+
if (null !== $this->index && $i !== $this->index) {
44+
continue;
45+
}
46+
3747
if ($this->equalArguments($this->arguments, $arguments)) {
3848
return true;
3949
}
@@ -43,9 +53,10 @@ public function evaluate($other, $description = '', $returnResult = false)
4353
$this->fail(
4454
$other,
4555
sprintf(
46-
'None of the method calls matched the expected method "%s" with arguments %s',
56+
'None of the method calls matched the expected method "%s" with arguments %s with %s invocation order index',
4757
$this->methodName,
48-
$this->exporter->export($this->arguments)
58+
$this->exporter->export($this->arguments),
59+
(null === $this->index) ? 'any' : sprintf('"%s"', $this->index)
4960
)
5061
);
5162
}
@@ -56,7 +67,7 @@ public function evaluate($other, $description = '', $returnResult = false)
5667
public function toString()
5768
{
5869
return sprintf(
59-
'has a method call to "%s" with the given arguments',
70+
'has a method call to "%s" with the given arguments on any or explicitly stated invocation order index.',
6071
$this->methodName
6172
);
6273
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ the given index.</dd>
311311
<dt><code>assertContainerBuilderHasServiceDefinitionWithArgument($serviceId, $argumentIndex, $expectedValue)</code></dt>
312312
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id, which has an argument at
313313
the given index, and its value is the given value.</dd>
314-
<dt><code>assertContainerBuilderHasServiceDefinitionWithMethodCall($serviceId, $method, array $arguments = array())</code></dt>
314+
<dt><code>assertContainerBuilderHasServiceDefinitionWithMethodCall($serviceId, $method, array $arguments = array(), $index = null)</code></dt>
315315
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id, which has a method call to
316-
the given method with the given arguments.</dd>
316+
the given method with the given arguments. If index is provided, invocation index order of method call is asserted as well.</dd>
317317
<dt><code>assertContainerBuilderHasServiceDefinitionWithTag($serviceId, $tag, array $attributes = array())</code></dt>
318318
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id, which has the given tag with the given arguments.</dd>
319319
<dt><code>assertContainerBuilderHasServiceDefinitionWithParent($serviceId, $parentServiceId)</code></dt>

Tests/PhpUnit/DefinitionHasMethodCallConstraintTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class DefinitionHasMethodCallConstraintTest extends TestCase
1212
* @test
1313
* @dataProvider definitionProvider
1414
*/
15-
public function match(Definition $definition, $method, $arguments, $expectedToMatch)
15+
public function match(Definition $definition, $method, $arguments, $index, $expectedToMatch)
1616
{
17-
$constraint = new DefinitionHasMethodCallConstraint($method, $arguments);
17+
$constraint = new DefinitionHasMethodCallConstraint($method, $arguments, $index);
1818

1919
$this->assertSame($expectedToMatch, $constraint->evaluate($definition, '', true));
2020
}
@@ -25,25 +25,25 @@ public function definitionProvider()
2525

2626
$definitionWithTwoMethodCalls = new Definition();
2727

28-
$method = 'add';
29-
3028
$argumentsOfFirstCall = array('argument of first call');
31-
$definitionWithTwoMethodCalls->addMethodCall($method, $argumentsOfFirstCall);
29+
$definitionWithTwoMethodCalls->addMethodCall('methodCallOne', $argumentsOfFirstCall);
3230

3331
$argumentsOfSecondCall = array('argument of second call');
34-
$definitionWithTwoMethodCalls->addMethodCall($method, $argumentsOfSecondCall);
32+
$definitionWithTwoMethodCalls->addMethodCall('methodCallTwo', $argumentsOfSecondCall);
3533

3634
$otherArguments = array('some other argument');
3735

3836
return array(
3937
// the definition has no method call
40-
array($definitionWithNoMethodCalls, $method, array(), false),
41-
// the definition has a call to this method, arguments match with the first call
42-
array($definitionWithTwoMethodCalls, $method, $argumentsOfFirstCall, true),
43-
// the definition has a call to this method, arguments match with the second call
44-
array($definitionWithTwoMethodCalls, $method, $argumentsOfSecondCall, true),
38+
array($definitionWithNoMethodCalls, 'noMethodCall', array(), null, false),
39+
// the definition has a call to this method on any invocation index, arguments match with the first call
40+
array($definitionWithTwoMethodCalls, 'methodCallOne', $argumentsOfFirstCall, null, true),
41+
// the definition has a call to this method on first invocation index, arguments match with the second call
42+
array($definitionWithTwoMethodCalls, 'methodCallTwo', $argumentsOfSecondCall, 1, true),
4543
// the definition has a call to this method, but the arguments don't match
46-
array($definitionWithTwoMethodCalls, $method, $otherArguments, false),
44+
array($definitionWithTwoMethodCalls, 'methodCallOne', $otherArguments, null, false),
45+
// the definition has a call to this method, arguments match with the first call, but invocation index is wrong
46+
array($definitionWithTwoMethodCalls, 'methodCallOne', $argumentsOfFirstCall, 1, false),
4747
);
4848
}
4949

@@ -55,6 +55,6 @@ public function it_has_a_string_representation()
5555
$method = 'methodName';
5656
$constraint = new DefinitionHasMethodCallConstraint($method, array());
5757

58-
$this->assertSame('has a method call to "'.$method.'" with the given arguments', $constraint->toString());
58+
$this->assertSame('has a method call to "'.$method.'" with the given arguments on any or explicitly stated invocation order index.', $constraint->toString());
5959
}
6060
}

0 commit comments

Comments
 (0)