Skip to content

Commit

Permalink
Added helper methods to ExtensionTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
John Cartwright committed Oct 21, 2013
1 parent 7aee52c commit 4ea09f9
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions Test/DependencyInjection/ExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Ryan Albon <ryanalbon@gmail.com>
* @author John Cartwright <jcartdev@gmail.com>
*/
abstract class ExtensionTestCase extends ContainerAwareTestCase
{
Expand Down Expand Up @@ -61,24 +62,24 @@ public function assertParameter($expected, $key)
*/
public function assertHasDefinition($id)
{
$actual = $this->container->hasDefinition($id) ?: $this->containerBuilder->hasAlias($id);
$actual = $this->container->hasDefinition($id) ?: $this->container->hasAlias($id);

$this->assertTrue($actual);
$this->assertTrue($actual, sprintf('Expected definition "%s" to exist', $id));
}

/**
* Assertion on the Constructor Arguments of a DIC Service Definition.
*
* @param \Symfony\Component\DependencyInjection\Definition $definition
* @param array $arguments
* @param array $argumentList
*/
public function assertDICConstructorArguments(Definition $definition, array $arguments)
public function assertDICConstructorArguments(Definition $definition, array $argumentList)
{
$this->assertEquals(
$arguments,
$argumentList,
$definition->getArguments(),
sprintf(
'Expected and actual DIC Service constructor arguments of definition "%s" do not match.',
'Expected and actual DIC Service constructor argumentList of definition "%s" do not match.',
$definition->getClass()
)
);
Expand All @@ -99,19 +100,54 @@ public function assertDICDefinitionClass(Definition $definition, $expectedClass)
);
}

/**
* Assertion on the called Method of a DIC Service Definition.
*
* @param \Symfony\Component\DependencyInjection\Definition $definition
* @param string $methodName
* @param array $parameterList
*/
public function assertDICDefinitionMethodCall(Definition $definition, $methodName, array $parameterList = null)
{
$callList = $definition->getMethodCalls();
$matchedCall = null;

foreach ($callList as $call) {
if ($call[0] === $methodName) {
$matchedCall = $call;

break;
}
}

if ( ! $matchedCall) {
$this->fail(
sprintf('Method "%s" is was expected to be called.', $methodName)
);
}

if ($parameterList !== null) {
$this->assertEquals(
$parameterList,
$matchedCall[1],
sprintf('Expected parameters to method "%s" do not match the actual parameters.', $methodName)
);
}
}

/**
* Assertion on the called Method position of a DIC Service Definition.
*
* @param integer $position
* @param \Symfony\Component\DependencyInjection\Definition $definition
* @param string $methodName
* @param array $params
* @param array $parameterList
*/
public function assertDICDefinitionMethodCallAt($position, Definition $definition, $methodName, array $params = null)
public function assertDICDefinitionMethodCallAt($position, Definition $definition, $methodName, array $parameterList = null)
{
$calls = $definition->getMethodCalls();
$callList = $definition->getMethodCalls();

if ( ! isset($calls[$position][0])) {
if ( ! isset($callList[$position][0])) {
// Throws an Exception
$this->fail(
sprintf('Method "%s" is expected to be called at position %s.', $methodName, $position)
Expand All @@ -120,16 +156,35 @@ public function assertDICDefinitionMethodCallAt($position, Definition $definitio

$this->assertEquals(
$methodName,
$calls[$position][0],
$callList[$position][0],
sprintf('Method "%s" is expected to be called at position %s.', $methodName, $position)
);

if ($params !== null) {
if ($parameterList !== null) {
$this->assertEquals(
$params,
$calls[$position][1],
$parameterList,
$callList[$position][1],
sprintf('Expected parameters to methods "%s" do not match the actual parameters.', $methodName)
);
}
}

/**
* Assertion on the Definition that a method is not called for a DIC Service Definition.
*
* @param \Symfony\Component\DependencyInjection\Definition $definition
* @param string $methodName
*/
public function assertDICDefinitionMethodNotCalled(Definition $definition, $methodName)
{
$callList = $definition->getMethodCalls();

foreach ($callList as $call) {
if ($call[0] === $methodName) {
$this->fail(
sprintf('Method "%s" is not expected to be called.', $methodName)
);
}
}
}
}

0 comments on commit 4ea09f9

Please # to comment.