diff options
author | tof <> | 2007-06-27 11:51:32 +0000 |
---|---|---|
committer | tof <> | 2007-06-27 11:51:32 +0000 |
commit | fdb122f2da9e36dd41243a69e8ea9eec20ad3a1d (patch) | |
tree | ffe0f3ca249df9a97b80240c04cf9e5e4b827bed /tests | |
parent | 0621133cf16ece2cbf4be49438ab0eacaada5630 (diff) |
Add unit test for TXmlDocument and TXmlElementList
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/Xml/TXmlDocumentTest.php | 58 | ||||
-rw-r--r-- | tests/unit/Xml/TXmlElementListTest.php | 21 | ||||
-rw-r--r-- | tests/unit/Xml/data/test.xml | 8 |
3 files changed, 78 insertions, 9 deletions
diff --git a/tests/unit/Xml/TXmlDocumentTest.php b/tests/unit/Xml/TXmlDocumentTest.php index b47f735d..8766b231 100644 --- a/tests/unit/Xml/TXmlDocumentTest.php +++ b/tests/unit/Xml/TXmlDocumentTest.php @@ -9,27 +9,73 @@ Prado::using('System.Xml.TXmlDocument'); class TXmlDocumentTest extends PHPUnit_Framework_TestCase { public function testConstruct() { - throw new PHPUnit_Framework_IncompleteTestError(); + $xmldoc=new TXmlDocument ('1.0', 'utf-8'); + self::assertEquals('1.0', $xmldoc->getVersion()); + self::assertEquals('utf-8', $xmldoc->getEncoding()); } public function testSetVersion() { - throw new PHPUnit_Framework_IncompleteTestError(); + $xmldoc=new TXmlDocument ('1.0', 'utf-8'); + self::assertEquals('1.0', $xmldoc->getVersion()); + $xmldoc->setVersion('2.0'); + self::assertEquals('2.0', $xmldoc->getVersion()); } public function testSetEncoding() { - throw new PHPUnit_Framework_IncompleteTestError(); + $xmldoc=new TXmlDocument ('1.0', 'utf-8'); + self::assertEquals('utf-8', $xmldoc->getEncoding()); + $xmldoc->setEncoding('iso8859-1'); + self::assertEquals('iso8859-1', $xmldoc->getEncoding()); } public function testLoadFromFile() { - throw new PHPUnit_Framework_IncompleteTestError(); + $file=dirname(__FILE__).'/data/test.xml'; + $xmldoc=new TXmlDocument(); + try { + $xmldoc->loadFromFile('unexistentXmlFile.xml'); + self::fail('Expected TIOException not thrown'); + } catch (TIOException $e) {} + + self::assertTrue($xmldoc->loadFromFile($file)); + self::assertEquals('1.0', $xmldoc->getVersion()); + self::assertEquals('UTF-8',$xmldoc->getEncoding()); } public function testLoadFromString() { - throw new PHPUnit_Framework_IncompleteTestError(); + $xmlStr='<?xml version="1.0" encoding="UTF-8"?><rootNode><node id="node1" param="attribute1"/><node id="node2" param="attribute2"/></rootNode>'; + $xmldoc=new TXmlDocument(); + self::assertTrue($xmldoc->loadFromString($xmlStr)); + self::assertEquals('1.0', $xmldoc->getVersion()); + self::assertEquals('UTF-8',$xmldoc->getEncoding()); } public function testSaveToString() { - throw new PHPUnit_Framework_IncompleteTestError(); + $xmldoc=new TXmlDocument('1.0','utf-8'); + $xmldoc->setTagName('root'); + $node=new TXmlElement('node'); + $node->setAttribute('param','attribute1'); + $xmldoc->getElements()->add($node); + $xmlString=$xmldoc->saveToString(); + // test magic method + $magicString=(string)$xmldoc; + self::assertEquals($magicString,$xmlString); + // Result string should be : + $resultString="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n <node param=\"attribute1\"\n</root>"; + self::assertEquals($xmlString, $magicString); + + } + + public function testSaveToFile() { + $file=dirname(__FILE__).'/data/tmp.xml'; + if (!is_writable(dirname($file))) self::markTestSkipped(dirname($file).' must be writable for this test'); + $xmldoc=new TXmlDocument('1.0','utf-8'); + $xmldoc->setTagName('root'); + $node=new TXmlElement('node'); + $node->setAttribute('param','attribute1'); + $xmldoc->getElements()->add($node); + $xmldoc->saveToFile($file); + self::assertTrue(is_file($file)); + if (is_file($file)) unlink ($file); } } ?> diff --git a/tests/unit/Xml/TXmlElementListTest.php b/tests/unit/Xml/TXmlElementListTest.php index 95906c1f..91e09267 100644 --- a/tests/unit/Xml/TXmlElementListTest.php +++ b/tests/unit/Xml/TXmlElementListTest.php @@ -9,15 +9,30 @@ Prado::using('System.Xml.TXmlDocument'); class TXmlElementListTest extends PHPUnit_Framework_TestCase { public function testConstruct() { - throw new PHPUnit_Framework_IncompleteTestError(); + $element=new TXmlElement('tag'); + $list=new TXmlElementList($element); + self::assertEquals($element,self::getAttribute($list, '_o')); } public function testInsertAt() { - throw new PHPUnit_Framework_IncompleteTestError(); + $element=new TXmlElement('tag'); + $list=new TXmlElementList($element); + try { + $list->insertAt(0,'ABadElement'); + self::fail('Expected TInvalidDataTypeException not thrown'); + } catch (TInvalidDataTypeException $e) {} + $newElement=new TXmlElement('newTag'); + $list->insertAt(0,$newElement); + self::assertEquals($newElement,$list->itemAt(0)); + } public function testRemoveAt() { - throw new PHPUnit_Framework_IncompleteTestError(); + $element=new TXmlElement('tag'); + $list=new TXmlElementList($element); + $newElement=new TXmlElement('newTag'); + $list->insertAt(0,$newElement); + self::assertEquals($newElement,$list->removeAt(0)); } } ?> diff --git a/tests/unit/Xml/data/test.xml b/tests/unit/Xml/data/test.xml new file mode 100644 index 00000000..0cd2f58c --- /dev/null +++ b/tests/unit/Xml/data/test.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<rootNode attr1="test1"> + <node id="node1" attr1="attribute1" attr2="attribute2" attr3="attribute3"/> + <node id="node2"> + <attr name="attribute1">Value1</attr> + <attr name="attribute2">Value2</attr> + </node> +</rootNode>
\ No newline at end of file |