QueryTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dom_QueryTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. /** Zend_Dom_Query */
  28. require_once 'Zend/Dom/Query.php';
  29. /**
  30. * Test class for Zend_Dom_Query.
  31. *
  32. * @category Zend
  33. * @package Zend_Dom
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Dom
  38. */
  39. class Zend_Dom_QueryTest extends PHPUnit_Framework_TestCase
  40. {
  41. public $html;
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_Dom_QueryTest");
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. /**
  53. * Sets up the fixture, for example, open a network connection.
  54. * This method is called before a test is executed.
  55. *
  56. * @return void
  57. */
  58. public function setUp()
  59. {
  60. $this->query = new Zend_Dom_Query();
  61. }
  62. /**
  63. * Tears down the fixture, for example, close a network connection.
  64. * This method is called after a test is executed.
  65. *
  66. * @return void
  67. */
  68. public function tearDown()
  69. {
  70. }
  71. public function getHtml()
  72. {
  73. if (null === $this->html) {
  74. $this->html = file_get_contents(dirname(__FILE__) . '/_files/sample.xhtml');
  75. }
  76. return $this->html;
  77. }
  78. public function loadHtml()
  79. {
  80. $this->query->setDocument($this->getHtml());
  81. }
  82. public function handleError($msg, $code = 0)
  83. {
  84. $this->error = $msg;
  85. }
  86. public function testConstructorShouldNotRequireArguments()
  87. {
  88. $query = new Zend_Dom_Query();
  89. }
  90. public function testConstructorShouldAcceptDocumentString()
  91. {
  92. $html = $this->getHtml();
  93. $query = new Zend_Dom_Query($html);
  94. $this->assertSame($html, $query->getDocument());
  95. }
  96. public function testDocShouldBeNullByDefault()
  97. {
  98. $this->assertNull($this->query->getDocument());
  99. }
  100. public function testDocShouldBeNullByEmptyStringConstructor()
  101. {
  102. $emptyStr = "";
  103. $query = new Zend_Dom_Query($emptyStr);
  104. $this->assertNull($this->query->getDocument());
  105. }
  106. public function testDocShouldBeNullByEmptyStringSet()
  107. {
  108. $emptyStr = "";
  109. $this->query->setDocument($emptyStr);
  110. $this->assertNull($this->query->getDocument());
  111. }
  112. public function testDocTypeShouldBeNullByDefault()
  113. {
  114. $this->assertNull($this->query->getDocumentType());
  115. }
  116. public function testShouldAllowSettingDocument()
  117. {
  118. $this->testDocShouldBeNullByDefault();
  119. $this->loadHtml();
  120. $this->assertEquals($this->getHtml(), $this->query->getDocument());
  121. }
  122. public function testDocumentTypeShouldBeAutomaticallyDiscovered()
  123. {
  124. $this->loadHtml();
  125. $this->assertEquals(Zend_Dom_Query::DOC_XHTML, $this->query->getDocumentType());
  126. $this->query->setDocument('<?xml version="1.0"?><root></root>');
  127. $this->assertEquals(Zend_Dom_Query::DOC_XML, $this->query->getDocumentType());
  128. $this->query->setDocument('<html><body></body></html>');
  129. $this->assertEquals(Zend_Dom_Query::DOC_HTML, $this->query->getDocumentType());
  130. }
  131. public function testQueryingWithoutRegisteringDocumentShouldThrowException()
  132. {
  133. try {
  134. $this->query->query('.foo');
  135. $this->fail('Querying without registering document should throw exception');
  136. } catch (Zend_Dom_Exception $e) {
  137. $this->assertContains('no document', $e->getMessage());
  138. }
  139. }
  140. public function testQueryingInvalidDocumentShouldThrowException()
  141. {
  142. set_error_handler(array($this, 'handleError'));
  143. $this->query->setDocumentXml('some bogus string');
  144. try {
  145. $this->query->query('.foo');
  146. restore_error_handler();
  147. $this->fail('Querying invalid document should throw exception');
  148. } catch (Zend_Dom_Exception $e) {
  149. restore_error_handler();
  150. $this->assertContains('Error parsing', $e->getMessage());
  151. }
  152. }
  153. public function testQueryShouldReturnResultObject()
  154. {
  155. $this->loadHtml();
  156. $test = $this->query->query('.foo');
  157. $this->assertTrue($test instanceof Zend_Dom_Query_Result);
  158. }
  159. public function testResultShouldIndicateNumberOfFoundNodes()
  160. {
  161. $this->loadHtml();
  162. $result = $this->query->query('.foo');
  163. $message = 'Xpath: ' . $result->getXpathQuery() . "\n";
  164. $this->assertEquals(3, count($result), $message);
  165. }
  166. public function testResultShouldAllowIteratingOverFoundNodes()
  167. {
  168. $this->loadHtml();
  169. $result = $this->query->query('.foo');
  170. $this->assertEquals(3, count($result));
  171. foreach ($result as $node) {
  172. $this->assertTrue($node instanceof DOMNode, var_export($result, 1));
  173. }
  174. }
  175. public function testQueryShouldFindNodesWithMultipleClasses()
  176. {
  177. $this->loadHtml();
  178. $result = $this->query->query('.footerblock .last');
  179. $this->assertEquals(1, count($result), $result->getXpathQuery());
  180. }
  181. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsExactly()
  182. {
  183. $this->loadHtml();
  184. $result = $this->query->query('div[dojoType="FilteringSelect"]');
  185. $this->assertEquals(1, count($result), $result->getXpathQuery());
  186. }
  187. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAsDiscreteWords()
  188. {
  189. $this->loadHtml();
  190. $result = $this->query->query('li[dojoType~="bar"]');
  191. $this->assertEquals(2, count($result), $result->getXpathQuery());
  192. }
  193. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAndAttributeValue()
  194. {
  195. $this->loadHtml();
  196. $result = $this->query->query('li[dojoType*="bar"]');
  197. $this->assertEquals(2, count($result), $result->getXpathQuery());
  198. }
  199. public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
  200. {
  201. $this->loadHtml();
  202. $result = $this->query->queryXpath('//li[contains(@dojotype, "bar")]');
  203. $this->assertEquals(2, count($result), $result->getXpathQuery());
  204. }
  205. /**
  206. * @group ZF-9243
  207. */
  208. public function testLoadingDocumentWithErrorsShouldNotRaisePhpErrors()
  209. {
  210. $file = file_get_contents(dirname(__FILE__) . '/_files/bad-sample.html');
  211. $this->query->setDocument($file);
  212. $this->query->query('p');
  213. $errors = $this->query->getDocumentErrors();
  214. $this->assertTrue(is_array($errors));
  215. $this->assertTrue(0 < count($errors));
  216. }
  217. }
  218. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  219. if (PHPUnit_MAIN_METHOD == "Zend_Dom_QueryTest::main") {
  220. Zend_Dom_QueryTest::main();
  221. }