Ver Fonte

added exception when a loaded YAML resource is not an array

Fabien Potencier há 14 anos atrás
pai
commit
48e30537c4

+ 2 - 1
src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

@@ -41,7 +41,8 @@ class YamlFileLoader extends FileLoader
 
         $this->container->addResource(new FileResource($path));
 
-        if (!$content) {
+        // empty file
+        if (null === $content) {
             return;
         }
 

+ 10 - 0
src/Symfony/Component/Routing/Loader/YamlFileLoader.php

@@ -42,6 +42,16 @@ class YamlFileLoader extends FileLoader
         $collection = new RouteCollection();
         $collection->addResource(new FileResource($path));
 
+        // empty file
+        if (null === $config) {
+            $config = array();
+        }
+
+        // not an array
+        if (!is_array($config)) {
+            throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file));
+        }
+
         foreach ($config as $name => $config) {
             if (isset($config['resource'])) {
                 $type = isset($config['type']) ? $config['type'] : null;

+ 10 - 0
src/Symfony/Component/Translation/Loader/YamlFileLoader.php

@@ -28,6 +28,16 @@ class YamlFileLoader extends ArrayLoader implements LoaderInterface
     {
         $messages = Yaml::load($resource);
 
+        // empty file
+        if (null === $messages) {
+            $messages = array();
+        }
+
+        // not an array
+        if (!is_array($messages)) {
+            throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $resource));
+        }
+
         $catalogue = parent::load($messages, $locale, $domain);
         $catalogue->addResource(new FileResource($resource));
 

+ 10 - 0
src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

@@ -32,6 +32,16 @@ class YamlFileLoader extends FileLoader
             $this->classes = Yaml::load($this->file);
         }
 
+        // empty file
+        if (null === $this->classes) {
+            return false;
+        }
+
+        // not an array
+        if (!is_array($this->classes)) {
+            throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
+        }
+
         // TODO validation
 
         if (isset($this->classes[$metadata->getClassName()])) {

+ 0 - 0
tests/Symfony/Tests/Component/Routing/Fixtures/empty.yml


+ 1 - 0
tests/Symfony/Tests/Component/Routing/Fixtures/nonvalid.yml

@@ -0,0 +1 @@
+foo

+ 19 - 0
tests/Symfony/Tests/Component/Routing/Loader/YamlFileLoaderTest.php

@@ -14,6 +14,7 @@ use Symfony\Component\Routing\Loader\LoaderResolver;
 use Symfony\Component\Routing\Loader\YamlFileLoader;
 use Symfony\Component\Routing\Route;
 use Symfony\Component\Routing\RouteCollection;
+use Symfony\Component\Routing\Resource\FileResource;
 
 class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
 {
@@ -30,4 +31,22 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
         $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
     }
+
+    public function testLoadDoesNothingIfEmpty()
+    {
+        $loader = new YamlFileLoader(array(__DIR__.'/../Fixtures'));
+        $collection = $loader->load('empty.yml');
+
+        $this->assertEquals(array(), $collection->all());
+        $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
+    }
+
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testLoadThrowsExceptionIfNotAnArray()
+    {
+        $loader = new YamlFileLoader(array(__DIR__.'/../Fixtures'));
+        $loader->load('nonvalid.yml');
+    }
 }

+ 21 - 0
tests/Symfony/Tests/Component/Translation/Loader/YamlFileLoaderTest.php

@@ -26,4 +26,25 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('en', $catalogue->getLocale());
         $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
     }
+
+    public function testLoadDoesNothingIfEmpty()
+    {
+        $loader = new YamlFileLoader();
+        $resource = __DIR__.'/../fixtures/empty.yml';
+        $catalogue = $loader->load($resource, 'en', 'domain1');
+
+        $this->assertEquals(array(), $catalogue->all('domain1'));
+        $this->assertEquals('en', $catalogue->getLocale());
+        $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
+    }
+
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testLoadThrowsAnExceptionIfNotAnArray()
+    {
+        $loader = new YamlFileLoader();
+        $resource = __DIR__.'/../fixtures/non-valid.yml';
+        $loader->load($resource, 'en', 'domain1');
+    }
 }

+ 0 - 0
tests/Symfony/Tests/Component/Translation/fixtures/empty.yml


+ 1 - 0
tests/Symfony/Tests/Component/Translation/fixtures/non-valid.yml

@@ -0,0 +1 @@
+foo

+ 18 - 0
tests/Symfony/Tests/Component/Validator/Mapping/Loader/YamlFileLoaderTest.php

@@ -16,6 +16,24 @@ use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
 
 class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
 {
+    public function testLoadClassMetadataReturnsFalseIfEmpty()
+    {
+        $loader = new YamlFileLoader(__DIR__.'/empty-mapping.yml');
+        $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
+
+        $this->assertFalse($loader->loadClassMetadata($metadata));
+    }
+
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testLoadClassMetadataThrowsExceptionIfNotAnArray()
+    {
+        $loader = new YamlFileLoader(__DIR__.'/nonvalid-mapping.yml');
+        $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
+        $loader->loadClassMetadata($metadata);
+    }
+
     public function testLoadClassMetadataReturnsTrueIfSuccessful()
     {
         $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');

+ 0 - 0
tests/Symfony/Tests/Component/Validator/Mapping/Loader/empty-mapping.yml


+ 1 - 0
tests/Symfony/Tests/Component/Validator/Mapping/Loader/nonvalid-mapping.yml

@@ -0,0 +1 @@
+foo