Skip to content

Commit

Permalink
added exception when a loaded YAML resource is not an array
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 12, 2010
1 parent 9944542 commit 48e3053
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 1 deletion.
Expand Up @@ -41,7 +41,8 @@ public function load($file)

$this->container->addResource(new FileResource($path));

if (!$content) {
// empty file
if (null === $content) {
return;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Expand Up @@ -42,6 +42,16 @@ public function load($file, $type = null)
$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;
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Translation/Loader/YamlFileLoader.php
Expand Up @@ -28,6 +28,16 @@ public function load($resource, $locale, $domain = 'messages')
{
$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));

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php
Expand Up @@ -32,6 +32,16 @@ public function loadClassMetadata(ClassMetadata $metadata)
$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()])) {
Expand Down
Empty file.
@@ -0,0 +1 @@
foo
Expand Up @@ -14,6 +14,7 @@
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
{
Expand All @@ -30,4 +31,22 @@ public function testSupports()
$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');
}
}
Expand Up @@ -26,4 +26,25 @@ public function testLoad()
$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');
}
}
Empty file.
@@ -0,0 +1 @@
foo
Expand Up @@ -16,6 +16,24 @@

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');
Expand Down
Empty file.
@@ -0,0 +1 @@
foo

0 comments on commit 48e3053

Please sign in to comment.