| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- require_once(dirname(__FILE__).'/../src/xmlStroller.php');
- use XmlStroller\XmlStroller as XmlStroller;
- class XmlStrollerTester extends PHPUnit_Framework_TestCase
- {
- public function testLoad()
- {
- $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
- $success = true;
- try {
- $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/booksFail.xml")));
- $success = false;
- }
- catch (Exception $e) {}
- $this->assertTrue($success);
- }
- public function testSelectSimpleChild()
- {
- $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.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()
- {
- $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.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()
- {
- $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.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];
- $book = $book->parent();
- try {
- $parser->{"catalog book[id=]"};
- $this->fail("Expected Exception");
- } catch (\Exception $e) {}
- }
- public function testGetAttributes()
- {
- $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.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"});
- */
- }
- }
|