Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DI] Fix XmlFileLoader bad error message #35982

Merged
merged 1 commit into from Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
przemyslaw-bogusz marked this conversation as resolved.
Show resolved Hide resolved
$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