minor #22012 [DI] [YamlFileLoader] change error message of a non existing file (jordscream)

This PR was squashed before being merged into the 2.7 branch (closes #22012).

Discussion
----------

[DI] [YamlFileLoader] change error message of a non existing file

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22007
| License       | MIT
| Doc PR        | symfony/symfony-docs

This PR replaces the error message when non existing Yaml file is loaded. It gives more sense for the user.

Commits
-------

1c2ea97585 [DI] [YamlFileLoader] change error message of a non existing file
This commit is contained in:
Fabien Potencier 2017-03-20 07:02:55 -07:00
commit 2ba564d984
2 changed files with 22 additions and 27 deletions

View File

@ -319,7 +319,7 @@ class YamlFileLoader extends FileLoader
} }
if (!file_exists($file)) { if (!file_exists($file)) {
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file)); throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
} }
if (null === $this->yamlParser) { if (null === $this->yamlParser) {

View File

@ -33,40 +33,33 @@ class YamlFileLoaderTest extends TestCase
require_once self::$fixturesPath.'/includes/ProjectExtension.php'; require_once self::$fixturesPath.'/includes/ProjectExtension.php';
} }
public function testLoadFile() /**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /The file ".+" does not exist./
*/
public function testLoadUnExistFile()
{ {
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini')); $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
$r = new \ReflectionObject($loader); $r = new \ReflectionObject($loader);
$m = $r->getMethod('loadFile'); $m = $r->getMethod('loadFile');
$m->setAccessible(true); $m->setAccessible(true);
try { $m->invoke($loader, 'foo.yml');
$m->invoke($loader, 'foo.yml'); }
$this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
$this->assertEquals('The service file "foo.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
}
try { /**
$m->invoke($loader, 'parameters.ini'); * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
$this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file'); * @expectedExceptionMessageRegExp /The file ".+" does not contain valid YAML./
} catch (\Exception $e) { */
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file'); public function testLoadInvalidYamlFile()
$this->assertEquals('The service file "parameters.ini" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file'); {
} $path = self::$fixturesPath.'/ini';
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator($path));
$r = new \ReflectionObject($loader);
$m = $r->getMethod('loadFile');
$m->setAccessible(true);
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml')); $m->invoke($loader, $path.'/parameters.ini');
foreach (array('nonvalid1', 'nonvalid2') as $fixture) {
try {
$m->invoke($loader, $fixture.'.yml');
$this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not validate');
$this->assertStringMatchesFormat('The service file "nonvalid%d.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not validate');
}
}
} }
/** /**
@ -90,6 +83,8 @@ class YamlFileLoaderTest extends TestCase
array('bad_service'), array('bad_service'),
array('bad_calls'), array('bad_calls'),
array('bad_format'), array('bad_format'),
array('nonvalid1'),
array('nonvalid2'),
); );
} }