-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow empty docs inside configurators like pretty_print
- Loading branch information
Showing
10 changed files
with
172 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Loader; | ||
|
||
use Closure; | ||
use Dom\XMLDocument; | ||
use function Psl\Type\non_empty_string; | ||
use function VeeWee\Xml\ErrorHandling\disallow_issues; | ||
|
||
/** | ||
* Loads a copy of current document. | ||
* | ||
* @param int $options - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php | ||
* @return Closure(): XMLDocument | ||
*/ | ||
function xml_document_loader( | ||
XMLDocument $importedDocument, | ||
int $options = 0, | ||
?string $override_encoding = null | ||
): Closure { | ||
return static fn () => disallow_issues(static function () use ($importedDocument, $options, $override_encoding): XMLDocument { | ||
|
||
if ($importedDocument->documentElement === null) { | ||
return XMLDocument::createEmpty($importedDocument->xmlVersion, $importedDocument->xmlEncoding); | ||
} | ||
|
||
return XMLDocument::createFromString( | ||
non_empty_string()->assert($importedDocument->saveXml()), | ||
$options, | ||
$override_encoding | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Loader; | ||
|
||
use Dom\XMLDocument; | ||
use PHPUnit\Framework\TestCase; | ||
use function VeeWee\Xml\Dom\Loader\xml_document_loader; | ||
|
||
final class XmlDocumentLoaderTest extends TestCase | ||
{ | ||
public function test_it_can_load_xml_string(): void | ||
{ | ||
$initialDoc = XMLDocument::createFromString('<hello />'); | ||
$loader = xml_document_loader($initialDoc); | ||
$doc = $loader(); | ||
|
||
static::assertXmlStringEqualsXmlString($initialDoc->saveXml(), $doc->saveXML()); | ||
} | ||
|
||
public function test_it_can_load_xml_string_with_different_charset(): void | ||
{ | ||
$initialDoc = XMLDocument::createFromString('<hello>héllo</hello>'); | ||
$loader = xml_document_loader($initialDoc, override_encoding: 'Windows-1252'); | ||
$doc = $loader(); | ||
|
||
static::assertNotSame($initialDoc, $doc); | ||
static::assertSame('héllo', $doc->documentElement->textContent); | ||
static::assertSame('Windows-1252', $doc->xmlEncoding); | ||
} | ||
|
||
public function test_it_can_load_with_options(): void | ||
{ | ||
$initialDoc = XMLDocument::createFromString('<hello><![CDATA[HELLO]]></hello>'); | ||
$loader = xml_document_loader($initialDoc, options: LIBXML_NOCDATA); | ||
$doc = $loader(); | ||
|
||
static::assertNotSame($initialDoc, $doc); | ||
static::assertSame('<hello>HELLO</hello>', $doc->saveXML($doc->documentElement)); | ||
} | ||
|
||
public function test_it_can_load_empty_xml_string(): void | ||
{ | ||
$initialDoc = XMLDocument::createEmpty(version: '1.1', encoding: 'ASCII'); | ||
$loader = xml_document_loader($initialDoc); | ||
$doc = $loader(); | ||
|
||
static::assertSame($initialDoc->saveXml(), $doc->saveXML()); | ||
static::assertSame('ASCII', $doc->xmlEncoding); | ||
static::assertSame('1.1', $doc->xmlVersion); | ||
} | ||
|
||
} |