test_SimpleXml.php 5.0 KB

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