Merge branch '2.7' into 2.8

* 2.7:
  backport GlobTest from 2.7 branch
  [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()
This commit is contained in:
Fabien Potencier 2016-02-02 15:46:44 +01:00
commit 2f4cdb92c1
15 changed files with 114 additions and 10 deletions

View File

@ -513,7 +513,7 @@ abstract class FrameworkExtensionTest extends TestCase
protected function createContainerFromFile($file, $data = array()) protected function createContainerFromFile($file, $data = array())
{ {
$cacheKey = md5($file.serialize($data)); $cacheKey = md5(get_class($this).$file.serialize($data));
if (isset(self::$containerCache[$cacheKey])) { if (isset(self::$containerCache[$cacheKey])) {
return self::$containerCache[$cacheKey]; return self::$containerCache[$cacheKey];
} }

View File

@ -28,7 +28,7 @@ class PhpFrameworkExtensionTest extends FrameworkExtensionTest
*/ */
public function testAssetsCannotHavePathAndUrl() public function testAssetsCannotHavePathAndUrl()
{ {
$container = $this->createContainerFromClosure(function ($container) { $this->createContainerFromClosure(function ($container) {
$container->loadFromExtension('framework', array( $container->loadFromExtension('framework', array(
'assets' => array( 'assets' => array(
'base_urls' => 'http://cdn.example.com', 'base_urls' => 'http://cdn.example.com',
@ -43,7 +43,7 @@ class PhpFrameworkExtensionTest extends FrameworkExtensionTest
*/ */
public function testAssetPackageCannotHavePathAndUrl() public function testAssetPackageCannotHavePathAndUrl()
{ {
$container = $this->createContainerFromClosure(function ($container) { $this->createContainerFromClosure(function ($container) {
$container->loadFromExtension('framework', array( $container->loadFromExtension('framework', array(
'assets' => array( 'assets' => array(
'packages' => array( 'packages' => array(

View File

@ -249,6 +249,10 @@ class XmlFileLoader extends FileLoader
$parameters[$name] = XmlUtils::phpize($node->nodeValue); $parameters[$name] = XmlUtils::phpize($node->nodeValue);
} }
if ('' === $tag->getAttribute('name')) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', (string) $service->getAttribute('id'), $file));
}
$definition->addTag($tag->getAttribute('name'), $parameters); $definition->addTag($tag->getAttribute('name'), $parameters);
} }

View File

@ -281,6 +281,10 @@ class YamlFileLoader extends FileLoader
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
} }
if (!is_string($tag['name']) || '' === $tag['name']) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
}
$name = $tag['name']; $name = $tag['name'];
unset($tag['name']); unset($tag['name']);

View File

@ -108,7 +108,7 @@
</xsd:complexType> </xsd:complexType>
<xsd:complexType name="tag"> <xsd:complexType name="tag">
<xsd:attribute name="name" type="xsd:string" /> <xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:anyAttribute namespace="##any" processContents="lax" /> <xsd:anyAttribute namespace="##any" processContents="lax" />
</xsd:complexType> </xsd:complexType>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="foo" class="BarClass">
<tag name="" foo="bar" />
</service>
</services>
</container>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="foo" class="BarClass">
<tag foo="bar" />
</service>
</services>
</container>

View File

@ -0,0 +1,6 @@
services:
foo_service:
class: FooClass
tags:
# tag name is an empty string
- { name: '', foo: bar }

View File

@ -0,0 +1,6 @@
services:
foo_service:
class: FooClass
tags:
# tag name is not a string
- { name: [], foo: bar }

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Loader;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@ -270,6 +271,27 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
} }
} }
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
*/
public function testParseTagsWithoutNameThrowsException()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('tag_without_name.xml');
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .* must be a non-empty string/
*/
public function testParseTagWithEmptyNameThrowsException()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('tag_with_empty_name.xml');
}
public function testConvertDomElementToArray() public function testConvertDomElementToArray()
{ {
$doc = new \DOMDocument('1.0'); $doc = new \DOMDocument('1.0');

View File

@ -289,6 +289,26 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader->load('bad_types1.yml'); $loader->load('bad_types1.yml');
} }
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .+ must be a non-empty string/
*/
public function testTagWithEmptyNameThrowsException()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('tag_name_empty_string.yml');
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageREgExp /The tag name for service "\.+" must be a non-empty string/
*/
public function testTagWithNonStringNameThrowsException()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('tag_name_no_string.yml');
}
/** /**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
*/ */

View File

@ -73,7 +73,7 @@ CHANGELOG
* moved CSRF implementation to the new Security CSRF sub-component * moved CSRF implementation to the new Security CSRF sub-component
* deprecated CsrfProviderInterface and its implementations * deprecated CsrfProviderInterface and its implementations
* deprecated options "csrf_provider" and "intention" in favor of the new options "csrf_token_generator" and "csrf_token_id" * deprecated options "csrf_provider" and "intention" in favor of the new options "csrf_token_manager" and "csrf_token_id"
2.3.0 2.3.0
----- -----

View File

@ -273,6 +273,26 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
$translator->trans('foo'); $translator->trans('foo');
} }
public function testFallbackCatalogueResources()
{
$translator = new Translator('en_GB', new MessageSelector());
$translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
$translator->addResource('yml', __DIR__.'/fixtures/empty.yml', 'en_GB');
$translator->addResource('yml', __DIR__.'/fixtures/resources.yml', 'en');
// force catalogue loading
$this->assertEquals('bar', $translator->trans('foo', array()));
$resources = $translator->getCatalogue('en')->getResources();
$this->assertCount(1, $resources);
$this->assertContains( __DIR__.'/fixtures/resources.yml', $resources);
$resources = $translator->getCatalogue('en_GB')->getResources();
$this->assertCount(2, $resources);
$this->assertContains( __DIR__.'/fixtures/empty.yml', $resources);
$this->assertContains( __DIR__.'/fixtures/resources.yml', $resources);
}
/** /**
* @dataProvider getTransTests * @dataProvider getTransTests
*/ */

View File

@ -428,6 +428,9 @@ EOF
} }
$fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all()); $fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
foreach ($this->catalogues[$fallback]->getResources() as $resource) {
$fallbackCatalogue->addResource($resource);
}
$current->addFallbackCatalogue($fallbackCatalogue); $current->addFallbackCatalogue($fallbackCatalogue);
$current = $fallbackCatalogue; $current = $fallbackCatalogue;
} }

View File

@ -21,10 +21,7 @@ use Symfony\Component\Yaml\Exception\ParseException;
class Yaml class Yaml
{ {
/** /**
* Parses YAML into a PHP array. * Parses YAML into a PHP value.
*
* The parse method, when supplied with a YAML stream (string or file),
* will do its best to convert YAML in a file into a PHP array.
* *
* Usage: * Usage:
* <code> * <code>
@ -43,7 +40,7 @@ class Yaml
* @param bool $objectSupport True if object support is enabled, false otherwise * @param bool $objectSupport True if object support is enabled, false otherwise
* @param bool $objectForMap True if maps should return a stdClass instead of array() * @param bool $objectForMap True if maps should return a stdClass instead of array()
* *
* @return array The YAML converted to a PHP array * @return mixed The YAML converted to a PHP value
* *
* @throws ParseException If the YAML is not valid * @throws ParseException If the YAML is not valid
*/ */