From be7afc6d85cae3d745a7fbc2af0c14b7870c80f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Bogusz?= Date: Thu, 5 Mar 2020 17:34:39 +0100 Subject: [PATCH] [DI] Fix XmlFileLoader bad error message --- .../Tests/Fixtures/Util/not_readable.xml | 0 .../Config/Tests/Util/XmlUtilsTest.php | 29 +++++++++++++++++-- .../Component/Config/Util/XmlUtils.php | 11 ++++++- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Config/Tests/Fixtures/Util/not_readable.xml diff --git a/src/Symfony/Component/Config/Tests/Fixtures/Util/not_readable.xml b/src/Symfony/Component/Config/Tests/Fixtures/Util/not_readable.xml new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php b/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php index d1a47a8ea337..0378e7b69a97 100644 --- a/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php +++ b/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php @@ -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(); @@ -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); } @@ -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(); diff --git a/src/Symfony/Component/Config/Util/XmlUtils.php b/src/Symfony/Component/Config/Util/XmlUtils.php index d0042af30226..c925f315e5ed 100644 --- a/src/Symfony/Component/Config/Util/XmlUtils.php +++ b/src/Symfony/Component/Config/Util/XmlUtils.php @@ -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 {