Skip to content

Commit 96019ec

Browse files
jsamouhfabpot
authored andcommitted
[DI] [YamlFileLoader] change error message of a non existing file
1 parent d2c2c05 commit 96019ec

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ protected function loadFile($file)
319319
}
320320

321321
if (!file_exists($file)) {
322-
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
322+
throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
323323
}
324324

325325
if (null === $this->yamlParser) {

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,33 @@ public static function setUpBeforeClass()
3333
require_once self::$fixturesPath.'/includes/ProjectExtension.php';
3434
}
3535

36-
public function testLoadFile()
36+
/**
37+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
38+
* @expectedExceptionMessageRegExp /The file ".+" does not exist./
39+
*/
40+
public function testLoadUnExistFile()
3741
{
3842
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/ini'));
3943
$r = new \ReflectionObject($loader);
4044
$m = $r->getMethod('loadFile');
4145
$m->setAccessible(true);
4246

43-
try {
44-
$m->invoke($loader, 'foo.yml');
45-
$this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
46-
} catch (\Exception $e) {
47-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
48-
$this->assertEquals('The service file "foo.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
49-
}
50-
51-
try {
52-
$m->invoke($loader, 'parameters.ini');
53-
$this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
54-
} catch (\Exception $e) {
55-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file');
56-
$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');
57-
}
47+
$m->invoke($loader, 'foo.yml');
48+
}
5849

59-
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
50+
/**
51+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
52+
* @expectedExceptionMessageRegExp /The file ".+" does not contain valid YAML./
53+
*/
54+
public function testLoadInvalidYamlFile()
55+
{
56+
$path = self::$fixturesPath.'/ini';
57+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator($path));
58+
$r = new \ReflectionObject($loader);
59+
$m = $r->getMethod('loadFile');
60+
$m->setAccessible(true);
6061

61-
foreach (array('nonvalid1', 'nonvalid2') as $fixture) {
62-
try {
63-
$m->invoke($loader, $fixture.'.yml');
64-
$this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate');
65-
} catch (\Exception $e) {
66-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not validate');
67-
$this->assertStringMatchesFormat('The service file "nonvalid%d.yml" is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not validate');
68-
}
69-
}
62+
$m->invoke($loader, $path.'/parameters.ini');
7063
}
7164

7265
/**
@@ -90,6 +83,8 @@ public function provideInvalidFiles()
9083
array('bad_service'),
9184
array('bad_calls'),
9285
array('bad_format'),
86+
array('nonvalid1'),
87+
array('nonvalid2'),
9388
);
9489
}
9590

0 commit comments

Comments
 (0)