QueryTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Dom_QueryTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../TestHelper.php';
  7. /** Zend_Dom_Query */
  8. require_once 'Zend/Dom/Query.php';
  9. /**
  10. * Test class for Zend_Dom_Query.
  11. */
  12. class Zend_Dom_QueryTest extends PHPUnit_Framework_TestCase
  13. {
  14. public $html;
  15. /**
  16. * Runs the test methods of this class.
  17. *
  18. * @return void
  19. */
  20. public static function main()
  21. {
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_Dom_QueryTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. /**
  26. * Sets up the fixture, for example, open a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. $this->query = new Zend_Dom_Query();
  34. }
  35. /**
  36. * Tears down the fixture, for example, close a network connection.
  37. * This method is called after a test is executed.
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. }
  44. public function getHtml()
  45. {
  46. if (null === $this->html) {
  47. $this->html = file_get_contents(dirname(__FILE__) . '/_files/sample.xhtml');
  48. }
  49. return $this->html;
  50. }
  51. public function loadHtml()
  52. {
  53. $this->query->setDocument($this->getHtml());
  54. }
  55. public function testConstructorShouldNotRequireArguments()
  56. {
  57. $query = new Zend_Dom_Query();
  58. }
  59. public function testConstructorShouldAcceptDocumentString()
  60. {
  61. $html = $this->getHtml();
  62. $query = new Zend_Dom_Query($html);
  63. $this->assertSame($html, $query->getDocument());
  64. }
  65. public function testDocShouldBeNullByDefault()
  66. {
  67. $this->assertNull($this->query->getDocument());
  68. }
  69. public function testDocTypeShouldBeNullByDefault()
  70. {
  71. $this->assertNull($this->query->getDocumentType());
  72. }
  73. public function testShouldAllowSettingDocument()
  74. {
  75. $this->testDocShouldBeNullByDefault();
  76. $this->loadHtml();
  77. $this->assertEquals($this->getHtml(), $this->query->getDocument());
  78. }
  79. public function testDocumentTypeShouldBeAutomaticallyDiscovered()
  80. {
  81. $this->loadHtml();
  82. $this->assertEquals(Zend_Dom_Query::DOC_XHTML, $this->query->getDocumentType());
  83. $this->query->setDocument('<?xml version="1.0"?><root></root>');
  84. $this->assertEquals(Zend_Dom_Query::DOC_XML, $this->query->getDocumentType());
  85. $this->query->setDocument('<html><body></body></html>');
  86. $this->assertEquals(Zend_Dom_Query::DOC_HTML, $this->query->getDocumentType());
  87. }
  88. public function testQueryingWithoutRegisteringDocumentShouldThrowException()
  89. {
  90. try {
  91. $this->query->query('.foo');
  92. $this->fail('Querying without registering document should throw exception');
  93. } catch (Zend_Dom_Exception $e) {
  94. $this->assertContains('no document', $e->getMessage());
  95. }
  96. }
  97. public function testQueryingInvalidDocumentShouldThrowException()
  98. {
  99. $this->query->setDocumentXml('some bogus string');
  100. try {
  101. $this->query->query('.foo');
  102. $this->fail('Querying invalid document should throw exception');
  103. } catch (Zend_Dom_Exception $e) {
  104. $this->assertContains('Error parsing', $e->getMessage());
  105. }
  106. }
  107. public function testQueryShouldReturnResultObject()
  108. {
  109. $this->loadHtml();
  110. $test = $this->query->query('.foo');
  111. $this->assertTrue($test instanceof Zend_Dom_Query_Result);
  112. }
  113. public function testResultShouldIndicateNumberOfFoundNodes()
  114. {
  115. $this->loadHtml();
  116. $result = $this->query->query('.foo');
  117. $message = 'Xpath: ' . $result->getXpathQuery() . "\n";
  118. $this->assertEquals(3, count($result), $message);
  119. }
  120. public function testResultShouldAllowIteratingOverFoundNodes()
  121. {
  122. $this->loadHtml();
  123. $result = $this->query->query('.foo');
  124. $this->assertEquals(3, count($result));
  125. foreach ($result as $node) {
  126. $this->assertTrue($node instanceof DOMNode, var_export($result, 1));
  127. }
  128. }
  129. public function testQueryShouldFindNodesWithMultipleClasses()
  130. {
  131. $this->loadHtml();
  132. $result = $this->query->query('.footerblock .last');
  133. $this->assertEquals(1, count($result), $result->getXpathQuery());
  134. }
  135. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsExactly()
  136. {
  137. $this->loadHtml();
  138. $result = $this->query->query('div[dojoType="FilteringSelect"]');
  139. $this->assertEquals(1, count($result), $result->getXpathQuery());
  140. }
  141. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAsDiscreteWords()
  142. {
  143. $this->loadHtml();
  144. $result = $this->query->query('li[dojoType~="bar"]');
  145. $this->assertEquals(2, count($result), $result->getXpathQuery());
  146. }
  147. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAndAttributeValue()
  148. {
  149. $this->loadHtml();
  150. $result = $this->query->query('li[dojoType*="bar"]');
  151. $this->assertEquals(2, count($result), $result->getXpathQuery());
  152. }
  153. public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
  154. {
  155. $this->loadHtml();
  156. $result = $this->query->queryXpath('//li[contains(@dojotype, "bar")]');
  157. $this->assertEquals(2, count($result), $result->getXpathQuery());
  158. }
  159. }
  160. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  161. if (PHPUnit_MAIN_METHOD == "Zend_Dom_QueryTest::main") {
  162. Zend_Dom_QueryTest::main();
  163. }