]> granicus.if.org Git - php/commitdiff
DOM Tests from London Testfest
authorScott MacVicar <scottmac@php.net>
Tue, 27 May 2008 10:21:16 +0000 (10:21 +0000)
committerScott MacVicar <scottmac@php.net>
Tue, 27 May 2008 10:21:16 +0000 (10:21 +0000)
ext/dom/tests/DOMDocument_savexml_basic.phpt [new file with mode: 0644]
ext/dom/tests/DOMNode_hasChildNodes.phpt [new file with mode: 0644]
ext/dom/tests/DOMNode_insertBefore.phpt [new file with mode: 0644]
ext/dom/tests/DOMNode_normalize_basic.phpt [new file with mode: 0644]

diff --git a/ext/dom/tests/DOMDocument_savexml_basic.phpt b/ext/dom/tests/DOMDocument_savexml_basic.phpt
new file mode 100644 (file)
index 0000000..34dfcc3
--- /dev/null
@@ -0,0 +1,39 @@
+--TEST--
+DOM Document : save and saveXML
+--CREDIT--
+Sami Greenbury (sami@patabugen.co.uk)
+# TestFest 2008
+--SKIPIF--
+<?php
+require_once('skipif.inc');
+?>
+--FILE--
+<?php
+
+$xml = <<< EOXML
+<?xml version="1.0" encoding="utf-8"?>
+<courses>
+       <!-- Hello World! -->
+       <aNode>
+               <childNode>
+                       <childlessNode />
+               </childNode>
+       </aNode>
+</courses>
+EOXML;
+
+$dom = new DOMDocument();
+$dom->loadXML($xml);
+$root = $dom->documentElement;
+$directory = dirname(__FILE__);
+
+$filename = $directory."/tmp_dom_savexml".time();
+var_dump($dom->save($filename));
+$result = file_get_contents($filename);
+var_dump($result == $dom->saveXML());
+
+unlink($filename);
+
+--EXPECTF--
+int(151)
+bool(true)
\ No newline at end of file
diff --git a/ext/dom/tests/DOMNode_hasChildNodes.phpt b/ext/dom/tests/DOMNode_hasChildNodes.phpt
new file mode 100644 (file)
index 0000000..406f8d1
--- /dev/null
@@ -0,0 +1,47 @@
+--TEST--
+Tests DOMNode::hasChildNodes()
+--CREDITS--
+Michael Stillwell <mjs@beebo.org>
+# TestFest 2008
+--FILE--
+<?php
+
+$dom = new DOMDocument();
+
+$dom->loadXML('<root/>');
+
+echo $dom->saveXML();
+
+echo "Document has child nodes\n";
+var_dump($dom->documentElement->hasChildNodes());
+
+echo "Document has child nodes\n";
+$dom->loadXML('<root><a/></root>');
+var_dump($dom->documentElement->hasChildNodes());
+
+echo "Remove node and save\n";
+$dom->documentElement->removeChild($dom->documentElement->firstChild);
+echo $dom->saveXML();
+
+echo "Document has child nodes\n";
+var_dump($dom->documentElement->hasChildNodes());
+
+echo "Document with 2 child nodes\n";
+$dom->loadXML('<root><a/><b/></root>');
+var_dump($dom->documentElement->hasChildNodes());
+
+?>
+--EXPECTF--
+<?xml version="1.0"?>
+<root/>
+Document has child nodes
+bool(false)
+Document has child nodes
+bool(true)
+Remove node and save
+<?xml version="1.0"?>
+<root/>
+Document has child nodes
+bool(false)
+Document with 2 child nodes
+bool(true)
diff --git a/ext/dom/tests/DOMNode_insertBefore.phpt b/ext/dom/tests/DOMNode_insertBefore.phpt
new file mode 100644 (file)
index 0000000..85cd502
--- /dev/null
@@ -0,0 +1,32 @@
+--TEST--
+Tests DOMNode::insertBefore()
+--CREDITS--
+Michael Stillwell <mjs@beebo.org>
+# TestFest 2008
+--FILE--
+<?php
+
+$dom = new DOMDocument();
+$dom->loadXML('<root/>');
+echo $dom->saveXML();
+
+$e1 = $dom->createElement("A");
+$e2 = $dom->documentElement->appendChild($dom->createElement("B"));
+
+echo "Add new node B\n";
+echo $dom->saveXML();
+
+echo "Add new node A before B\n";
+$e2->parentNode->insertBefore($e1, $e2);
+echo $dom->saveXML();
+
+?>
+--EXPECTF--
+<?xml version="1.0"?>
+<root/>
+Add new node B
+<?xml version="1.0"?>
+<root><B/></root>
+Add new node A before B
+<?xml version="1.0"?>
+<root><A/><B/></root>
diff --git a/ext/dom/tests/DOMNode_normalize_basic.phpt b/ext/dom/tests/DOMNode_normalize_basic.phpt
new file mode 100644 (file)
index 0000000..8ab9080
--- /dev/null
@@ -0,0 +1,64 @@
+--TEST--
+DomNode::normalize()
+--SKIPIF--
+<?php
+include('skipif.inc');
+?>
+--FILE--
+<?php
+
+/* Create an XML document
+ * with structure
+ * <book>
+ *  <author></author>
+ *  <title>This is the title</title>
+ * </book>
+ * Calculate the number of title text nodes (1).
+ * Add another text node to title. Calculate the number of title text nodes (2).
+ * Normalize author. Calculate the number of title text nodes (2).
+ * Normalize title. Calculate the number of title text nodes (1).
+*/
+
+$doc = new DOMDocument();
+
+$root = $doc->createElement('book');
+$doc->appendChild($root);
+
+$title = $doc->createElement('title');
+$root->appendChild($title);
+
+$author = $doc->createElement('author');
+$root->appendChild($author);
+
+$text = $doc->createTextNode('This is the first title');
+$title->appendChild($text);
+
+echo "Number of child nodes of title = ";
+var_dump($title->childNodes->length);
+
+// add a second text node to title
+$text = $doc->createTextNode('This is the second title');
+$title->appendChild($text);
+
+echo "Number of child nodes of title after adding second title = ";
+var_dump($title->childNodes->length);
+
+// should do nothing
+$author->normalize();
+
+echo "Number of child nodes of title after normalizing author = ";
+var_dump($title->childNodes->length);
+
+
+// should concatenate first and second title text nodes
+$title->normalize();
+
+echo "Number of child nodes of title after normalizing title = ";
+var_dump($title->childNodes->length);
+
+?>
+--EXPECTF--
+Number of child nodes of title = int(1)
+Number of child nodes of title after adding second title = int(2)
+Number of child nodes of title after normalizing author = int(2)
+Number of child nodes of title after normalizing title = int(1)
\ No newline at end of file