DomQuery37.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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-2012 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-2012 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 Response content to be matched against (haystack)
  125. * @param null|string Assertion type
  126. * @param string (optional) String to match (needle), may be required depending on assertion type
  127. * @return bool
  128. *
  129. * NOTE:
  130. * Drastic changes up to PHPUnit 3.5.15 this was:
  131. * public function evaluate($other, $assertType = null)
  132. * In PHPUnit 3.6.0 they changed the interface into this:
  133. * public function evaluate($other, $description = '', $returnResult = FALSE)
  134. * We use the new interface for PHP-strict checking, but emulate the old one
  135. */
  136. public function evaluate($content, $assertType = '', $match = FALSE)
  137. {
  138. if (strstr($assertType, 'Not')) {
  139. $this->setNegate(true);
  140. $assertType = str_replace('Not', '', $assertType);
  141. }
  142. if (strstr($assertType, 'Xpath')) {
  143. $this->setUseXpath(true);
  144. $assertType = str_replace('Xpath', 'Query', $assertType);
  145. }
  146. if (!in_array($assertType, $this->_assertTypes)) {
  147. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  148. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  149. }
  150. $this->_assertType = $assertType;
  151. $method = $this->_useXpath ? 'queryXpath' : 'query';
  152. $domQuery = new Zend_Dom_Query($content);
  153. $domQuery->registerXpathNamespaces($this->_xpathNamespaces);
  154. $result = $domQuery->$method($this->_path);
  155. switch ($assertType) {
  156. case self::ASSERT_CONTENT_CONTAINS:
  157. if (!$match) {
  158. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  159. throw new Zend_Test_PHPUnit_Constraint_Exception('No content provided against which to match');
  160. }
  161. $this->_content = $match;
  162. return ($this->_negate)
  163. ? $this->_notMatchContent($result, $match)
  164. : $this->_matchContent($result, $match);
  165. case self::ASSERT_CONTENT_REGEX:
  166. if (!$match) {
  167. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  168. throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match');
  169. }
  170. $this->_content = $match;
  171. return ($this->_negate)
  172. ? $this->_notRegexContent($result, $match)
  173. : $this->_regexContent($result, $match);
  174. case self::ASSERT_CONTENT_COUNT:
  175. case self::ASSERT_CONTENT_COUNT_MIN:
  176. case self::ASSERT_CONTENT_COUNT_MAX:
  177. if (!$match) {
  178. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  179. throw new Zend_Test_PHPUnit_Constraint_Exception('No count provided against which to compare');
  180. }
  181. $this->_content = $match;
  182. return $this->_countContent($result, $match, $assertType);
  183. case self::ASSERT_QUERY:
  184. default:
  185. if ($this->_negate) {
  186. return (0 == count($result));
  187. } else {
  188. return (0 != count($result));
  189. }
  190. }
  191. }
  192. /**
  193. * Report Failure
  194. *
  195. * @see PHPUnit_Framework_Constraint for implementation details
  196. * @param mixed CSS selector path
  197. * @param string Failure description
  198. * @param object Cannot be used, null
  199. * @return void
  200. * @throws PHPUnit_Framework_ExpectationFailedException
  201. * NOTE:
  202. * Drastic changes up to PHPUnit 3.5.15 this was:
  203. * public function fail($other, $description, $not = false)
  204. * In PHPUnit 3.6.0 they changed the interface into this:
  205. * protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)
  206. * We use the new interface for PHP-strict checking
  207. */
  208. public function fail($other, $description, PHPUnit_Framework_ComparisonFailure $cannot_be_used = NULL)
  209. {
  210. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  211. switch ($this->_assertType) {
  212. case self::ASSERT_CONTENT_CONTAINS:
  213. $failure = 'Failed asserting node denoted by %s CONTAINS content "%s"';
  214. if ($this->_negate) {
  215. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"';
  216. }
  217. $failure = sprintf($failure, $other, $this->_content);
  218. break;
  219. case self::ASSERT_CONTENT_REGEX:
  220. $failure = 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s"';
  221. if ($this->_negate) {
  222. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"';
  223. }
  224. $failure = sprintf($failure, $other, $this->_content);
  225. break;
  226. case self::ASSERT_CONTENT_COUNT:
  227. $failure = 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times';
  228. if ($this->_negate) {
  229. $failure = 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times';
  230. }
  231. $failure = sprintf($failure, $other, $this->_content);
  232. break;
  233. case self::ASSERT_CONTENT_COUNT_MIN:
  234. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times';
  235. $failure = sprintf($failure, $other, $this->_content);
  236. break;
  237. case self::ASSERT_CONTENT_COUNT_MAX:
  238. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times';
  239. $failure = sprintf($failure, $other, $this->_content);
  240. break;
  241. case self::ASSERT_QUERY:
  242. default:
  243. $failure = 'Failed asserting node DENOTED BY %s EXISTS';
  244. if ($this->_negate) {
  245. $failure = 'Failed asserting node DENOTED BY %s DOES NOT EXIST';
  246. }
  247. $failure = sprintf($failure, $other);
  248. break;
  249. }
  250. if (!empty($description)) {
  251. $failure = $description . "\n" . $failure;
  252. }
  253. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  254. }
  255. /**
  256. * Complete implementation
  257. *
  258. * @return string
  259. */
  260. public function toString()
  261. {
  262. return '';
  263. }
  264. /**
  265. * Register XPath namespaces
  266. *
  267. * @param array $xpathNamespaces
  268. * @return void
  269. */
  270. public function registerXpathNamespaces($xpathNamespaces)
  271. {
  272. $this->_xpathNamespaces = $xpathNamespaces;
  273. }
  274. /**
  275. * Check to see if content is matched in selected nodes
  276. *
  277. * @param Zend_Dom_Query_Result $result
  278. * @param string $match Content to match
  279. * @return bool
  280. */
  281. protected function _matchContent($result, $match)
  282. {
  283. $match = (string) $match;
  284. if (0 == count($result)) {
  285. return false;
  286. }
  287. foreach ($result as $node) {
  288. $content = $this->_getNodeContent($node);
  289. if (strstr($content, $match)) {
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. /**
  296. * Check to see if content is NOT matched in selected nodes
  297. *
  298. * @param Zend_Dom_Query_Result $result
  299. * @param string $match
  300. * @return bool
  301. */
  302. protected function _notMatchContent($result, $match)
  303. {
  304. if (0 == count($result)) {
  305. return true;
  306. }
  307. foreach ($result as $node) {
  308. $content = $this->_getNodeContent($node);
  309. if (strstr($content, $match)) {
  310. return false;
  311. }
  312. }
  313. return true;
  314. }
  315. /**
  316. * Check to see if content is matched by regex in selected nodes
  317. *
  318. * @param Zend_Dom_Query_Result $result
  319. * @param string $pattern
  320. * @return bool
  321. */
  322. protected function _regexContent($result, $pattern)
  323. {
  324. if (0 == count($result)) {
  325. return false;
  326. }
  327. foreach ($result as $node) {
  328. $content = $this->_getNodeContent($node);
  329. if (preg_match($pattern, $content)) {
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335. /**
  336. * Check to see if content is NOT matched by regex in selected nodes
  337. *
  338. * @param Zend_Dom_Query_Result $result
  339. * @param string $pattern
  340. * @return bool
  341. */
  342. protected function _notRegexContent($result, $pattern)
  343. {
  344. if (0 == count($result)) {
  345. return true;
  346. }
  347. foreach ($result as $node) {
  348. $content = $this->_getNodeContent($node);
  349. if (preg_match($pattern, $content)) {
  350. return false;
  351. }
  352. }
  353. return true;
  354. }
  355. /**
  356. * Determine if content count matches criteria
  357. *
  358. * @param Zend_Dom_Query_Result $result
  359. * @param int $test Value against which to test
  360. * @param string $type assertion type
  361. * @return boolean
  362. */
  363. protected function _countContent($result, $test, $type)
  364. {
  365. $count = count($result);
  366. switch ($type) {
  367. case self::ASSERT_CONTENT_COUNT:
  368. return ($this->_negate)
  369. ? ($test != $count)
  370. : ($test == $count);
  371. case self::ASSERT_CONTENT_COUNT_MIN:
  372. return ($count >= $test);
  373. case self::ASSERT_CONTENT_COUNT_MAX:
  374. return ($count <= $test);
  375. default:
  376. return false;
  377. }
  378. }
  379. /**
  380. * Get node content, minus node markup tags
  381. *
  382. * @param DOMNode $node
  383. * @return string
  384. */
  385. protected function _getNodeContent(DOMNode $node)
  386. {
  387. if ($node instanceof DOMAttr) {
  388. return $node->value;
  389. } else {
  390. $doc = $node->ownerDocument;
  391. $content = $doc->saveXML($node);
  392. $tag = $node->nodeName;
  393. $regex = '|</?' . $tag . '[^>]*>|';
  394. return preg_replace($regex, '', $content);
  395. }
  396. }
  397. }