Skip to content

Commit e9f375e

Browse files
Merge branch '2.7' into 2.8
* 2.7: [Console] Add missing `@require` annotation in test Fix merge [appveyor] Fix failure reporting [#17634] move DebugBundle license file backport GlobTest from 2.7 branch Move licenses according to new best practices [FrameworkBundle] Remove unused code in test [2.3] Fixed an undefined variable in Glob::toRegex simplified a test fix container cache key generation [Form] fix option name in changelog [Translation] Add resources from fallback locale [DependencyInjection] enforce tags to have a name [YAML] Refine the return value of Yaml::parse() Conflicts: src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
2 parents 1855866 + b3cc037 commit e9f375e

File tree

9 files changed

+85
-1
lines changed

9 files changed

+85
-1
lines changed

Loader/XmlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ private function parseDefinition(\DOMElement $service, $file)
249249
$parameters[$name] = XmlUtils::phpize($node->nodeValue);
250250
}
251251

252+
if ('' === $tag->getAttribute('name')) {
253+
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', (string) $service->getAttribute('id'), $file));
254+
}
255+
252256
$definition->addTag($tag->getAttribute('name'), $parameters);
253257
}
254258

Loader/YamlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ private function parseDefinition($id, $service, $file)
281281
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
282282
}
283283

284+
if (!is_string($tag['name']) || '' === $tag['name']) {
285+
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
286+
}
287+
284288
$name = $tag['name'];
285289
unset($tag['name']);
286290

Loader/schema/dic/services/services-1.0.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
</xsd:complexType>
109109

110110
<xsd:complexType name="tag">
111-
<xsd:attribute name="name" type="xsd:string" />
111+
<xsd:attribute name="name" type="xsd:string" use="required" />
112112
<xsd:anyAttribute namespace="##any" processContents="lax" />
113113
</xsd:complexType>
114114

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag name="" foo="bar" />
9+
</service>
10+
</services>
11+
</container>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag foo="bar" />
9+
</service>
10+
</services>
11+
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
foo_service:
3+
class: FooClass
4+
tags:
5+
# tag name is an empty string
6+
- { name: '', foo: bar }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
foo_service:
3+
class: FooClass
4+
tags:
5+
# tag name is not a string
6+
- { name: [], foo: bar }

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1819
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@@ -270,6 +271,27 @@ public function testParsesTags()
270271
}
271272
}
272273

274+
/**
275+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
276+
*/
277+
public function testParseTagsWithoutNameThrowsException()
278+
{
279+
$container = new ContainerBuilder();
280+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
281+
$loader->load('tag_without_name.xml');
282+
}
283+
284+
/**
285+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
286+
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .* must be a non-empty string/
287+
*/
288+
public function testParseTagWithEmptyNameThrowsException()
289+
{
290+
$container = new ContainerBuilder();
291+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
292+
$loader->load('tag_with_empty_name.xml');
293+
}
294+
273295
public function testConvertDomElementToArray()
274296
{
275297
$doc = new \DOMDocument('1.0');

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,26 @@ public function testLoadYamlOnlyWithKeys()
280280
$this->assertEquals(array('manager' => array(array('alias' => 'user'))), $definition->getTags());
281281
}
282282

283+
/**
284+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
285+
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .+ must be a non-empty string/
286+
*/
287+
public function testTagWithEmptyNameThrowsException()
288+
{
289+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
290+
$loader->load('tag_name_empty_string.yml');
291+
}
292+
293+
/**
294+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
295+
* @expectedExceptionMessageREgExp /The tag name for service "\.+" must be a non-empty string/
296+
*/
297+
public function testTagWithNonStringNameThrowsException()
298+
{
299+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
300+
$loader->load('tag_name_no_string.yml');
301+
}
302+
283303
/**
284304
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
285305
*/

0 commit comments

Comments
 (0)