test.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. require_once(__DIR__.'/xmlStroller.php');
  3. class XmlStrollerTester extends PHPUnit_Framework_TestCase
  4. {
  5. public function testLoad()
  6. {
  7. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  8. $success = true;
  9. try {
  10. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/booksFail.xml")));
  11. $success = false;
  12. }
  13. catch (Exception $e) {}
  14. $this->assertTrue($success);
  15. }
  16. public function testSelectSimpleChild()
  17. {
  18. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  19. $this->assertCount(1, $parser->catalog);
  20. $this->assertCount(13, $parser->{"catalog book"});
  21. $this->assertCount(13, $parser->{"catalog book"});
  22. $this->assertCount(13, $parser->{"book"});
  23. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"catalog book"});
  24. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"catalog book"});
  25. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"book"});
  26. $this->assertCount(0, $parser->{"fail"});
  27. $this->assertCount(0, $parser->{"catalog fail"});
  28. $this->assertCount(0, $parser->{"book fail"});
  29. }
  30. public function testSelectDirectChild()
  31. {
  32. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  33. $this->assertCount(1, $parser->{">catalog"});
  34. $this->assertCount(1, $parser->{"> catalog"});
  35. $this->assertCount(1, $parser->{" > catalog"});
  36. $this->assertCount(13, $parser->{"catalog>book"});
  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->assertContainsOnlyInstancesOf("XmlStroller", $parser->{">catalog>book"});
  45. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"catalog>book"});
  46. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"catalog >book"});
  47. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"catalog> book"});
  48. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"catalog > book"});
  49. $this->assertContainsOnlyInstancesOf("XmlStroller", $parser->{"catalog > book"});
  50. $this->assertCount(0, $parser->{"catalog > fail"});
  51. $this->assertCount(0, $parser->{">fail"});
  52. }
  53. public function testSelectAttribute()
  54. {
  55. $parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
  56. $book = $parser->{"catalog book[id=bk101]"};
  57. $this->assertCount(1, $book);
  58. $this->assertEquals("bk101", $book[0]->attr("id"));
  59. $book = $parser->{"catalog [id=bk101]book"};
  60. $this->assertCount(1, $book);
  61. $this->assertEquals("bk101", $book[0]->attr("id"));
  62. $book = $parser->{"catalog [id=bk101]"};
  63. $this->assertCount(1, $book);
  64. $this->assertEquals("bk101", $book[0]->attr("id"));
  65. $this->assertCount(0, $book = $parser->{"catalog[id=oupas]"});
  66. $this->assertCount(12, $parser->{"catalog [id]"});
  67. $this->assertCount(12, $parser->{"catalog [id]book"});
  68. $this->assertCount(12, $parser->{"catalog book[id]"});
  69. $this->assertCount(12, $parser->{"catalog book[id] *[currency]"});
  70. $this->assertCount(12, $parser->{"catalog book[id] [currency]"});
  71. $this->assertCount(12, $parser->{"catalog book[id] price[currency]"});
  72. $this->assertCount(13, $parser->{"catalog book price[currency=USD]"});
  73. $this->assertCount(1, $parser->{"catalog book price[currency=EURO]"});
  74. $books = $parser->{"price[currency=USD]"};
  75. $this->assertEquals(49.90, $books[count($books) -1]->valueDouble());
  76. $this->assertEquals(39.95, $parser->{"catalog book price[currency=EURO]"}[0]->valueDouble());
  77. $book = $parser->{"book price[currency=EURO]"}[0];
  78. $book = $book->parent();
  79. var_dump($book);
  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. }