[DI] Fix XmlFileLoader bad error message
This commit is contained in:
parent
bd0bf5240f
commit
be7afc6d85
|
@ -20,6 +20,31 @@ class XmlUtilsTest extends TestCase
|
|||
{
|
||||
$fixtures = __DIR__.'/../Fixtures/Util/';
|
||||
|
||||
try {
|
||||
XmlUtils::loadFile($fixtures);
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertStringContainsString('is not a file', $e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
XmlUtils::loadFile($fixtures.'non_existing.xml');
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertStringContainsString('is not a file', $e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
if ('\\' === \DIRECTORY_SEPARATOR) {
|
||||
$this->markTestSkipped('chmod is not supported on Windows');
|
||||
}
|
||||
chmod($fixtures.'not_readable.xml', 000);
|
||||
XmlUtils::loadFile($fixtures.'not_readable.xml');
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertStringContainsString('is not readable', $e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
XmlUtils::loadFile($fixtures.'invalid.xml');
|
||||
$this->fail();
|
||||
|
@ -165,7 +190,7 @@ class XmlUtilsTest extends TestCase
|
|||
$file = __DIR__.'/../Fixtures/foo.xml';
|
||||
|
||||
$this->expectException('InvalidArgumentException');
|
||||
$this->expectExceptionMessage(sprintf('File %s does not contain valid XML, it is empty.', $file));
|
||||
$this->expectExceptionMessage(sprintf('File "%s" does not contain valid XML, it is empty.', $file));
|
||||
|
||||
XmlUtils::loadFile($file);
|
||||
}
|
||||
|
@ -186,7 +211,7 @@ class XmlUtilsTest extends TestCase
|
|||
XmlUtils::loadFile($file);
|
||||
$this->fail('An exception should have been raised');
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertEquals(sprintf('File %s does not contain valid XML, it is empty.', $file), $e->getMessage());
|
||||
$this->assertEquals(sprintf('File "%s" does not contain valid XML, it is empty.', $file), $e->getMessage());
|
||||
}
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
|
|
|
@ -122,9 +122,18 @@ class XmlUtils
|
|||
*/
|
||||
public static function loadFile($file, $schemaOrCallable = null)
|
||||
{
|
||||
if (!is_file($file)) {
|
||||
throw new \InvalidArgumentException(sprintf('Resource "%s" is not a file.', $file));
|
||||
}
|
||||
|
||||
if (!is_readable($file)) {
|
||||
throw new \InvalidArgumentException(sprintf('File "%s" is not readable.', $file));
|
||||
}
|
||||
|
||||
$content = @file_get_contents($file);
|
||||
|
||||
if ('' === trim($content)) {
|
||||
throw new \InvalidArgumentException(sprintf('File %s does not contain valid XML, it is empty.', $file));
|
||||
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid XML, it is empty.', $file));
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
Reference in New Issue