DomQuery.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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_Test
  17. * @subpackage PHPUnit
  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. /** @see PHPUnit_Framework_Constraint */
  23. require_once 'PHPUnit/Framework/Constraint.php';
  24. /** @see Zend_Dom_Query */
  25. require_once 'Zend/Dom/Query.php';
  26. /**
  27. * Zend_Dom_Query-based PHPUnit Constraint
  28. *
  29. * @uses PHPUnit_Framework_Constraint
  30. * @category Zend
  31. * @package Zend_Test
  32. * @subpackage PHPUnit
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Test_PHPUnit_Constraint_DomQuery extends PHPUnit_Framework_Constraint
  37. {
  38. /**#@+
  39. * Assertion type constants
  40. */
  41. const ASSERT_QUERY = 'assertQuery';
  42. const ASSERT_CONTENT_CONTAINS = 'assertQueryContentContains';
  43. const ASSERT_CONTENT_REGEX = 'assertQueryContentRegex';
  44. const ASSERT_CONTENT_COUNT = 'assertQueryCount';
  45. const ASSERT_CONTENT_COUNT_MIN= 'assertQueryCountMin';
  46. const ASSERT_CONTENT_COUNT_MAX= 'assertQueryCountMax';
  47. /**#@-*/
  48. /**
  49. * Current assertion type
  50. * @var string
  51. */
  52. protected $_assertType = null;
  53. /**
  54. * Available assertion types
  55. * @var array
  56. */
  57. protected $_assertTypes = array(
  58. self::ASSERT_QUERY,
  59. self::ASSERT_CONTENT_CONTAINS,
  60. self::ASSERT_CONTENT_REGEX,
  61. self::ASSERT_CONTENT_COUNT,
  62. self::ASSERT_CONTENT_COUNT_MIN,
  63. self::ASSERT_CONTENT_COUNT_MAX,
  64. );
  65. /**
  66. * Content being matched
  67. * @var string
  68. */
  69. protected $_content = null;
  70. /**
  71. * Whether or not assertion is negated
  72. * @var bool
  73. */
  74. protected $_negate = false;
  75. /**
  76. * CSS selector or XPath path to select against
  77. * @var string
  78. */
  79. protected $_path = null;
  80. /**
  81. * Whether or not to use XPath when querying
  82. * @var bool
  83. */
  84. protected $_useXpath = false;
  85. /**
  86. * XPath namespaces
  87. * @var array
  88. */
  89. protected $_xpathNamespaces = array();
  90. /**
  91. * Constructor; setup constraint state
  92. *
  93. * @param string $path CSS selector path
  94. * @return void
  95. */
  96. public function __construct($path)
  97. {
  98. $this->_path = $path;
  99. }
  100. /**
  101. * Indicate negative match
  102. *
  103. * @param bool $flag
  104. * @return void
  105. */
  106. public function setNegate($flag = true)
  107. {
  108. $this->_negate = $flag;
  109. }
  110. /**
  111. * Whether or not path is a straight XPath expression
  112. *
  113. * @param bool $flag
  114. * @return Zend_Test_PHPUnit_Constraint_DomQuery
  115. */
  116. public function setUseXpath($flag = true)
  117. {
  118. $this->_useXpath = (bool) $flag;
  119. return $this;
  120. }
  121. /**
  122. * Evaluate an object to see if it fits the constraints
  123. *
  124. * @param string $other String to examine
  125. * @param null|string Assertion type
  126. * @return bool
  127. */
  128. public function evaluate($other, $assertType = null)
  129. {
  130. if (strstr($assertType, 'Not')) {
  131. $this->setNegate(true);
  132. $assertType = str_replace('Not', '', $assertType);
  133. }
  134. if (strstr($assertType, 'Xpath')) {
  135. $this->setUseXpath(true);
  136. $assertType = str_replace('Xpath', 'Query', $assertType);
  137. }
  138. if (!in_array($assertType, $this->_assertTypes)) {
  139. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  140. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  141. }
  142. $this->_assertType = $assertType;
  143. $method = $this->_useXpath ? 'queryXpath' : 'query';
  144. $domQuery = new Zend_Dom_Query($other);
  145. $domQuery->registerXpathNamespaces($this->_xpathNamespaces);
  146. $result = $domQuery->$method($this->_path);
  147. $argv = func_get_args();
  148. $argc = func_num_args();
  149. switch ($assertType) {
  150. case self::ASSERT_CONTENT_CONTAINS:
  151. if (3 > $argc) {
  152. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  153. throw new Zend_Test_PHPUnit_Constraint_Exception('No content provided against which to match');
  154. }
  155. $this->_content = $content = $argv[2];
  156. return ($this->_negate)
  157. ? $this->_notMatchContent($result, $content)
  158. : $this->_matchContent($result, $content);
  159. case self::ASSERT_CONTENT_REGEX:
  160. if (3 > $argc) {
  161. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  162. throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match');
  163. }
  164. $this->_content = $content = $argv[2];
  165. return ($this->_negate)
  166. ? $this->_notRegexContent($result, $content)
  167. : $this->_regexContent($result, $content);
  168. case self::ASSERT_CONTENT_COUNT:
  169. case self::ASSERT_CONTENT_COUNT_MIN:
  170. case self::ASSERT_CONTENT_COUNT_MAX:
  171. if (3 > $argc) {
  172. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  173. throw new Zend_Test_PHPUnit_Constraint_Exception('No count provided against which to compare');
  174. }
  175. $this->_content = $content = $argv[2];
  176. return $this->_countContent($result, $content, $assertType);
  177. case self::ASSERT_QUERY:
  178. default:
  179. if ($this->_negate) {
  180. return (0 == count($result));
  181. } else {
  182. return (0 != count($result));
  183. }
  184. }
  185. }
  186. /**
  187. * Report Failure
  188. *
  189. * @see PHPUnit_Framework_Constraint for implementation details
  190. * @param mixed $other CSS selector path
  191. * @param string $description
  192. * @param bool $not
  193. * @return void
  194. * @throws PHPUnit_Framework_ExpectationFailedException
  195. */
  196. public function fail($other, $description, $not = false)
  197. {
  198. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  199. switch ($this->_assertType) {
  200. case self::ASSERT_CONTENT_CONTAINS:
  201. $failure = 'Failed asserting node denoted by %s CONTAINS content "%s"';
  202. if ($this->_negate) {
  203. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"';
  204. }
  205. $failure = sprintf($failure, $other, $this->_content);
  206. break;
  207. case self::ASSERT_CONTENT_REGEX:
  208. $failure = 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s"';
  209. if ($this->_negate) {
  210. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"';
  211. }
  212. $failure = sprintf($failure, $other, $this->_content);
  213. break;
  214. case self::ASSERT_CONTENT_COUNT:
  215. $failure = 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times';
  216. if ($this->_negate) {
  217. $failure = 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times';
  218. }
  219. $failure = sprintf($failure, $other, $this->_content);
  220. break;
  221. case self::ASSERT_CONTENT_COUNT_MIN:
  222. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times';
  223. $failure = sprintf($failure, $other, $this->_content);
  224. break;
  225. case self::ASSERT_CONTENT_COUNT_MAX:
  226. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times';
  227. $failure = sprintf($failure, $other, $this->_content);
  228. break;
  229. case self::ASSERT_QUERY:
  230. default:
  231. $failure = 'Failed asserting node DENOTED BY %s EXISTS';
  232. if ($this->_negate) {
  233. $failure = 'Failed asserting node DENOTED BY %s DOES NOT EXIST';
  234. }
  235. $failure = sprintf($failure, $other);
  236. break;
  237. }
  238. if (!empty($description)) {
  239. $failure = $description . "\n" . $failure;
  240. }
  241. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  242. }
  243. /**
  244. * Complete implementation
  245. *
  246. * @return string
  247. */
  248. public function toString()
  249. {
  250. return '';
  251. }
  252. /**
  253. * Register XPath namespaces
  254. *
  255. * @param array $xpathNamespaces
  256. * @return void
  257. */
  258. public function registerXpathNamespaces($xpathNamespaces)
  259. {
  260. $this->_xpathNamespaces = $xpathNamespaces;
  261. }
  262. /**
  263. * Check to see if content is matched in selected nodes
  264. *
  265. * @param Zend_Dom_Query_Result $result
  266. * @param string $match Content to match
  267. * @return bool
  268. */
  269. protected function _matchContent($result, $match)
  270. {
  271. if (0 == count($result)) {
  272. return false;
  273. }
  274. foreach ($result as $node) {
  275. $content = $this->_getNodeContent($node);
  276. if (strstr($content, $match)) {
  277. return true;
  278. }
  279. }
  280. return false;
  281. }
  282. /**
  283. * Check to see if content is NOT matched in selected nodes
  284. *
  285. * @param Zend_Dom_Query_Result $result
  286. * @param string $match
  287. * @return bool
  288. */
  289. protected function _notMatchContent($result, $match)
  290. {
  291. if (0 == count($result)) {
  292. return true;
  293. }
  294. foreach ($result as $node) {
  295. $content = $this->_getNodeContent($node);
  296. if (strstr($content, $match)) {
  297. return false;
  298. }
  299. }
  300. return true;
  301. }
  302. /**
  303. * Check to see if content is matched by regex in selected nodes
  304. *
  305. * @param Zend_Dom_Query_Result $result
  306. * @param string $pattern
  307. * @return bool
  308. */
  309. protected function _regexContent($result, $pattern)
  310. {
  311. if (0 == count($result)) {
  312. return false;
  313. }
  314. foreach ($result as $node) {
  315. $content = $this->_getNodeContent($node);
  316. if (preg_match($pattern, $content)) {
  317. return true;
  318. }
  319. }
  320. return false;
  321. }
  322. /**
  323. * Check to see if content is NOT matched by regex in selected nodes
  324. *
  325. * @param Zend_Dom_Query_Result $result
  326. * @param string $pattern
  327. * @return bool
  328. */
  329. protected function _notRegexContent($result, $pattern)
  330. {
  331. if (0 == count($result)) {
  332. return true;
  333. }
  334. foreach ($result as $node) {
  335. $content = $this->_getNodeContent($node);
  336. if (preg_match($pattern, $content)) {
  337. return false;
  338. }
  339. }
  340. return true;
  341. }
  342. /**
  343. * Determine if content count matches criteria
  344. *
  345. * @param Zend_Dom_Query_Result $result
  346. * @param int $test Value against which to test
  347. * @param string $type assertion type
  348. * @return boolean
  349. */
  350. protected function _countContent($result, $test, $type)
  351. {
  352. $count = count($result);
  353. switch ($type) {
  354. case self::ASSERT_CONTENT_COUNT:
  355. return ($this->_negate)
  356. ? ($test != $count)
  357. : ($test == $count);
  358. case self::ASSERT_CONTENT_COUNT_MIN:
  359. return ($count >= $test);
  360. case self::ASSERT_CONTENT_COUNT_MAX:
  361. return ($count <= $test);
  362. default:
  363. return false;
  364. }
  365. }
  366. /**
  367. * Get node content, minus node markup tags
  368. *
  369. * @param DOMNode $node
  370. * @return string
  371. */
  372. protected function _getNodeContent(DOMNode $node)
  373. {
  374. if ($node instanceof DOMAttr) {
  375. return $node->value;
  376. } else {
  377. $doc = $node->ownerDocument;
  378. $content = $doc->saveXML($node);
  379. $tag = $node->nodeName;
  380. $regex = '|</?' . $tag . '[^>]*>|';
  381. return preg_replace($regex, '', $content);
  382. }
  383. }
  384. }