diff --git a/src/Xml/Dom/Assert/assert_cdata.php b/src/Xml/Dom/Assert/assert_cdata.php index 21ca62af..b9c21673 100644 --- a/src/Xml/Dom/Assert/assert_cdata.php +++ b/src/Xml/Dom/Assert/assert_cdata.php @@ -4,15 +4,15 @@ namespace VeeWee\Xml\Dom\Assert; -use \DOM\CdataSection; +use \DOM\CDATASection; use Psl\Type\Exception\AssertException; use function Psl\Type\instance_of; /** - * @psalm-assert \DOM\CdataSection $node + * @psalm-assert \DOM\CDATASection $node * @throws AssertException */ -function assert_cdata(mixed $node): \DOM\CdataSection +function assert_cdata(mixed $node): \DOM\CDATASection { - return instance_of(\DOM\CdataSection::class)->assert($node); + return instance_of(\DOM\CDATASection::class)->assert($node); } diff --git a/src/Xml/Dom/Builder/cdata.php b/src/Xml/Dom/Builder/cdata.php index bcfd39bc..a8e20859 100644 --- a/src/Xml/Dom/Builder/cdata.php +++ b/src/Xml/Dom/Builder/cdata.php @@ -5,20 +5,20 @@ namespace VeeWee\Xml\Dom\Builder; use Closure; -use \DOM\CdataSection; +use \DOM\CDATASection; use \DOM\Node; use function VeeWee\Xml\Dom\Assert\assert_cdata; use function VeeWee\Xml\Dom\Locator\Node\detect_document; use function VeeWee\Xml\Internal\configure; /** - * @param list $configurators + * @param list $configurators * - * @return Closure(\DOM\Node): \DOM\CdataSection + * @return Closure(\DOM\Node): \DOM\CDATASection */ function cdata(string $data, ...$configurators): Closure { - return static function (\DOM\Node $node) use ($data, $configurators): \DOM\CdataSection { + return static function (\DOM\Node $node) use ($data, $configurators): \DOM\CDATASection { $document = detect_document($node); return assert_cdata( diff --git a/src/Xml/Dom/Builder/escaped_value.php b/src/Xml/Dom/Builder/escaped_value.php index 2d63b835..4f22482d 100644 --- a/src/Xml/Dom/Builder/escaped_value.php +++ b/src/Xml/Dom/Builder/escaped_value.php @@ -13,7 +13,8 @@ function escaped_value(string $value): Closure { return static function (\DOM\Element $node) use ($value): \DOM\Element { - $node->nodeValue = htmlspecialchars($value, ENT_XML1|ENT_QUOTES); + // TODO : Add entity subtitution through appendXML ? + $node->textContent = htmlspecialchars($value, ENT_XML1|ENT_QUOTES); return $node; }; diff --git a/src/Xml/Dom/Builder/value.php b/src/Xml/Dom/Builder/value.php index 48c3eb59..b0afa13a 100644 --- a/src/Xml/Dom/Builder/value.php +++ b/src/Xml/Dom/Builder/value.php @@ -13,7 +13,8 @@ function value(string $value): Closure { return static function (\DOM\Element $node) use ($value): \DOM\Element { - $node->nodeValue = $value; + // TODO : Add entity subtitution through appendXML ? + $node->textContent = $value; return $node; }; diff --git a/src/Xml/Dom/Collection/NodeList.php b/src/Xml/Dom/Collection/NodeList.php index 8ead96ad..fa8191e1 100644 --- a/src/Xml/Dom/Collection/NodeList.php +++ b/src/Xml/Dom/Collection/NodeList.php @@ -59,6 +59,15 @@ public static function empty(): self return new self(); } + /** + * @param \DOM\HTMLCollection $list + * @return NodeList<\DOM\Element> + */ + public static function fromDOMHTMLCollection(\DOM\HTMLCollection $list): self + { + return new self(...values($list->getIterator())); + } + /** * @template X of \DOM\Node * @param DOMNodeList $list diff --git a/src/Xml/Dom/Configurator/loader.php b/src/Xml/Dom/Configurator/loader.php deleted file mode 100644 index fe5bb569..00000000 --- a/src/Xml/Dom/Configurator/loader.php +++ /dev/null @@ -1,21 +0,0 @@ -preserveWhiteSpace = false; + // TODO : not fully implemented yet in the new API + //$document->preserveWhiteSpace = false; $document->formatOutput = true; return $document; diff --git a/src/Xml/Dom/Configurator/trim_spaces.php b/src/Xml/Dom/Configurator/trim_spaces.php index 60097497..9b23089a 100644 --- a/src/Xml/Dom/Configurator/trim_spaces.php +++ b/src/Xml/Dom/Configurator/trim_spaces.php @@ -13,7 +13,8 @@ function trim_spaces(): Closure { return static function (\DOM\XMLDocument $document): \DOM\XMLDocument { - $document->preserveWhiteSpace = false; + // TODO : not fully implemented yet in the new API + //$document->preserveWhiteSpace = false; $document->formatOutput = false; return $document; diff --git a/src/Xml/Dom/Configurator/utf8.php b/src/Xml/Dom/Configurator/utf8.php index 688aaf22..afdeaa2d 100644 --- a/src/Xml/Dom/Configurator/utf8.php +++ b/src/Xml/Dom/Configurator/utf8.php @@ -13,7 +13,7 @@ function utf8(): Closure { return static function (\DOM\XMLDocument $document): \DOM\XMLDocument { - $document->encoding = 'UTF-8'; + $document->charset = 'UTF-8'; return $document; }; diff --git a/src/Xml/Dom/Document.php b/src/Xml/Dom/Document.php index 61786d1c..bb6b0967 100644 --- a/src/Xml/Dom/Document.php +++ b/src/Xml/Dom/Document.php @@ -14,10 +14,7 @@ use VeeWee\Xml\ErrorHandling\Issue\IssueCollection; use VeeWee\Xml\Exception\RuntimeException; use function Psl\Vec\map; -use function VeeWee\Xml\Dom\Configurator\loader; -use function VeeWee\Xml\Dom\Loader\xml_file_loader; -use function VeeWee\Xml\Dom\Loader\xml_node_loader; -use function VeeWee\Xml\Dom\Loader\xml_string_loader; +use function VeeWee\Xml\Dom\Loader; use function VeeWee\Xml\Dom\Locator\document_element; use function VeeWee\Xml\Dom\Mapper\xml_string; use function VeeWee\Xml\Internal\configure; @@ -47,18 +44,28 @@ public static function configure(callable ... $configurators): self } /** + * @param callable(): XMLDocument $loader * @param list $configurators * * @throws RuntimeException */ - public static function fromXmlFile(string $file, callable ...$configurators): self + public static function fromLoader(callable $loader, callable ...$configurators): self { return new self( - configure(...$configurators)(XMLDocument::createFromFile($file)) - // TODO : What with loader(xml_file_loader($file)) + configure(...$configurators)($loader()) ); } + /** + * @param list $configurators + * + * @throws RuntimeException + */ + public static function fromXmlFile(string $file, callable ...$configurators): self + { + return self::fromLoader(Loader\xml_file_loader($file), ...$configurators); + } + /** * @param non-empty-string $xml * @param list $configurators @@ -67,10 +74,7 @@ public static function fromXmlFile(string $file, callable ...$configurators): se */ public static function fromXmlString(string $xml, callable ...$configurators): self { - return new self( - configure(...$configurators)(XMLDocument::createFromString($xml)) - // TODO : What with loader(xml_string_loader($file)) - ); + return self::fromLoader(Loader\xml_string_loader($xml), ...$configurators); } /** @@ -80,10 +84,7 @@ public static function fromXmlString(string $xml, callable ...$configurators): s */ public static function fromXmlNode(\DOM\Node $node, callable ...$configurators): self { - return self::configure( - loader(xml_node_loader($node)), - ...$configurators - ); + return self::fromLoader(Loader\xml_node_loader($node), ...$configurators); } /** @@ -93,9 +94,7 @@ public static function fromXmlNode(\DOM\Node $node, callable ...$configurators): */ public static function fromUnsafeDocument(XMLDocument $document, callable ...$configurators): self { - return new self( - configure(...$configurators)($document) - ); + return self::fromLoader(static fn () => $document, ...$configurators); } public function toUnsafeDocument(): XMLDocument diff --git a/src/Xml/Dom/Loader/Loader.php b/src/Xml/Dom/Loader/Loader.php index 98b1e5e1..f0c34c9a 100644 --- a/src/Xml/Dom/Loader/Loader.php +++ b/src/Xml/Dom/Loader/Loader.php @@ -8,5 +8,5 @@ interface Loader { - public function __invoke(\DOM\XMLDocument $document): void; + public function __invoke(): XMLDocument; } diff --git a/src/Xml/Dom/Loader/load.php b/src/Xml/Dom/Loader/load.php deleted file mode 100644 index 8f1bc645..00000000 --- a/src/Xml/Dom/Loader/load.php +++ /dev/null @@ -1,25 +0,0 @@ -load($file, $options); - } - ); - }; + return static fn () => disallow_issues(static function () use ($file, $options): XMLDocument { + Assert::fileExists($file); + + return XMLDocument::createFromFile($file, $options); + }); } diff --git a/src/Xml/Dom/Loader/xml_node_loader.php b/src/Xml/Dom/Loader/xml_node_loader.php index 7a5f1ef1..9222cd1a 100644 --- a/src/Xml/Dom/Loader/xml_node_loader.php +++ b/src/Xml/Dom/Loader/xml_node_loader.php @@ -6,16 +6,20 @@ use Closure; use \DOM\XMLDocument; - use \DOM\Node; use function VeeWee\Xml\Dom\Manipulator\Node\append_external_node; +use function VeeWee\Xml\ErrorHandling\disallow_issues; /** - * @return Closure(\DOM\XMLDocument): void + * @return Closure(): XMLDocument */ function xml_node_loader(\DOM\Node $importedNode): Closure { - return static function (\DOM\XMLDocument $document) use ($importedNode): void { - load(static fn (): bool => (bool) append_external_node($document, $importedNode)); - }; + return static fn () => disallow_issues(static function () use ($importedNode): XMLDocument { + $document = XMLDocument::createEmpty(); + + append_external_node($document, $importedNode); + + return $document; + }); } diff --git a/src/Xml/Dom/Loader/xml_string_loader.php b/src/Xml/Dom/Loader/xml_string_loader.php index 8191be2a..c7dd0977 100644 --- a/src/Xml/Dom/Loader/xml_string_loader.php +++ b/src/Xml/Dom/Loader/xml_string_loader.php @@ -6,15 +6,16 @@ use Closure; use \DOM\XMLDocument; +use function VeeWee\Xml\ErrorHandling\disallow_issues; /** * @param non-empty-string $xml * @param int $options - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php - * @return Closure(\DOM\XMLDocument): void + * @return Closure(): XMLDocument */ function xml_string_loader(string $xml, int $options = 0): Closure { - return static function (\DOM\XMLDocument $document) use ($xml, $options): void { - load(static fn (): bool => $document->loadXML($xml, $options)); - }; + return static fn () => disallow_issues(static function () use ($xml, $options): XMLDocument { + return XMLDocument::createFromString($xml, $options); + }); } diff --git a/src/Xml/Dom/Locator/Element/locate_by_namespaced_tag_name.php b/src/Xml/Dom/Locator/Element/locate_by_namespaced_tag_name.php index 5124bb5f..55dd2218 100644 --- a/src/Xml/Dom/Locator/Element/locate_by_namespaced_tag_name.php +++ b/src/Xml/Dom/Locator/Element/locate_by_namespaced_tag_name.php @@ -12,5 +12,5 @@ */ function locate_by_namespaced_tag_name(\DOM\Element $node, string $namespace, string $localTagName): NodeList { - return NodeList::fromDOMNodeList($node->getElementsByTagNameNS($namespace, $localTagName)); + return NodeList::fromDOMHTMLCollection($node->getElementsByTagNameNS($namespace, $localTagName)); } diff --git a/src/Xml/Dom/Locator/Element/locate_by_tag_name.php b/src/Xml/Dom/Locator/Element/locate_by_tag_name.php index b134ca20..f299118b 100644 --- a/src/Xml/Dom/Locator/Element/locate_by_tag_name.php +++ b/src/Xml/Dom/Locator/Element/locate_by_tag_name.php @@ -12,5 +12,5 @@ */ function locate_by_tag_name(\DOM\Element $node, string $tag): NodeList { - return NodeList::fromDOMNodeList($node->getElementsByTagName($tag)); + return NodeList::fromDOMHTMLCollection($node->getElementsByTagName($tag)); } diff --git a/src/Xml/Dom/Locator/Node/detect_document.php b/src/Xml/Dom/Locator/Node/detect_document.php index 65112509..86a5c7a5 100644 --- a/src/Xml/Dom/Locator/Node/detect_document.php +++ b/src/Xml/Dom/Locator/Node/detect_document.php @@ -16,8 +16,5 @@ */ function detect_document(\DOM\Node $node): \DOM\XMLDocument { - $document = is_document($node) ? $node : $node->ownerDocument; - Assert::notNull($document, 'Expected to find an ownerDocument on provided \DOM\Node.'); - - return $document; + return is_document($node) ? $node : $node->ownerDocument; } diff --git a/src/Xml/Dom/Locator/Node/value.php b/src/Xml/Dom/Locator/Node/value.php index dc51ad80..c82e77d6 100644 --- a/src/Xml/Dom/Locator/Node/value.php +++ b/src/Xml/Dom/Locator/Node/value.php @@ -19,5 +19,8 @@ */ function value(\DOM\Node $node, TypeInterface $type) { - return $type->coerce($node->nodeValue); + // TODO : nodeValue did entity substitution + // TODO : nodeValue returns null for elements + // TODO : How to best deal with this? + return $type->coerce($node->textContent ?? ''); } diff --git a/src/Xml/Dom/Manipulator/Element/rename.php b/src/Xml/Dom/Manipulator/Element/rename.php index ae6e8aa1..dca0f633 100644 --- a/src/Xml/Dom/Manipulator/Element/rename.php +++ b/src/Xml/Dom/Manipulator/Element/rename.php @@ -4,8 +4,6 @@ namespace VeeWee\Xml\Dom\Manipulator\Element; -use \DOM\Attr; -use \DOM\Element; use \DOM\NameSpaceNode; use VeeWee\Xml\Exception\RuntimeException; use function VeeWee\Xml\Dom\Builder\element; @@ -44,7 +42,8 @@ static function (\DOM\NameSpaceNode $attribute) use ($target, $newElement): void ); attributes_list($target)->forEach( - static function (\DOM\Attr $attribute) use ($newElement): void { + static function (\DOM\Attr $attribute) use ($target, $newElement): void { + $target->removeAttributeNode($attribute); $newElement->setAttributeNode($attribute); } ); diff --git a/src/Xml/Dom/Mapper/xslt_template.php b/src/Xml/Dom/Mapper/xslt_template.php index 9862e390..8600b182 100644 --- a/src/Xml/Dom/Mapper/xslt_template.php +++ b/src/Xml/Dom/Mapper/xslt_template.php @@ -5,19 +5,19 @@ namespace VeeWee\Xml\Dom\Mapper; use Closure; -use DOMDocument as DOMDocument; -use VeeWee\XmlDOMDocument; +use VeeWee\Xml\Dom\Document; +use DOM\XMLDocument; use VeeWee\Xml\Xslt\Processor; use XSLTProcessor; /** * @param list $configurators - * @return Closure(DOMDocument): string + * @return Closure(XMLDocument): string */ function xslt_template(Document $template, callable ... $configurators): Closure { - return static fn (DOMDocument $document): string - => Processor::fromTemplateDocument($template, ...$configurators)->transformDocumentToString( + return static fn (XMLDocument $document): string => + Processor::fromTemplateDocument($template, ...$configurators)->transformDocumentToString( Document::fromUnsafeDocument($document) ); } diff --git a/src/Xml/Dom/Predicate/is_cdata.php b/src/Xml/Dom/Predicate/is_cdata.php index 60b7ac29..3fbd1ef6 100644 --- a/src/Xml/Dom/Predicate/is_cdata.php +++ b/src/Xml/Dom/Predicate/is_cdata.php @@ -4,13 +4,13 @@ namespace VeeWee\Xml\Dom\Predicate; -use \DOM\CdataSection; +use \DOM\CDATASection; use \DOM\Node; /** - * @psalm-assert-if-true \DOM\CdataSection $node + * @psalm-assert-if-true \DOM\CDATASection $node */ function is_cdata(\DOM\Node $node): bool { - return $node instanceof \DOM\CdataSection; + return $node instanceof \DOM\CDATASection; } diff --git a/src/Xml/Dom/Traverser/Visitor/SortAttributes.php b/src/Xml/Dom/Traverser/Visitor/SortAttributes.php index 20b02ce3..4dfa6ca1 100644 --- a/src/Xml/Dom/Traverser/Visitor/SortAttributes.php +++ b/src/Xml/Dom/Traverser/Visitor/SortAttributes.php @@ -4,12 +4,10 @@ namespace VeeWee\Xml\Dom\Traverser\Visitor; -use \DOM\Attr; -use \DOM\Node; use VeeWee\Xml\Dom\Traverser\Action; use function VeeWee\Xml\Dom\Locator\Attribute\attributes_list; -use function VeeWee\Xml\Dom\Manipulator\append; use function VeeWee\Xml\Dom\Predicate\is_element; +use function VeeWee\Xml\ErrorHandling\disallow_issues; final class SortAttributes extends AbstractVisitor { @@ -23,7 +21,8 @@ public function onNodeEnter(\DOM\Node $node): Action ->sort(static fn (\DOM\Attr $a, \DOM\Attr $b): int => $a->nodeName <=> $b->nodeName) ->forEach( static function (\DOM\Attr $attr) use ($node): void { - append($attr)($node); + // TODO : move login into Manipulator\append() + disallow_issues(static fn () => $node->setAttributeNode($attr)); } ); diff --git a/src/Xml/Xslt/Configurator/functions.php b/src/Xml/Xslt/Configurator/functions.php index cb6e1175..210b1705 100644 --- a/src/Xml/Xslt/Configurator/functions.php +++ b/src/Xml/Xslt/Configurator/functions.php @@ -8,6 +8,8 @@ use XSLTProcessor; /** + * TODO : Add support for callables : https://wiki.php.net/rfc/improve_callbacks_dom_and_xsl (either here or through a separate configurator) + * * @param non-empty-list $functions * * @return Closure(XSLTProcessor): XSLTProcessor diff --git a/src/bootstrap.php b/src/bootstrap.php index a6e0898a..ce099e05 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -24,7 +24,6 @@ 'Xml\Dom\Configurator\canonicalize' => __DIR__.'/Xml/Dom/Configurator/canonicalize.php', 'Xml\Dom\Configurator\comparable' => __DIR__.'/Xml/Dom/Configurator/comparable.php', 'Xml\Dom\Configurator\document_uri' => __DIR__.'/Xml/Dom/Configurator/document_uri.php', - 'Xml\Dom\Configurator\loader' => __DIR__.'/Xml/Dom/Configurator/loader.php', 'Xml\Dom\Configurator\normalize' => __DIR__.'/Xml/Dom/Configurator/normalize.php', 'Xml\Dom\Configurator\optimize_namespaces' => __DIR__.'/Xml/Dom/Configurator/optimize_namespaces.php', 'Xml\Dom\Configurator\pretty_print' => __DIR__.'/Xml/Dom/Configurator/pretty_print.php', @@ -32,7 +31,6 @@ 'Xml\Dom\Configurator\trim_spaces' => __DIR__.'/Xml/Dom/Configurator/trim_spaces.php', 'Xml\Dom\Configurator\utf8' => __DIR__.'/Xml/Dom/Configurator/utf8.php', 'Xml\Dom\Configurator\validator' => __DIR__.'/Xml/Dom/Configurator/validator.php', - 'Xml\Dom\Loader\load' => __DIR__.'/Xml/Dom/Loader/load.php', 'Xml\Dom\Loader\xml_file_loader' => __DIR__.'/Xml/Dom/Loader/xml_file_loader.php', 'Xml\Dom\Loader\xml_node_loader' => __DIR__.'/Xml/Dom/Loader/xml_node_loader.php', 'Xml\Dom\Loader\xml_string_loader' => __DIR__.'/Xml/Dom/Loader/xml_string_loader.php', diff --git a/stubs/DOM.phpstub b/stubs/DOM.phpstub index 520be9c2..9fe7fc46 100644 --- a/stubs/DOM.phpstub +++ b/stubs/DOM.phpstub @@ -1,11 +1,1661 @@ toUnsafeDocument(); $node = cdata($data = 'hello')($doc); - static::assertInstanceOf(\DOM\CdataSection::class, $node); + static::assertInstanceOf(\DOM\CDATASection::class, $node); static::assertSame($data, $node->textContent); static::assertSame(xml_string()($node), ''); } @@ -29,7 +29,7 @@ public function test_it_can_build_cdata_with_configurators(): void $doc = Document::empty()->toUnsafeDocument(); $node = cdata($data = 'hello', identity())($doc); - static::assertInstanceOf(\DOM\CdataSection::class, $node); + static::assertInstanceOf(\DOM\CDATASection::class, $node); static::assertSame($data, $node->textContent); } } diff --git a/tests/Xml/Dom/Builder/ElementTest.php b/tests/Xml/Dom/Builder/ElementTest.php index c1629a3e..bd6d9c9a 100644 --- a/tests/Xml/Dom/Builder/ElementTest.php +++ b/tests/Xml/Dom/Builder/ElementTest.php @@ -40,7 +40,7 @@ public function test_it_can_build_an_element_with_value(): void static::assertSame('hello', $node->nodeName); static::assertSame('hello', $node->localName); - static::assertSame('world', $node->nodeValue); + static::assertSame('world', $node->textContent); static::assertSame($doc, $node->ownerDocument); } } diff --git a/tests/Xml/Dom/Builder/EscapedValueTest.php b/tests/Xml/Dom/Builder/EscapedValueTest.php index 96957b50..c4efa448 100644 --- a/tests/Xml/Dom/Builder/EscapedValueTest.php +++ b/tests/Xml/Dom/Builder/EscapedValueTest.php @@ -18,7 +18,7 @@ public function test_it_can_build_an_element_with_html_value(): void $doc = Document::empty()->toUnsafeDocument(); $node = element('hello', escaped_value(''))($doc); - static::assertSame('', $node->nodeValue); + static::assertSame('', $node->textContent); // TODO : nodeValue did entity substitution static::assertSame(xml_string()($node), '<wor " ld>'); } } diff --git a/tests/Xml/Dom/Builder/ValueTest.php b/tests/Xml/Dom/Builder/ValueTest.php index 70f679b5..00453eaa 100644 --- a/tests/Xml/Dom/Builder/ValueTest.php +++ b/tests/Xml/Dom/Builder/ValueTest.php @@ -18,7 +18,7 @@ public function test_it_can_build_an_element_with_html_value(): void $doc = Document::empty()->toUnsafeDocument(); $node = element('hello', value(''))($doc); - static::assertSame('', $node->nodeValue); + static::assertSame('', $node->textContent); static::assertSame(xml_string()($node), '<world>'); } } diff --git a/tests/Xml/Dom/Collection/NodeListTest.php b/tests/Xml/Dom/Collection/NodeListTest.php index 92a6aeb0..c1dd517d 100644 --- a/tests/Xml/Dom/Collection/NodeListTest.php +++ b/tests/Xml/Dom/Collection/NodeListTest.php @@ -120,7 +120,7 @@ public function test_it_can_expect_items() public function test_it_can_map(): void { $items = $this->loadProducts()->map( - static fn (\DOM\Element $element): string => $element->nodeValue + static fn (\DOM\Element $element): string => $element->textContent ); static::assertSame( @@ -139,7 +139,7 @@ public function test_it_can_loop_over_all_items(): void $x = new MutableVector([]); $this->loadProducts()->forEach( static function (\DOM\Element $element) use ($x) : void { - $x->add($element->nodeValue); + $x->add($element->textContent); } ); @@ -166,11 +166,11 @@ public function test_it_can_filter(): void static::assertSame($all->item(3), $filtered->item(1)); } - + public function test_it_can_reduce(): void { $total = $this->loadPrices()->reduce( - static fn (int $total, \DOM\Element $element): int => $total + (int) $element->nodeValue, + static fn (int $total, \DOM\Element $element): int => $total + (int) $element->textContent, 0 ); @@ -189,7 +189,7 @@ public function test_it_can_detect(): void static::assertCount(2, $list); } - + public function test_it_can_query_xpath(): void { $items = $this->root()->query('./prices/price', identity()); @@ -199,7 +199,7 @@ public function test_it_can_query_xpath(): void } } - + public function test_it_can_evaluate_xpath(): void { $evaluated = $this->loadPrices()->evaluate('number(.)', Type\int(), identity()); @@ -217,7 +217,7 @@ public function test_it_can_equal(): void static::assertSame([$prices->item(3)], [...$prices->eq(3)]); static::assertSame([], [...$prices->eq(4)]); } - + public function test_it_can_get_first(): void { $prices = $this->loadPrices(); @@ -282,7 +282,7 @@ public function test_it_can_expect_last(): void $this->expectExceptionMessage('No last item was found'); NodeList::empty()->expectFirst('No last item was found'); } - + public function test_it_can_search_ancestors(): void { $prices = $this->loadPrices(); @@ -331,7 +331,7 @@ public function test_it_can_validate_types_and_fail(): void public function test_it_can_sort(): void { $prices = $this->loadPrices(); - $sorted = $prices->sort(static fn (\DOM\Node $a, \DOM\Node $b) => $b->nodeValue <=> $a->nodeValue); + $sorted = $prices->sort(static fn (\DOM\Node $a, \DOM\Node $b) => $b->textContent <=> $a->textContent); static::assertSame( [ diff --git a/tests/Xml/Dom/Configurator/LoaderTest.php b/tests/Xml/Dom/Configurator/LoaderTest.php deleted file mode 100644 index 788cd87e..00000000 --- a/tests/Xml/Dom/Configurator/LoaderTest.php +++ /dev/null @@ -1,41 +0,0 @@ -toUnsafeDocument(); - $xml = ''; - - $loader = loader(static function (\DOM\XMLDocument $doc) use ($xml): void { - $doc->loadXML($xml); - }); - - $result = $loader($doc); - static::assertSame($doc, $result); - static::assertXmlStringEqualsXmlString($xml, $doc->saveXML()); - } - - - public function test_it_can_mark_xml_loading_as_failed(): void - { - $doc = Document::empty()->toUnsafeDocument(); - $exception = new Exception('Could not load the XML document'); - $loader = loader(static function (\DOM\XMLDocument $doc) use ($exception): void { - throw $exception; - }); - - $this->expectExceptionObject($exception); - $loader($doc); - } -} diff --git a/tests/Xml/Dom/Configurator/PrettyPrintTest.php b/tests/Xml/Dom/Configurator/PrettyPrintTest.php index 79ed80c3..29337608 100644 --- a/tests/Xml/Dom/Configurator/PrettyPrintTest.php +++ b/tests/Xml/Dom/Configurator/PrettyPrintTest.php @@ -26,7 +26,7 @@ public function test_it_can_trim_contents(): void EOXML; static::assertSame($doc, $result); - static::assertFalse($doc->preserveWhiteSpace); + // TODO : static::assertFalse($doc->preserveWhiteSpace); static::assertTrue($doc->formatOutput); static::assertSame($expected, xml_string()($doc->documentElement)); } diff --git a/tests/Xml/Dom/Configurator/TrimSpacesTest.php b/tests/Xml/Dom/Configurator/TrimSpacesTest.php index f12fff13..9fd1e130 100644 --- a/tests/Xml/Dom/Configurator/TrimSpacesTest.php +++ b/tests/Xml/Dom/Configurator/TrimSpacesTest.php @@ -19,7 +19,7 @@ public function test_it_can_trim_contents(): void $result = $configurator($doc); static::assertSame($doc, $result); - static::assertFalse($doc->preserveWhiteSpace); + // TODO : static::assertFalse($doc->preserveWhiteSpace); static::assertFalse($doc->formatOutput); static::assertSame('', xml_string()($doc->documentElement)); } diff --git a/tests/Xml/Dom/Configurator/Utf8Test.php b/tests/Xml/Dom/Configurator/Utf8Test.php index ac45857a..de470e6f 100644 --- a/tests/Xml/Dom/Configurator/Utf8Test.php +++ b/tests/Xml/Dom/Configurator/Utf8Test.php @@ -19,6 +19,6 @@ public function test_it_can_convert_to_utf8(): void $result = $configurator($doc); static::assertSame($doc, $result); - static::assertSame('UTF-8', $doc->encoding); + static::assertSame('UTF-8', $doc->charset); } } diff --git a/tests/Xml/Dom/Loader/XmlFileLoaderTest.php b/tests/Xml/Dom/Loader/XmlFileLoaderTest.php index 9f3f68d8..8dd63a00 100644 --- a/tests/Xml/Dom/Loader/XmlFileLoaderTest.php +++ b/tests/Xml/Dom/Loader/XmlFileLoaderTest.php @@ -16,12 +16,11 @@ final class XmlFileLoaderTest extends TestCase public function test_it_can_load_xml_file(): void { - $doc = new \DOM\XMLDocument(); $xml = ''; [$file, $handle] = $this->fillFile($xml); $loader = xml_file_loader($file); - $loader($doc); + $doc = $loader(); fclose($handle); static::assertXmlStringEqualsXmlString($xml, $doc->saveXML()); @@ -29,39 +28,36 @@ public function test_it_can_load_xml_file(): void public function test_it_can_load_with_options(): void { - $doc = new \DOM\XMLDocument(); $xml = ''; [$file, $handle] = $this->fillFile($xml); $loader = xml_file_loader($file, LIBXML_NOCDATA); - $loader($doc); + $doc = $loader(); fclose($handle); static::assertSame('HELLO', $doc->saveXML($doc->documentElement)); } - + public function test_it_cannot_load_invalid_xml_file(): void { - $doc = new \DOM\XMLDocument(); $xml = 'fillFile($xml); $loader = xml_file_loader($file); $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Could not load the DOM Document'); + $this->expectExceptionMessage('XML document is malformed'); - $loader($doc); + $doc = $loader(); fclose($handle); } public function test_it_throws_exception_on_invalid_file(): void { - $doc = new \DOM\XMLDocument(); $loader = xml_file_loader('invalid-file'); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('The file "invalid-file" does not exist'); - $loader($doc); + $loader(); } } diff --git a/tests/Xml/Dom/Loader/XmlNodeLoaderTest.php b/tests/Xml/Dom/Loader/XmlNodeLoaderTest.php index c0a115e3..ca3cc5a4 100644 --- a/tests/Xml/Dom/Loader/XmlNodeLoaderTest.php +++ b/tests/Xml/Dom/Loader/XmlNodeLoaderTest.php @@ -15,11 +15,10 @@ final class XmlNodeLoaderTest extends TestCase public function test_it_can_load_xml_node(): void { $source = Document::fromXmlString($xml = '')->toUnsafeDocument(); - $doc = Document::empty()->toUnsafeDocument(); $loader = xml_node_loader($source->documentElement); - $loader($doc); + $doc = $loader(); static::assertXmlStringEqualsXmlString($xml, $doc->saveXML()); } @@ -27,13 +26,12 @@ public function test_it_can_load_xml_node(): void public function test_it_can_not_load_invalid_xml_node(): void { $source = Document::fromXmlString($xml = '')->toUnsafeDocument(); - $doc = Document::empty()->toUnsafeDocument(); $loader = xml_node_loader($source); $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Cannot import node: Node Type Not Supported'); + $this->expectExceptionMessage('Not Supported Error'); - $loader($doc); + $loader(); } } diff --git a/tests/Xml/Dom/Loader/XmlStringLoaderTest.php b/tests/Xml/Dom/Loader/XmlStringLoaderTest.php index ebed9148..8233ce03 100644 --- a/tests/Xml/Dom/Loader/XmlStringLoaderTest.php +++ b/tests/Xml/Dom/Loader/XmlStringLoaderTest.php @@ -13,33 +13,30 @@ final class XmlStringLoaderTest extends TestCase { public function test_it_can_load_xml_string(): void { - $doc = new \DOM\XMLDocument(); $xml = ''; $loader = xml_string_loader($xml); + $doc = $loader(); - $loader($doc); static::assertXmlStringEqualsXmlString($xml, $doc->saveXML()); } public function test_it_can_not_load_invalid_xml_string(): void { - $doc = new \DOM\XMLDocument(); $xml = 'expectException(RuntimeException::class); - $this->expectExceptionMessage('Could not load the DOM Document'); + $this->expectExceptionMessage('XML document is malformed'); - $loader($doc); + $loader(); } public function test_it_can_load_with_options(): void { - $doc = new \DOM\XMLDocument(); $xml = ''; $loader = xml_string_loader($xml, LIBXML_NOCDATA); + $doc = $loader(); - $loader($doc); static::assertSame('HELLO', $doc->saveXML($doc->documentElement)); } } diff --git a/tests/Xml/Dom/Locator/Element/SiblingsTest.php b/tests/Xml/Dom/Locator/Element/SiblingsTest.php index d55909cf..26c1a779 100644 --- a/tests/Xml/Dom/Locator/Element/SiblingsTest.php +++ b/tests/Xml/Dom/Locator/Element/SiblingsTest.php @@ -29,8 +29,8 @@ public function test_it_can_detect_siblings(): void $siblings = siblings($domdoc->documentElement->firstElementChild); static::assertCount(3, $siblings); - static::assertSame('1', $siblings->item(0)->nodeValue); - static::assertSame('2', $siblings->item(1)->nodeValue); - static::assertSame('3', $siblings->item(2)->nodeValue); + static::assertSame('1', $siblings->item(0)->textContent); + static::assertSame('2', $siblings->item(1)->textContent); + static::assertSame('3', $siblings->item(2)->textContent); } } diff --git a/tests/Xml/Dom/Locator/Node/ChildrenTest.php b/tests/Xml/Dom/Locator/Node/ChildrenTest.php index d9d0e5d7..c554b35a 100644 --- a/tests/Xml/Dom/Locator/Node/ChildrenTest.php +++ b/tests/Xml/Dom/Locator/Node/ChildrenTest.php @@ -23,6 +23,6 @@ public function test_it_can_detect_children(): void static::assertCount(3, $children); static::assertSame('world', $children->item(0)->nodeName); static::assertSame('moon', $children->item(1)->nodeName); - static::assertSame('Comment', $children->item(2)->nodeValue); + static::assertSame('Comment', $children->item(2)->textContent); } } diff --git a/tests/Xml/Dom/Locator/Node/DetectDocumentTest.php b/tests/Xml/Dom/Locator/Node/DetectDocumentTest.php index 4c7e1017..2d5ef0d4 100644 --- a/tests/Xml/Dom/Locator/Node/DetectDocumentTest.php +++ b/tests/Xml/Dom/Locator/Node/DetectDocumentTest.php @@ -5,7 +5,6 @@ namespace VeeWee\Tests\Xml\Dom\Locator\Node; use \DOM\Element; -use InvalidArgumentException; use PHPUnit\Framework\TestCase; use VeeWee\Xml\Dom\Document; use function VeeWee\Xml\Dom\Locator\Node\detect_document; @@ -20,15 +19,4 @@ public function test_it_can_detect_document(): void static::assertSame($domdoc, detect_document($domdoc)); static::assertSame($domdoc, detect_document($domdoc->documentElement)); } - - - public function test_it_throws_exception_on_unlinked_node(): void - { - $element = new \DOM\Element('name'); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Expected to find an ownerDocument on provided \DOM\Node.'); - - detect_document($element); - } } diff --git a/tests/Xml/Dom/Locator/Xsd/LocateXsdSchemasTest.php b/tests/Xml/Dom/Locator/Xsd/LocateXsdSchemasTest.php index a0d6bfdb..3289e5d2 100644 --- a/tests/Xml/Dom/Locator/Xsd/LocateXsdSchemasTest.php +++ b/tests/Xml/Dom/Locator/Xsd/LocateXsdSchemasTest.php @@ -12,7 +12,7 @@ use function VeeWee\Xml\Dom\Locator\Xsd\locate_namespaced_xsd_schemas; use function VeeWee\Xml\Dom\Locator\Xsd\locate_no_namespaced_xsd_schemas; -final class LocateAllXsdSchemasTest extends TestCase +final class LocateXsdSchemasTest extends TestCase { public function test_it_can_locate_namespaced_xsd_schemas(): void { diff --git a/tests/Xml/Dom/Manipulator/Node/ImportNodeDeeplyTest.php b/tests/Xml/Dom/Manipulator/Node/ImportNodeDeeplyTest.php index 000cfde0..b59f5b39 100644 --- a/tests/Xml/Dom/Manipulator/Node/ImportNodeDeeplyTest.php +++ b/tests/Xml/Dom/Manipulator/Node/ImportNodeDeeplyTest.php @@ -57,8 +57,8 @@ public function test_it_can_recursively_import_a_node_based_on_a_target_document static::assertInstanceOf(\DOM\Element::class, $result); static::assertSame('world', $result->nodeName); - static::assertSame('myvalue', $result->attributes->getNamedItem('myattrib')->nodeValue); + static::assertSame('myvalue', $result->attributes->getNamedItem('myattrib')->textContent); static::assertSame('name', $result->firstChild->nodeName); - static::assertSame('VeeWee', $result->firstChild->nodeValue); + static::assertSame('VeeWee', $result->firstChild->textContent); } } diff --git a/tests/Xml/Dom/Traverser/TraverserTest.php b/tests/Xml/Dom/Traverser/TraverserTest.php index 77c134ae..5367c968 100644 --- a/tests/Xml/Dom/Traverser/TraverserTest.php +++ b/tests/Xml/Dom/Traverser/TraverserTest.php @@ -29,7 +29,7 @@ public function test_it_can_traverse_without_visitors(): void static::assertXmlStringEqualsXmlString($doc->toXmlString(), $actual); } - + public function test_it_can_traverse_single_node(): void { $doc = Document::fromXmlString($actual = ''); @@ -49,7 +49,7 @@ public function onNodeLeave(\DOM\Node $node): Action static::assertXmlStringEqualsXmlString(xml_string()($newRoot), ''); } - + public function test_it_can_traverse_attributes(): void { $doc = Document::fromXmlString($actual = ''); @@ -61,7 +61,7 @@ public function onNodeLeave(\DOM\Node $node): Action return new Action\Noop(); } - $node->nodeValue = 'Jos'; + $node->textContent = 'Jos'; return new Action\Noop(); } @@ -71,7 +71,7 @@ public function onNodeLeave(\DOM\Node $node): Action static::assertXmlStringEqualsXmlString($doc->toXmlString(), ''); } - + public function test_it_can_traverse_child_nodes(): void { $doc = Document::fromXmlString($actual = ' '); @@ -80,11 +80,11 @@ public function test_it_can_traverse_child_nodes(): void public function onNodeLeave(\DOM\Node $node): Action { if (is_non_empty_text($node)) { - $node->nodeValue = 'Yo'; + $node->textContent = 'Yo'; return new Action\Noop(); } if (is_whitespace($node)) { - $node->nodeValue = ''; + $node->textContent = ''; return new Action\Noop(); } @@ -101,7 +101,7 @@ public function onNodeLeave(\DOM\Node $node): Action static::assertXmlStringEqualsXmlString($doc->toXmlString(), 'Yo'); } - + public function test_it_can_handle_node_enter_and_leave(): void { $doc = Document::fromXmlString($actual = ''); @@ -132,7 +132,7 @@ public function onNodeLeave(\DOM\Node $node): Action static::assertXmlStringEqualsXmlString($doc->toXmlString(), ''); } - + public function test_it_can_recursively_remove_empty_nodes(): void { $doc = Document::fromXmlString(