added exception when a loaded YAML resource is not an array

This commit is contained in:
Fabien Potencier 2010-12-12 08:39:37 +01:00
parent 9944542811
commit 48e30537c4
13 changed files with 93 additions and 1 deletions

View File

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

View File

@ -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;

View File

@ -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));

View File

@ -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()])) {

View File

@ -0,0 +1 @@
foo

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1 @@
foo

View File

@ -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');