MFH: DOM Tests from London Testfest

This commit is contained in:
Scott MacVicar 2008-05-27 10:21:21 +00:00
parent a591b8bdc8
commit a2da2a91b1
4 changed files with 182 additions and 0 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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>

View File

@ -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)