Skip to content

Commit

Permalink
Add tests for XML loader (and tweak its error messages)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed May 29, 2023
1 parent e0e14a6 commit fd9611c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/Util/Xml/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function loadFile(string $filename): DOMDocument
if ($contents === false) {
throw new XmlException(
sprintf(
'Could not read "%s".',
'Could not read XML from file "%s"',
$filename,
),
);
Expand All @@ -52,7 +52,16 @@ public function loadFile(string $filename): DOMDocument
public function load(string $actual, ?string $filename = null): DOMDocument
{
if ($actual === '') {
throw new XmlException('Could not load XML from empty string');
if ($filename === null) {
throw new XmlException('Could not parse XML from empty string');
}

throw new XmlException(
sprintf(
'Could not parse XML from empty file "%s"',
$filename,
),
);
}

$document = new DOMDocument;
Expand Down Expand Up @@ -94,9 +103,9 @@ public function load(string $actual, ?string $filename = null): DOMDocument
if ($filename !== null) {
throw new XmlException(
sprintf(
'Could not load "%s".%s',
'Could not load "%s"%s',
$filename,
$message !== '' ? "\n" . $message : '',
$message !== '' ? ":\n" . $message : '',
),
);
}
Expand Down
Empty file added tests/_files/empty.xml
Empty file.
1 change: 1 addition & 0 deletions tests/_files/invalid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<test>
50 changes: 48 additions & 2 deletions tests/unit/Util/Xml/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace PHPUnit\Util\Xml;

use DOMDocument;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
Expand All @@ -17,11 +18,56 @@
#[Small]
final class LoaderTest extends TestCase
{
public function testCannotLoadXmlFromEmptyString(): void
public function testCanParseFileWithValidXml(): void
{
$document = (new Loader)->loadFile(__DIR__ . '/../../../_files/configuration.xml');

$this->assertInstanceOf(DOMDocument::class, $document);
}

public function testCannotParseFileThatDoesNotExist(): void
{
$this->expectException(XmlException::class);
$this->expectExceptionMessage('Could not read XML from file "/does/not/exist.xml"');

(new Loader)->loadFile('/does/not/exist.xml');
}

public function testCannotParseEmptyFile(): void
{
$this->expectException(XmlException::class);

(new Loader)->loadFile(__DIR__ . '/../../../_files/empty.xml');
}

public function testCannotParseFileWithInvalidXml(): void
{
$this->expectException(XmlException::class);
$this->expectExceptionMessage('Could not load XML from empty string');
$this->expectExceptionMessage('Premature end of data in tag test line 1');

(new Loader)->loadFile(__DIR__ . '/../../../_files/invalid.xml');
}

public function testCanParseStringWithValidXml(): void
{
$document = (new Loader)->load('<test/>');

$this->assertInstanceOf(DOMDocument::class, $document);
}

public function testCannotParseEmptyString(): void
{
$this->expectException(XmlException::class);
$this->expectExceptionMessage('Could not parse XML from empty string');

(new Loader)->load('');
}

public function testCannotParseStringWithInvalidXml(): void
{
$this->expectException(XmlException::class);
$this->expectExceptionMessage('Premature end of data in tag test line 1');

(new Loader)->load('<test>');
}
}

0 comments on commit fd9611c

Please sign in to comment.