PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Test/PHPUnit/Constraint/DomQuery.php

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