Skip to content

Commit 6fee4cc

Browse files
Fix exception message and handling of associative array of arguments
Fixes #8. @cordoval reported a failure when an arguments array has no numeric indexes. The fix includes: - Changing the exception message to be a bit clearer - Converting an associative array of arguments to a normal array
1 parent 61eb0c9 commit 6fee4cc

5 files changed

+46
-4
lines changed

DefinitionArgumentsValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public function validate(Definition $definition)
3434

3535
$arguments = $definition->getArguments();
3636

37-
$this->argumentsValidator->validate($constructor, $arguments);
37+
$this->argumentsValidator->validate($constructor, array_values($arguments));
3838
}
3939
}

Exception/MissingRequiredArgumentException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class MissingRequiredArgumentException extends \RuntimeException implements Defi
77
public function __construct($className, $parameterName)
88
{
99
parent::__construct(sprintf(
10-
'Definition has no argument for required parameter %s',
10+
'Definition for class %s has no argument for required parameter %s',
1111
$className,
1212
$parameterName
1313
));

Tests/DefinitionArgumentsValidatorTest.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public function ifNoConstructorCouldBeFoundSkipsValidation()
7373
*/
7474
public function ifConstructorIsFoundValidatesUsingArgumentsValidator()
7575
{
76-
$class = 'stdClass';
77-
$definition = new Definition($class);
76+
$definition = new Definition();
7877
$arguments = array(0 => 'argument1', 1 => 'argument2');
7978
$definition->setArguments($arguments);
8079

@@ -97,6 +96,36 @@ public function ifConstructorIsFoundValidatesUsingArgumentsValidator()
9796
$validator->validate($definition);
9897
}
9998

99+
/**
100+
* @test
101+
*/
102+
public function itConvertsAnAssociativeArrayOfArgumentsToANumericallyIndexedOrderedArray()
103+
{
104+
$definition = new Definition();
105+
$arguments = array('named_argument1' => 'argument1', 'named_argument2' => 'argument2');
106+
$definition->setArguments($arguments);
107+
108+
$numericallyIndexedArguments = array('argument1', 'argument2');
109+
110+
$constructorMethod = new \ReflectionMethod('Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\ClassWithConstructor', '__construct');
111+
$argumentsValidator = $this->createMockArgumentsValidator();
112+
$argumentsValidator
113+
->expects($this->once())
114+
->method('validate')
115+
->with($this->equalTo($constructorMethod), $this->identicalTo($numericallyIndexedArguments));
116+
117+
$constructorResolver = $this->createMockConstructorResolver();
118+
$constructorResolver
119+
->expects($this->once())
120+
->method('resolve')
121+
->with($definition)
122+
->will($this->returnValue($constructorMethod));
123+
124+
$validator = new DefinitionArgumentsValidator($constructorResolver, $argumentsValidator);
125+
126+
$validator->validate($definition);
127+
}
128+
100129
private function createMockConstructorResolver()
101130
{
102131
return $this->getMock('Matthias\SymfonyServiceDefinitionValidator\ConstructorResolverInterface');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
pdo:
3+
class: PDO
4+
arguments:
5+
dsn: "mysql:dbname=dbname;host=host"
6+
user: "user"
7+
password: "password"
8+
calls:
9+
- [setAttribute, [3, 2]]

Tests/Functional/FunctionalTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
99
use Symfony\Component\DependencyInjection\ContainerBuilder;
1010
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
11+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1112

1213
class FunctionalTest extends \PHPUnit_Framework_TestCase
1314
{
@@ -33,6 +34,9 @@ public function testIfTheServiceDefinitionsAreCorrectTheContainerWillBeCompiled(
3334
$loader = new XmlFileLoader($this->container, new FileLocator(__DIR__ . '/Fixtures'));
3435
$loader->load('correct_service_definitions.xml');
3536

37+
$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__ . '/Fixtures'));
38+
$loader->load('reported_problems.yml');
39+
3640
$this->container->compile();
3741
}
3842

0 commit comments

Comments
 (0)