QueryTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 testDocShouldBeNullByEmptyStringConstructor()
  70. {
  71. $emptyStr = "";
  72. $query = new Zend_Dom_Query($emptyStr);
  73. $this->assertNull($this->query->getDocument());
  74. }
  75. public function testDocShouldBeNullByEmptyStringSet()
  76. {
  77. $emptyStr = "";
  78. $this->query->setDocument($emptyStr);
  79. $this->assertNull($this->query->getDocument());
  80. }
  81. public function testDocTypeShouldBeNullByDefault()
  82. {
  83. $this->assertNull($this->query->getDocumentType());
  84. }
  85. public function testShouldAllowSettingDocument()
  86. {
  87. $this->testDocShouldBeNullByDefault();
  88. $this->loadHtml();
  89. $this->assertEquals($this->getHtml(), $this->query->getDocument());
  90. }
  91. public function testDocumentTypeShouldBeAutomaticallyDiscovered()
  92. {
  93. $this->loadHtml();
  94. $this->assertEquals(Zend_Dom_Query::DOC_XHTML, $this->query->getDocumentType());
  95. $this->query->setDocument('<?xml version="1.0"?><root></root>');
  96. $this->assertEquals(Zend_Dom_Query::DOC_XML, $this->query->getDocumentType());
  97. $this->query->setDocument('<html><body></body></html>');
  98. $this->assertEquals(Zend_Dom_Query::DOC_HTML, $this->query->getDocumentType());
  99. }
  100. public function testQueryingWithoutRegisteringDocumentShouldThrowException()
  101. {
  102. try {
  103. $this->query->query('.foo');
  104. $this->fail('Querying without registering document should throw exception');
  105. } catch (Zend_Dom_Exception $e) {
  106. $this->assertContains('no document', $e->getMessage());
  107. }
  108. }
  109. public function testQueryingInvalidDocumentShouldThrowException()
  110. {
  111. $this->query->setDocumentXml('some bogus string');
  112. try {
  113. $this->query->query('.foo');
  114. $this->fail('Querying invalid document should throw exception');
  115. } catch (Zend_Dom_Exception $e) {
  116. $this->assertContains('Error parsing', $e->getMessage());
  117. }
  118. }
  119. public function testQueryShouldReturnResultObject()
  120. {
  121. $this->loadHtml();
  122. $test = $this->query->query('.foo');
  123. $this->assertTrue($test instanceof Zend_Dom_Query_Result);
  124. }
  125. public function testResultShouldIndicateNumberOfFoundNodes()
  126. {
  127. $this->loadHtml();
  128. $result = $this->query->query('.foo');
  129. $message = 'Xpath: ' . $result->getXpathQuery() . "\n";
  130. $this->assertEquals(3, count($result), $message);
  131. }
  132. public function testResultShouldAllowIteratingOverFoundNodes()
  133. {
  134. $this->loadHtml();
  135. $result = $this->query->query('.foo');
  136. $this->assertEquals(3, count($result));
  137. foreach ($result as $node) {
  138. $this->assertTrue($node instanceof DOMNode, var_export($result, 1));
  139. }
  140. }
  141. public function testQueryShouldFindNodesWithMultipleClasses()
  142. {
  143. $this->loadHtml();
  144. $result = $this->query->query('.footerblock .last');
  145. $this->assertEquals(1, count($result), $result->getXpathQuery());
  146. }
  147. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsExactly()
  148. {
  149. $this->loadHtml();
  150. $result = $this->query->query('div[dojoType="FilteringSelect"]');
  151. $this->assertEquals(1, count($result), $result->getXpathQuery());
  152. }
  153. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAsDiscreteWords()
  154. {
  155. $this->loadHtml();
  156. $result = $this->query->query('li[dojoType~="bar"]');
  157. $this->assertEquals(2, count($result), $result->getXpathQuery());
  158. }
  159. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAndAttributeValue()
  160. {
  161. $this->loadHtml();
  162. $result = $this->query->query('li[dojoType*="bar"]');
  163. $this->assertEquals(2, count($result), $result->getXpathQuery());
  164. }
  165. public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
  166. {
  167. $this->loadHtml();
  168. $result = $this->query->queryXpath('//li[contains(@dojotype, "bar")]');
  169. $this->assertEquals(2, count($result), $result->getXpathQuery());
  170. }
  171. }
  172. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  173. if (PHPUnit_MAIN_METHOD == "Zend_Dom_QueryTest::main") {
  174. Zend_Dom_QueryTest::main();
  175. }