Skip to content

Commit

Permalink
[DI] Fix XmlFileLoader bad error message
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslaw-bogusz authored and fabpot committed Mar 6, 2020
1 parent bd0bf52 commit be7afc6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Empty file.
29 changes: 27 additions & 2 deletions src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php
Expand Up @@ -20,6 +20,31 @@ public function testLoadFile()
{
$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();
Expand Down Expand Up @@ -165,7 +190,7 @@ public function testLoadEmptyXmlFile()
$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);
}
Expand All @@ -186,7 +211,7 @@ public function testLoadWrongEmptyXMLWithErrorHandler()
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();
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Config/Util/XmlUtils.php
Expand Up @@ -122,9 +122,18 @@ public static function parse($content, $schemaOrCallable = null)
*/
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 {
Expand Down

0 comments on commit be7afc6

Please sign in to comment.