diff options
-rw-r--r-- | HISTORY | 1 | ||||
-rw-r--r-- | framework/Xml/TXmlDocument.php | 54 | ||||
-rw-r--r-- | tests/unit/Xml/AllTests.php | 6 | ||||
-rw-r--r-- | tests/unit/Xml/TXmlDocumentTest.php | 38 | ||||
-rw-r--r-- | tests/unit/Xml/TXmlElementListTest.php | 20 | ||||
-rw-r--r-- | tests/unit/Xml/TXmlElementTest.php | 92 |
6 files changed, 144 insertions, 67 deletions
@@ -3,6 +3,7 @@ Version 3.1.0 To Be Released BUG: Ticket#621 - TWizardNavigationButtonStyle could not be found (Qiang) BUG: TWizard Sidebar using TDataListItemRenderer has error (Qiang) ENH: Ticket#631 - Make TQueue implement Countable as the other collection classes (Knut) +ENH: Ticket#634 - Override __toString for TXmlElement and TXmlDocument (Knut) Version 3.1.0 RC May 14, 2007 ============================= diff --git a/framework/Xml/TXmlDocument.php b/framework/Xml/TXmlDocument.php index b4293763..0b44214e 100644 --- a/framework/Xml/TXmlDocument.php +++ b/framework/Xml/TXmlDocument.php @@ -14,7 +14,7 @@ * TXmlElement class. * * TXmlElement represents an XML element node. - * You can obtain its tagname, attributes, text between the openning and closing + * You can obtain its tag-name, attributes, text between the opening and closing * tags via the TagName, Attributes, and Value properties, respectively. * You can also retrieve its parent and child elements by Parent and Elements * properties, respectively. @@ -33,11 +33,11 @@ class TXmlElement extends TComponent */ private $_parent=null; /** - * @var string tagname of this element + * @var string tag-name of this element */ private $_tagName='unknown'; /** - * @var string text enclosed between openning and closing tags of this element + * @var string text enclosed between opening and closing tags of this element */ private $_value=''; /** @@ -51,7 +51,7 @@ class TXmlElement extends TComponent /** * Constructor. - * @param string tagname for this element + * @param string tag-name for this element */ public function __construct($tagName) { @@ -75,7 +75,7 @@ class TXmlElement extends TComponent } /** - * @return string tagname of this element + * @return string tag-name of this element */ public function getTagName() { @@ -83,7 +83,7 @@ class TXmlElement extends TComponent } /** - * @param string tagname of this element + * @param string tag-name of this element */ public function setTagName($tagName) { @@ -163,7 +163,7 @@ class TXmlElement extends TComponent } /** - * @return TXmlElement the first child element that has the specified tagname, null if not found + * @return TXmlElement the first child element that has the specified tag-name, null if not found */ public function getElementByTagName($tagName) { @@ -177,7 +177,7 @@ class TXmlElement extends TComponent } /** - * @return TList list of all child elements that have the specified tagname + * @return TList list of all child elements that have the specified tag-name */ public function getElementsByTagName($tagName) { @@ -222,6 +222,24 @@ class TXmlElement extends TComponent else return $prefix."<{$this->_tagName}$attr />"; } + + /** + * Magic-method override. Called whenever this element is used as a string. + * <code> + * $element = new TXmlElement('tag'); + * echo $element; + * </code> + * or + * <code> + * $element = new TXmlElement('tag'); + * $xml = (string)$element; + * </code> + * @return string string representation of this element + */ + public function __toString() + { + return $this->toString(); + } private function xmlEncode($str) { @@ -415,6 +433,26 @@ class TXmlDocument extends TXmlElement $encoding=empty($this->_encoding)?'':' encoding="'.$this->_encoding.'"'; return "<?xml{$version}{$encoding}?>\n".$this->toString(0); } + + /** + * Magic-method override. Called whenever this document is used as a string. + * <code> + * $document = new TXmlDocument(); + * $document->TagName = 'root'; + * echo $document; + * </code> + * or + * <code> + * $document = new TXmlDocument(); + * $document->TagName = 'root'; + * $xml = (string)$document; + * </code> + * @return string string representation of this document + */ + public function __toString() + { + return $this->saveToString(); + } /** * Recursively converts DOM XML nodes into TXmlElement diff --git a/tests/unit/Xml/AllTests.php b/tests/unit/Xml/AllTests.php index 0760474f..10658d42 100644 --- a/tests/unit/Xml/AllTests.php +++ b/tests/unit/Xml/AllTests.php @@ -5,9 +5,9 @@ if(!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'Xml_AllTests::main'); } -require_once 'Xml/TXmlDocumentTest.php'; -require_once 'Xml/TXmlElementTest.php'; -require_once 'Xml/TXmlElementListTest.php'; +require_once 'TXmlDocumentTest.php'; +require_once 'TXmlElementTest.php'; +require_once 'TXmlElementListTest.php'; class Xml_AllTests { public static function main() { diff --git a/tests/unit/Xml/TXmlDocumentTest.php b/tests/unit/Xml/TXmlDocumentTest.php index 1e588974..b47f735d 100644 --- a/tests/unit/Xml/TXmlDocumentTest.php +++ b/tests/unit/Xml/TXmlDocumentTest.php @@ -8,28 +8,28 @@ Prado::using('System.Xml.TXmlDocument'); */ class TXmlDocumentTest extends PHPUnit_Framework_TestCase { - public function testConstruct() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testConstruct() { + throw new PHPUnit_Framework_IncompleteTestError(); + } - public function testSetVersion() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSetVersion() { + throw new PHPUnit_Framework_IncompleteTestError(); + } - public function testSetEncoding() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSetEncoding() { + throw new PHPUnit_Framework_IncompleteTestError(); + } - public function testLoadFromFile() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testLoadFromFile() { + throw new PHPUnit_Framework_IncompleteTestError(); + } - public function testLoadFromString() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testLoadFromString() { + throw new PHPUnit_Framework_IncompleteTestError(); + } - public function testSaveToString() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSaveToString() { + throw new PHPUnit_Framework_IncompleteTestError(); + } } -?>
\ No newline at end of file +?> diff --git a/tests/unit/Xml/TXmlElementListTest.php b/tests/unit/Xml/TXmlElementListTest.php index eb91c46f..95906c1f 100644 --- a/tests/unit/Xml/TXmlElementListTest.php +++ b/tests/unit/Xml/TXmlElementListTest.php @@ -8,16 +8,16 @@ Prado::using('System.Xml.TXmlDocument'); */ class TXmlElementListTest extends PHPUnit_Framework_TestCase { - public function testConstruct() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testConstruct() { + throw new PHPUnit_Framework_IncompleteTestError(); + } - public function testInsertAt() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testInsertAt() { + throw new PHPUnit_Framework_IncompleteTestError(); + } - public function testRemoveAt() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testRemoveAt() { + throw new PHPUnit_Framework_IncompleteTestError(); + } } -?>
\ No newline at end of file +?> diff --git a/tests/unit/Xml/TXmlElementTest.php b/tests/unit/Xml/TXmlElementTest.php index eec22b1f..89baed03 100644 --- a/tests/unit/Xml/TXmlElementTest.php +++ b/tests/unit/Xml/TXmlElementTest.php @@ -8,39 +8,77 @@ Prado::using('System.Xml.TXmlDocument'); */ class TXmlElementTest extends PHPUnit_Framework_TestCase { - public function setUp() { - } + public function setUp() { + } - public function testConstruct() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testConstruct() { + $element = new TXmlElement('tag'); + self::assertEquals('tag', $element->getTagName()); + } - public function testSetParent() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSetParent() { + $parent = new TXmlElement('parent'); + $child = new TXmlElement('child'); + $child->setParent($parent); + self::assertEquals($parent, $child->getParent()); + } - public function testSetTagName() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSetTagName() { + $element = new TXmlElement('tag'); + $element->setTagName('newtag'); + self::assertEquals('newtag', $element->getTagName()); + } - public function testSetValue() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSetValue() { + $element = new TXmlElement('tag'); + $element->setValue('value'); + self::assertEquals('value', $element->getValue()); + } - public function testSetAttribute() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testHasElement() { + $element = new TXmlElement('first'); + self::assertEquals(false, $element->getHasElement()); + $element->Elements[] = new TXmlElement('second'); + self::assertEquals(true, $element->getHasElement()); + } - public function testGetElementByTagName() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testHasAttribute() { + $element = new TXmlElement('tag'); + self::assertEquals(false, $element->getHasAttribute()); + $element->Attributes[] = new TMap(array('key' => 'value')); + self::assertEquals(true, $element->getHasAttribute()); + } - public function testGetElementsByTagName() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testSetAttribute() { + $element = new TXmlElement('tag'); + self::assertEquals(null, $element->getAttribute('key')); + $element->setAttribute('key', 'value'); + self::assertEquals('value', $element->getAttribute('key')); + } - public function testToString() { - throw new PHPUnit_Framework_IncompleteTestError(); - } + public function testGetElementByTagName() { + $element = new TXmlElement('tag'); + self::assertEquals(null, $element->getElementByTagName('first')); + $element->Elements[] = new TXmlElement('first'); + $first = $element->getElementByTagName('first'); + self::assertType('TXmlElement', $first); + self::assertEquals('first', $first->getTagName()); + } + + public function testGetElementsByTagName() { + $element = new TXmlElement('tag'); + $element->Elements[] = new TXmlElement('tag'); + $element->Elements[] = new TXmlElement('tag'); + self::assertEquals(2, count($element->getElementsByTagName('tag'))); + } + + public function testToString() { + $element = new TXmlElement('tag'); + self::assertEquals('<tag />', (string)$element); + $element->setAttribute('key', 'value'); + self::assertEquals('<tag key="value" />', (string)$element); + $element->setValue('value'); + self::assertEquals('<tag key="value">value</tag>', (string)$element); + } } -?>
\ No newline at end of file +?> |