QueryTest.php 7.0 KB

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