diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index e4cbc54625..16b0be8180 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -239,7 +239,15 @@ class YamlFileLoader extends FileLoader } if (isset($service['tags'])) { + if (!is_array($service['tags'])) { + throw new \InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file)); + } + foreach ($service['tags'] as $tag) { + if (!isset($tag['name'])) { + throw new \InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key must be an array for service "%s" in %s.', $id, $file)); + } + $name = $tag['name']; unset($tag['name']); diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/badtag1.yml b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/badtag1.yml new file mode 100644 index 0000000000..af4af1f12b --- /dev/null +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/badtag1.yml @@ -0,0 +1,5 @@ +services: + foo_service: + class: FooClass + # tags is not an array + tags: string \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/badtag2.yml b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/badtag2.yml new file mode 100644 index 0000000000..269b6b6530 --- /dev/null +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/badtag2.yml @@ -0,0 +1,6 @@ +services: + foo_service: + class: FooClass + tags: + # tag is missing the name key + foo_tag: { foo: bar } \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Loader/YamlFileLoaderTest.php b/tests/Symfony/Tests/Component/DependencyInjection/Loader/YamlFileLoaderTest.php index a518c0ff43..ae0ff6bebf 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Loader/YamlFileLoaderTest.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/Loader/YamlFileLoaderTest.php @@ -166,6 +166,30 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase $interface = $interfaces['FooClass']; $this->assertTrue($interface->hasMethodCall('setBar'), '->load() parses interfaces elements'); } + + public function testNonArrayTagThrowsException() + { + $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml')); + try { + $loader->load('badtag1.yml'); + $this->fail('->load() should throw an exception when the tags key of a service is not an array'); + } catch (\Exception $e) { + $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tags key is not an array'); + $this->assertStringStartsWith('Parameter "tags" must be an array for service', $e->getMessage(), '->load() throws an InvalidArgumentException if the tags key is not an array'); + } + } + + public function testTagWithoutNameThrowsException() + { + $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml')); + try { + $loader->load('badtag2.yml'); + $this->fail('->load() should throw an exception when a tag is missing the name key'); + } catch (\Exception $e) { + $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is missing the name key'); + $this->assertStringStartsWith('A "tags" entry is missing a "name" key must be an array for service ', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is missing the name key'); + } + } } class ProjectLoader3 extends YamlFileLoader