PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/app/webroot/frameworks/Zend/Test/PHPUnit/Constraint/DomQuery.php

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