test.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. require_once(__DIR__.'/xmlStroller.php');
  3. use XmlStroller\XmlStroller as XmlStroller;
  4. class XmlStrollerTester extends PHPUnit_Framework_TestCase
  5. {
  6. public function testLoad()
  7. {
  8. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  9. $success = true;
  10. try {
  11. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/booksFail.xml")));
  12. $success = false;
  13. }
  14. catch (Exception $e) {}
  15. $this->assertTrue($success);
  16. }
  17. public function testSelectSimpleChild()
  18. {
  19. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  20. $this->assertCount(1, $parser->catalog);
  21. $this->assertCount(13, $parser->{"catalog book"});
  22. $this->assertCount(13, $parser->{"catalog book"});
  23. $this->assertCount(13, $parser->{"book"});
  24. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog book"});
  25. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog book"});
  26. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"book"});
  27. $this->assertCount(0, $parser->{"fail"});
  28. $this->assertCount(0, $parser->{"catalog fail"});
  29. $this->assertCount(0, $parser->{"book fail"});
  30. }
  31. public function testSelectDirectChild()
  32. {
  33. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  34. $this->assertCount(1, $parser->{">catalog"});
  35. $this->assertCount(1, $parser->{"> catalog"});
  36. $this->assertCount(1, $parser->{" > catalog"});
  37. $this->assertCount(13, $parser->{"catalog>book"});
  38. $this->assertCount(13, $parser->{"catalog >book"});
  39. $this->assertCount(13, $parser->{"catalog> book"});
  40. $this->assertCount(13, $parser->{"catalog >book"});
  41. $this->assertCount(13, $parser->{"catalog> book"});
  42. $this->assertCount(13, $parser->{"catalog > book"});
  43. $this->assertCount(13, $parser->{"catalog > book"});
  44. $this->assertCount(13, $parser->{" > catalog > book"});
  45. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{">catalog>book"});
  46. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog>book"});
  47. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog >book"});
  48. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog> book"});
  49. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog > book"});
  50. $this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog > book"});
  51. $this->assertCount(0, $parser->{"catalog > fail"});
  52. $this->assertCount(0, $parser->{">fail"});
  53. }
  54. public function testSelectAttribute()
  55. {
  56. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  57. $book = $parser->{"catalog book[id=bk101]"};
  58. $this->assertCount(1, $book);
  59. $this->assertEquals("bk101", $book[0]->attr("id"));
  60. $book = $parser->{"catalog [id=bk101]book"};
  61. $this->assertCount(1, $book);
  62. $this->assertEquals("bk101", $book[0]->attr("id"));
  63. $book = $parser->{"catalog [id=bk101]"};
  64. $this->assertCount(1, $book);
  65. $this->assertEquals("bk101", $book[0]->attr("id"));
  66. $this->assertCount(0, $book = $parser->{"catalog[id=oupas]"});
  67. $this->assertCount(12, $parser->{"catalog [id]"});
  68. $this->assertCount(12, $parser->{"catalog [id]book"});
  69. $this->assertCount(12, $parser->{"catalog book[id]"});
  70. $this->assertCount(12, $parser->{"catalog book[id] *[currency]"});
  71. $this->assertCount(12, $parser->{"catalog book[id] [currency]"});
  72. $this->assertCount(12, $parser->{"catalog book[id] price[currency]"});
  73. $this->assertCount(13, $parser->{"catalog book price[currency=USD]"});
  74. $this->assertCount(1, $parser->{"catalog book price[currency=EURO]"});
  75. $books = $parser->{"price[currency=USD]"};
  76. $this->assertEquals(49.90, $books[count($books) -1]->valueDouble());
  77. $this->assertEquals(39.95, $parser->{"catalog book price[currency=EURO]"}[0]->valueDouble());
  78. $book = $parser->{"book price[currency=EURO]"}[0];
  79. $book = $book->parent();
  80. try {
  81. $parser->{"catalog book[id=]"};
  82. $this->fail("Expected Exception");
  83. } catch (\Exception $e) {}
  84. }
  85. public function testGetAttributes()
  86. {
  87. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  88. $this->assertCount(13, $parser->{"catalog >book"});
  89. $firstBook = $parser->{"catalog >book"}[0];
  90. $this->assertInternalType('string', $firstBook->attr("id"));
  91. $this->assertEquals("bk101", $firstBook->attr("id"));
  92. $this->assertInternalType('array', $firstBook->attr());
  93. $this->assertCount(1, $firstBook->attr());
  94. $this->assertNull($firstBook->attr("fail"));
  95. $this->assertNull($firstBook->attr("fail", null));
  96. $parser->debug = true;
  97. $this->assertEquals("test", $firstBook->attr("fail", "test"));
  98. $parser->debug = false;
  99. }
  100. public function testFnc()
  101. {
  102. }
  103. public function testAttributeFnc()
  104. {
  105. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  106. /*
  107. $this->assertCount(1, $parser->{"catalog book[id=bk101]:first-child"});
  108. $this->assertCount(1, $parser->{"catalog book:first-child[id=bk101]"});
  109. $this->assertCount(1, $parser->{"catalog :first-child[id=bk101]"});
  110. $this->assertCount(1, $parser->{"catalog [id=bk101]:first-child"});
  111. */
  112. }
  113. }