PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lanoversolutions/afterschool
PHP | 405 lines | 226 code | 37 blank | 142 comment | 34 complexity | 04fc81bc6a2b5873b944673ab6adaac7 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. * @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: DomQuery.php 20096 2010-01-06 02:05:09Z bkarwin $
  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. * Constructor; setup constraint state
  87. *
  88. * @param string $path CSS selector path
  89. * @return void
  90. */
  91. public function __construct($path)
  92. {
  93. $this->_path = $path;
  94. }
  95. /**
  96. * Indicate negative match
  97. *
  98. * @param bool $flag
  99. * @return void
  100. */
  101. public function setNegate($flag = true)
  102. {
  103. $this->_negate = $flag;
  104. }
  105. /**
  106. * Whether or not path is a straight XPath expression
  107. *
  108. * @param bool $flag
  109. * @return Zend_Test_PHPUnit_Constraint_DomQuery
  110. */
  111. public function setUseXpath($flag = true)
  112. {
  113. $this->_useXpath = (bool) $flag;
  114. return $this;
  115. }
  116. /**
  117. * Evaluate an object to see if it fits the constraints
  118. *
  119. * @param string $other String to examine
  120. * @param null|string Assertion type
  121. * @return bool
  122. */
  123. public function evaluate($other, $assertType = null)
  124. {
  125. if (strstr($assertType, 'Not')) {
  126. $this->setNegate(true);
  127. $assertType = str_replace('Not', '', $assertType);
  128. }
  129. if (strstr($assertType, 'Xpath')) {
  130. $this->setUseXpath(true);
  131. $assertType = str_replace('Xpath', 'Query', $assertType);
  132. }
  133. if (!in_array($assertType, $this->_assertTypes)) {
  134. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  135. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  136. }
  137. $this->_assertType = $assertType;
  138. $method = $this->_useXpath ? 'queryXpath' : 'query';
  139. $domQuery = new Zend_Dom_Query($other);
  140. $result = $domQuery->$method($this->_path);
  141. $argv = func_get_args();
  142. $argc = func_num_args();
  143. switch ($assertType) {
  144. case self::ASSERT_CONTENT_CONTAINS:
  145. if (3 > $argc) {
  146. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  147. throw new Zend_Test_PHPUnit_Constraint_Exception('No content provided against which to match');
  148. }
  149. $this->_content = $content = $argv[2];
  150. return ($this->_negate)
  151. ? $this->_notMatchContent($result, $content)
  152. : $this->_matchContent($result, $content);
  153. case self::ASSERT_CONTENT_REGEX:
  154. if (3 > $argc) {
  155. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  156. throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match');
  157. }
  158. $this->_content = $content = $argv[2];
  159. return ($this->_negate)
  160. ? $this->_notRegexContent($result, $content)
  161. : $this->_regexContent($result, $content);
  162. case self::ASSERT_CONTENT_COUNT:
  163. case self::ASSERT_CONTENT_COUNT_MIN:
  164. case self::ASSERT_CONTENT_COUNT_MAX:
  165. if (3 > $argc) {
  166. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  167. throw new Zend_Test_PHPUnit_Constraint_Exception('No count provided against which to compare');
  168. }
  169. $this->_content = $content = $argv[2];
  170. return $this->_countContent($result, $content, $assertType);
  171. case self::ASSERT_QUERY:
  172. default:
  173. if ($this->_negate) {
  174. return (0 == count($result));
  175. } else {
  176. return (0 != count($result));
  177. }
  178. }
  179. }
  180. /**
  181. * Report Failure
  182. *
  183. * @see PHPUnit_Framework_Constraint for implementation details
  184. * @param mixed $other CSS selector path
  185. * @param string $description
  186. * @param bool $not
  187. * @return void
  188. * @throws PHPUnit_Framework_ExpectationFailedException
  189. */
  190. public function fail($other, $description, $not = false)
  191. {
  192. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  193. switch ($this->_assertType) {
  194. case self::ASSERT_CONTENT_CONTAINS:
  195. $failure = 'Failed asserting node denoted by %s CONTAINS content "%s"';
  196. if ($this->_negate) {
  197. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"';
  198. }
  199. $failure = sprintf($failure, $other, $this->_content);
  200. break;
  201. case self::ASSERT_CONTENT_REGEX:
  202. $failure = 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s"';
  203. if ($this->_negate) {
  204. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"';
  205. }
  206. $failure = sprintf($failure, $other, $this->_content);
  207. break;
  208. case self::ASSERT_CONTENT_COUNT:
  209. $failure = 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times';
  210. if ($this->_negate) {
  211. $failure = 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times';
  212. }
  213. $failure = sprintf($failure, $other, $this->_content);
  214. break;
  215. case self::ASSERT_CONTENT_COUNT_MIN:
  216. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times';
  217. $failure = sprintf($failure, $other, $this->_content);
  218. break;
  219. case self::ASSERT_CONTENT_COUNT_MAX:
  220. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times';
  221. $failure = sprintf($failure, $other, $this->_content);
  222. break;
  223. case self::ASSERT_QUERY:
  224. default:
  225. $failure = 'Failed asserting node DENOTED BY %s EXISTS';
  226. if ($this->_negate) {
  227. $failure = 'Failed asserting node DENOTED BY %s DOES NOT EXIST';
  228. }
  229. $failure = sprintf($failure, $other);
  230. break;
  231. }
  232. if (!empty($description)) {
  233. $failure = $description . "\n" . $failure;
  234. }
  235. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  236. }
  237. /**
  238. * Complete implementation
  239. *
  240. * @return string
  241. */
  242. public function toString()
  243. {
  244. return '';
  245. }
  246. /**
  247. * Check to see if content is matched in selected nodes
  248. *
  249. * @param Zend_Dom_Query_Result $result
  250. * @param string $match Content to match
  251. * @return bool
  252. */
  253. protected function _matchContent($result, $match)
  254. {
  255. if (0 == count($result)) {
  256. return false;
  257. }
  258. foreach ($result as $node) {
  259. $content = $this->_getNodeContent($node);
  260. if (strstr($content, $match)) {
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. /**
  267. * Check to see if content is NOT matched in selected nodes
  268. *
  269. * @param Zend_Dom_Query_Result $result
  270. * @param string $match
  271. * @return bool
  272. */
  273. protected function _notMatchContent($result, $match)
  274. {
  275. if (0 == count($result)) {
  276. return true;
  277. }
  278. foreach ($result as $node) {
  279. $content = $this->_getNodeContent($node);
  280. if (strstr($content, $match)) {
  281. return false;
  282. }
  283. }
  284. return true;
  285. }
  286. /**
  287. * Check to see if content is matched by regex in selected nodes
  288. *
  289. * @param Zend_Dom_Query_Result $result
  290. * @param string $pattern
  291. * @return bool
  292. */
  293. protected function _regexContent($result, $pattern)
  294. {
  295. if (0 == count($result)) {
  296. return false;
  297. }
  298. foreach ($result as $node) {
  299. $content = $this->_getNodeContent($node);
  300. if (preg_match($pattern, $content)) {
  301. return true;
  302. }
  303. }
  304. return false;
  305. }
  306. /**
  307. * Check to see if content is NOT matched by regex in selected nodes
  308. *
  309. * @param Zend_Dom_Query_Result $result
  310. * @param string $pattern
  311. * @return bool
  312. */
  313. protected function _notRegexContent($result, $pattern)
  314. {
  315. if (0 == count($result)) {
  316. return true;
  317. }
  318. foreach ($result as $node) {
  319. $content = $this->_getNodeContent($node);
  320. if (preg_match($pattern, $content)) {
  321. return false;
  322. }
  323. }
  324. return true;
  325. }
  326. /**
  327. * Determine if content count matches criteria
  328. *
  329. * @param Zend_Dom_Query_Result $result
  330. * @param int $test Value against which to test
  331. * @param string $type assertion type
  332. * @return boolean
  333. */
  334. protected function _countContent($result, $test, $type)
  335. {
  336. $count = count($result);
  337. switch ($type) {
  338. case self::ASSERT_CONTENT_COUNT:
  339. return ($this->_negate)
  340. ? ($test != $count)
  341. : ($test == $count);
  342. case self::ASSERT_CONTENT_COUNT_MIN:
  343. return ($count >= $test);
  344. case self::ASSERT_CONTENT_COUNT_MAX:
  345. return ($count <= $test);
  346. default:
  347. return false;
  348. }
  349. }
  350. /**
  351. * Get node content, minus node markup tags
  352. *
  353. * @param DOMNode $node
  354. * @return string
  355. */
  356. protected function _getNodeContent(DOMNode $node)
  357. {
  358. if ($node instanceof DOMAttr) {
  359. return $node->value;
  360. } else {
  361. $doc = $node->ownerDocument;
  362. $content = $doc->saveXML($node);
  363. $tag = $node->nodeName;
  364. $regex = '|</?' . $tag . '[^>]*>|';
  365. return preg_replace($regex, '', $content);
  366. }
  367. }
  368. }