Skip to content

Commit

Permalink
Add reconfigure
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jul 20, 2023
1 parent ad6651f commit c451252
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
13 changes: 13 additions & 0 deletions src/Xml/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public function map(callable $mapper)
return $mapper($this->document);
}

/**
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws RuntimeException
*/
public function reconfigure(callable ... $configurators): self
{
return self::fromUnsafeDocument($this->document, ...$configurators);
}

/**
* @no-named-arguments
*/
Expand All @@ -174,6 +184,9 @@ public function traverse(Visitor ... $visitors): DOMNode
return $traverser->traverse($this->map(document_element()));
}

/**
* @return non-empty-string
*/
public function toXmlString(): string
{
return $this->map(xml_string());
Expand Down
5 changes: 3 additions & 2 deletions src/Xml/Dom/Mapper/xml_string.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use Closure;
use DOMNode;
use function Psl\Type\non_empty_string;
use function VeeWee\Xml\Dom\Locator\Node\detect_document;
use function VeeWee\Xml\Dom\Predicate\is_document;
use function VeeWee\Xml\ErrorHandling\disallow_issues;
use function VeeWee\Xml\ErrorHandling\disallow_libxml_false_returns;

/**
* @return \Closure(DOMNode): string
* @return \Closure(DOMNode): non-empty-string
*/
function xml_string(): Closure
{
Expand All @@ -22,7 +23,7 @@ static function () use ($node): string {
$node = is_document($node) ? null : $node;

return disallow_libxml_false_returns(
$document->saveXML($node),
non_empty_string()->assert($document->saveXML($node)),
'Unable to output XML as string'
);
}
Expand Down
31 changes: 22 additions & 9 deletions tests/Xml/Dom/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class DocumentTest extends TestCase
{
use FillFileTrait;


public function test_it_can_create_a_document_from_dom(): void
{
$document = new DOMDocument();
Expand All @@ -30,7 +30,7 @@ public function test_it_can_create_a_document_from_dom(): void
static::assertSame($document, $doc->toUnsafeDocument());
}


public function test_it_can_create_an_empty_document(): void
{
$document = new DOMDocument();
Expand All @@ -39,7 +39,7 @@ public function test_it_can_create_an_empty_document(): void
static::assertEquals($document, $doc->toUnsafeDocument());
}


public function test_it_can_create_a_configured_document(): void
{
$document = new DOMDocument();
Expand All @@ -48,7 +48,7 @@ public function test_it_can_create_a_configured_document(): void
static::assertEquals($document, $doc->toUnsafeDocument());
}


public function test_it_can_add_various_configurators(): void
{
$doc = Document::fromXmlString(
Expand All @@ -64,7 +64,7 @@ public function test_it_can_add_various_configurators(): void
static::assertXmlStringEqualsXmlString($xml, $doc->toXmlString());
}


public function test_it_can_create_a_document_from_xml_nod(): void
{
$source = new DOMDocument();
Expand All @@ -78,7 +78,7 @@ public function test_it_can_create_a_document_from_xml_nod(): void
static::assertXmlStringEqualsXmlString($xml, $doc->toXmlString());
}


public function test_it_can_create_a_document_from_xml_file(): void
{
[$file, $handle] = $this->fillFile($xml = '<hello />');
Expand All @@ -93,7 +93,7 @@ public function test_it_can_create_a_document_from_xml_file(): void
fclose($handle);
}


public function test_it_can_create_a_document_from_xml_string(): void
{
$doc = Document::fromXmlString(
Expand All @@ -104,7 +104,7 @@ public function test_it_can_create_a_document_from_xml_string(): void
static::assertXmlStringEqualsXmlString($xml, $doc->toXmlString());
}


public function test_it_can_map(): void
{
$doc = new DOMDocument();
Expand All @@ -113,7 +113,7 @@ public function test_it_can_map(): void

static::assertSame($mapped, $doc);
}

public function test_it_can_traverse(): void
{
$doc = Document::fromXmlString('<hello>world</hello>');
Expand All @@ -131,4 +131,17 @@ public function onNodeLeave(DOMNode $node): Action
static::assertXmlStringEqualsXmlString('<hello />', $doc->toXmlString());
static::assertSame($result, $doc->map(document_element()));
}

public function test_it_can_reconfigure_document(): void
{
$doc1 = Document::fromXmlString('<hello />');
$xml1 = $doc1->toXmlString();

$doc2 = $doc1->reconfigure(identity());
$xml2 = $doc2->toXmlString();

static::assertNotSame($doc1, $doc2);
static::assertSame($doc1->toUnsafeDocument(), $doc2->toUnsafeDocument());
static::assertXmlStringEqualsXmlString($xml1, $xml2);
}
}
2 changes: 1 addition & 1 deletion tests/Xml/Dom/Manipulator/Node/RenameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function test_it_can_rename_an_element_and_drop_prefix(): void
$result = rename($node, 'thing');

static::assertSame(
Document::fromUnsafeDocument($doc->map(comparable()))->toXmlString(),
$doc->reconfigure(comparable())->toXmlString(),
Document::fromXmlString(
'<hello><thing xmlns="http://ok" xmlns:a="http://ok" xmlns:whatever="http://whatever" a:foo="bar"/></hello>',
comparable()
Expand Down

0 comments on commit c451252

Please sign in to comment.