DomQuery34.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 $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. $match = (string) $match;
  272. if (0 == count($result)) {
  273. return false;
  274. }
  275. foreach ($result as $node) {
  276. $content = $this->_getNodeContent($node);
  277. if (strstr($content, $match)) {
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. /**
  284. * Check to see if content is NOT matched in selected nodes
  285. *
  286. * @param Zend_Dom_Query_Result $result
  287. * @param string $match
  288. * @return bool
  289. */
  290. protected function _notMatchContent($result, $match)
  291. {
  292. if (0 == count($result)) {
  293. return true;
  294. }
  295. foreach ($result as $node) {
  296. $content = $this->_getNodeContent($node);
  297. if (strstr($content, $match)) {
  298. return false;
  299. }
  300. }
  301. return true;
  302. }
  303. /**
  304. * Check to see if content is matched by regex in selected nodes
  305. *
  306. * @param Zend_Dom_Query_Result $result
  307. * @param string $pattern
  308. * @return bool
  309. */
  310. protected function _regexContent($result, $pattern)
  311. {
  312. if (0 == count($result)) {
  313. return false;
  314. }
  315. foreach ($result as $node) {
  316. $content = $this->_getNodeContent($node);
  317. if (preg_match($pattern, $content)) {
  318. return true;
  319. }
  320. }
  321. return false;
  322. }
  323. /**
  324. * Check to see if content is NOT matched by regex in selected nodes
  325. *
  326. * @param Zend_Dom_Query_Result $result
  327. * @param string $pattern
  328. * @return bool
  329. */
  330. protected function _notRegexContent($result, $pattern)
  331. {
  332. if (0 == count($result)) {
  333. return true;
  334. }
  335. foreach ($result as $node) {
  336. $content = $this->_getNodeContent($node);
  337. if (preg_match($pattern, $content)) {
  338. return false;
  339. }
  340. }
  341. return true;
  342. }
  343. /**
  344. * Determine if content count matches criteria
  345. *
  346. * @param Zend_Dom_Query_Result $result
  347. * @param int $test Value against which to test
  348. * @param string $type assertion type
  349. * @return boolean
  350. */
  351. protected function _countContent($result, $test, $type)
  352. {
  353. $count = count($result);
  354. switch ($type) {
  355. case self::ASSERT_CONTENT_COUNT:
  356. return ($this->_negate)
  357. ? ($test != $count)
  358. : ($test == $count);
  359. case self::ASSERT_CONTENT_COUNT_MIN:
  360. return ($count >= $test);
  361. case self::ASSERT_CONTENT_COUNT_MAX:
  362. return ($count <= $test);
  363. default:
  364. return false;
  365. }
  366. }
  367. /**
  368. * Get node content, minus node markup tags
  369. *
  370. * @param DOMNode $node
  371. * @return string
  372. */
  373. protected function _getNodeContent(DOMNode $node)
  374. {
  375. if ($node instanceof DOMAttr) {
  376. return $node->value;
  377. } else {
  378. $doc = $node->ownerDocument;
  379. $content = $doc->saveXML($node);
  380. $tag = $node->nodeName;
  381. $regex = '|</?' . $tag . '[^>]*>|';
  382. return preg_replace($regex, '', $content);
  383. }
  384. }
  385. }