Skip to content

Commit

Permalink
[DependencyInjection] added tags validation when compiling a container
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 12, 2013
1 parent 3e370f6 commit 5cad478
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
Expand Up @@ -74,6 +74,17 @@ public function process(ContainerBuilder $container)
$id
));
}

// tag attribute values must be scalars
foreach ($definition->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
foreach ($attributes as $attribute => $value) {
if (!is_scalar($value)) {
throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
}
}
}
}
}
}
}
Expand Up @@ -225,7 +225,7 @@ private function parseDefinition($id, $service, $file)

foreach ($tag as $attribute => $value) {
if (!is_scalar($value)) {
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s.', $id, $name, $file));
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s.', $id, $name, $attribute, $file));
}
}

Expand Down
Expand Up @@ -18,7 +18,7 @@
class CheckDefinitionValidityPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function testProcessDetectsSyntheticNonPublicDefinitions()
{
Expand All @@ -29,7 +29,7 @@ public function testProcessDetectsSyntheticNonPublicDefinitions()
}

/**
* @expectedException \RuntimeException
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function testProcessDetectsSyntheticPrototypeDefinitions()
{
Expand All @@ -40,7 +40,7 @@ public function testProcessDetectsSyntheticPrototypeDefinitions()
}

/**
* @expectedException \RuntimeException
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function testProcessDetectsNonSyntheticNonAbstractDefinitionWithoutClass()
{
Expand All @@ -61,6 +61,17 @@ public function testProcess()
$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function testInvalidTags()
{
$container = new ContainerBuilder();
$container->register('a', 'class')->addTag('foo', array('bar' => array('baz' => 'baz')));

$this->process($container);
}

protected function process(ContainerBuilder $container)
{
$pass = new CheckDefinitionValidityPass();
Expand Down
Expand Up @@ -3,4 +3,4 @@ services:
class: FooClass
tags:
# tag-attribute is not a scalar
- { name: foo, foo: { foo: foo, bar: bar } }
- { name: foo, bar: { foo: foo, bar: bar } }
Expand Up @@ -198,7 +198,7 @@ public function testTagWithAttributeArrayThrowsException()
$this->fail('->load() should throw an exception when a tag-attribute is not a scalar');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
$this->assertStringStartsWith('A "tags" attribute must be of a scalar-type for service ', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
$this->assertStringStartsWith('A "tags" attribute must be of a scalar-type for service "foo_service", tag "foo", attribute "bar"', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag-attribute is not a scalar');
}
}
}

0 comments on commit 5cad478

Please sign in to comment.