Browse Source

Abstraction SimpleXmlIterator

isundil 10 years ago
parent
commit
9c69af0f79
3 changed files with 130 additions and 7 deletions
  1. 13 6
      src/xmlStroller.php
  2. 1 1
      tests/test.php
  3. 116 0
      tests/test_SimpleXml.php

+ 13 - 6
src/xmlStroller.php

@@ -37,6 +37,10 @@ class XmlStroller
 			$this->xmlNode = XmlNode::parse($data);
 			$this->xmlNode = XmlNode::parse($data);
 		else if ($data instanceof XmlNode)
 		else if ($data instanceof XmlNode)
 			$this->xmlNode = clone ($data);
 			$this->xmlNode = clone ($data);
+		else if ($data instanceof \SimpleXMLElement)
+			$this->xmlNode = $data;
+		else
+			throw new \Exception("Unrecognized parameter");
     }
     }
 
 
     private function getTokens($str)
     private function getTokens($str)
@@ -205,8 +209,11 @@ class XmlStroller
     {
     {
         $request = $this->compile($criteria_str);
         $request = $this->compile($criteria_str);
         $result = array();
         $result = array();
+		$node = $this->xmlNode;
+		if (!($node instanceof XmlNode))
+			$node = array($node);
         foreach ($request as $i)
         foreach ($request as $i)
-            $this->checkQuery($this->xmlNode, $result, $i);
+            $this->checkQuery($node, $result, $i);
 		$r = array();
 		$r = array();
 		foreach ($result as $i)
 		foreach ($result as $i)
 			$r[] = new self($i);
 			$r[] = new self($i);
@@ -260,11 +267,6 @@ class XmlStroller
 		return (int) $this->value();
 		return (int) $this->value();
 	}
 	}
 
 
-	public function parent()
-	{
-		return new self($this->xmlNode->parent());
-	}
-
     /**
     /**
      * Find in xml the request
      * Find in xml the request
     **/
     **/
@@ -272,5 +274,10 @@ class XmlStroller
     {
     {
         return $this->select($name);
         return $this->select($name);
     }
     }
+
+	public function node()
+	{
+		return $this->xmlNode;
+	}
 }
 }
 
 

+ 1 - 1
tests/test.php

@@ -82,7 +82,7 @@ class XmlStrollerTester extends PHPUnit_Framework_TestCase
 		$this->assertEquals(49.90, $books[count($books) -1]->valueDouble());
 		$this->assertEquals(49.90, $books[count($books) -1]->valueDouble());
 		$this->assertEquals(39.95, $parser->{"catalog book price[currency=EURO]"}[0]->valueDouble());
 		$this->assertEquals(39.95, $parser->{"catalog book price[currency=EURO]"}[0]->valueDouble());
 		$book = $parser->{"book price[currency=EURO]"}[0];
 		$book = $parser->{"book price[currency=EURO]"}[0];
-		$book = $book->parent();
+		$book = $book->node()->parent();
 		try {
 		try {
 			$parser->{"catalog book[id=]"};
 			$parser->{"catalog book[id=]"};
 			$this->fail("Expected Exception");
 			$this->fail("Expected Exception");

+ 116 - 0
tests/test_SimpleXml.php

@@ -0,0 +1,116 @@
+<?php
+
+require_once(dirname(__FILE__).'/../src/xmlStroller.php');
+
+use XmlStroller\XmlStroller as XmlStroller;
+
+class XmlStrollerTester extends PHPUnit_Framework_TestCase
+{
+	public function testLoad()
+	{
+		$xml = new \SimpleXmlIterator(utf8_encode(file_get_contents(__DIR__."/books.xml")));
+		$parser = new XmlStroller($xml);
+	}
+
+	public function testSelectSimpleChild()
+	{
+		$xml = new \SimpleXmlIterator(utf8_encode(file_get_contents(__DIR__."/books.xml")));
+		$parser = new XmlStroller($xml);
+		$this->assertCount(1, $parser->catalog);
+		$this->assertCount(13, $parser->{"catalog book"});
+		$this->assertCount(13, $parser->{"catalog    book"});
+		$this->assertCount(13, $parser->{"book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog   book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"book"});
+		$this->assertCount(0, $parser->{"fail"});
+		$this->assertCount(0, $parser->{"catalog fail"});
+		$this->assertCount(0, $parser->{"book fail"});
+	}
+
+	public function testSelectDirectChild()
+	{
+		$xml = new \SimpleXmlIterator(utf8_encode(file_get_contents(__DIR__."/books.xml")));
+		$parser = new XmlStroller($xml);
+		$this->assertCount(1, $parser->{">catalog"});
+		$this->assertCount(1, $parser->{"> catalog"});
+		$this->assertCount(1, $parser->{" > catalog"});
+		$this->assertCount(13, $parser->{"catalog>book"});
+		$this->assertCount(13, $parser->{"catalog >book"});
+		$this->assertCount(13, $parser->{"catalog>  book"});
+		$this->assertCount(13, $parser->{"catalog  >book"});
+		$this->assertCount(13, $parser->{"catalog> book"});
+		$this->assertCount(13, $parser->{"catalog > book"});
+		$this->assertCount(13, $parser->{"catalog   >   book"});
+		$this->assertCount(13, $parser->{" > catalog > book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{">catalog>book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog>book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog >book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog> book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog > book"});
+		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog   >   book"});
+		$this->assertCount(0, $parser->{"catalog > fail"});
+		$this->assertCount(0, $parser->{">fail"});
+	}
+
+	public function testSelectAttribute()
+	{
+		$xml = new \SimpleXmlIterator(utf8_encode(file_get_contents(__DIR__."/books.xml")));
+		$parser = new XmlStroller($xml);
+		$book = $parser->{"catalog book[id=bk101]"};
+		$this->assertCount(1, $book);
+		$this->assertEquals("bk101", $book[0]->attr("id"));
+		$book = $parser->{"catalog [id=bk101]book"};
+		$this->assertCount(1, $book);
+		$this->assertEquals("bk101", $book[0]->attr("id"));
+		$book = $parser->{"catalog [id=bk101]"};
+		$this->assertCount(1, $book);
+		$this->assertEquals("bk101", $book[0]->attr("id"));
+		$this->assertCount(0, $book = $parser->{"catalog[id=oupas]"});
+		$this->assertCount(12, $parser->{"catalog [id]"});
+		$this->assertCount(12, $parser->{"catalog [id]book"});
+		$this->assertCount(12, $parser->{"catalog book[id]"});
+		$this->assertCount(12, $parser->{"catalog book[id] *[currency]"});
+		$this->assertCount(12, $parser->{"catalog book[id] [currency]"});
+		$this->assertCount(12, $parser->{"catalog book[id] price[currency]"});
+		$this->assertCount(13, $parser->{"catalog book price[currency=USD]"});
+		$this->assertCount(1, $parser->{"catalog book price[currency=EURO]"});
+		$books = $parser->{"price[currency=USD]"};
+		$this->assertEquals(49.90, $books[count($books) -1]->valueDouble());
+		$this->assertEquals(39.95, $parser->{"catalog book price[currency=EURO]"}[0]->valueDouble());
+		$book = $parser->{"book price[currency=EURO]"}[0];
+	}
+
+	public function testGetAttributes()
+	{
+		$xml = new \SimpleXmlIterator(utf8_encode(file_get_contents(__DIR__."/books.xml")));
+		$parser = new XmlStroller($xml);
+		$this->assertCount(13, $parser->{"catalog >book"});
+		$firstBook = $parser->{"catalog >book"}[0];
+		$this->assertInternalType('string', $firstBook->attr("id"));
+		$this->assertEquals("bk101", $firstBook->attr("id"));
+		$this->assertInternalType('array', $firstBook->attr());
+		$this->assertCount(1, $firstBook->attr());
+		$this->assertNull($firstBook->attr("fail"));
+		$this->assertNull($firstBook->attr("fail", null));
+		$parser->debug = true;
+		$this->assertEquals("test", $firstBook->attr("fail", "test"));
+		$parser->debug = false;
+	}
+
+	public function testFnc()
+	{
+	}
+
+	public function testAttributeFnc()
+	{
+		$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
+		/*
+		$this->assertCount(1, $parser->{"catalog book[id=bk101]:first-child"});
+		$this->assertCount(1, $parser->{"catalog book:first-child[id=bk101]"});
+		$this->assertCount(1, $parser->{"catalog :first-child[id=bk101]"});
+		$this->assertCount(1, $parser->{"catalog [id=bk101]:first-child"});
+		*/
+	}
+}
+