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

https://bitbucket.org/DragonBe/zfform · PHP · 306 lines · 160 code · 31 blank · 115 comment · 22 complexity · f0e0cd78005aaa15493ac34e559d2e72 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-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. /**
  25. * Redirection constraints
  26. *
  27. * @uses PHPUnit_Framework_Constraint
  28. * @category Zend
  29. * @package Zend_Test
  30. * @subpackage PHPUnit
  31. * @copyright Copyright (c) 2005-2012 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_Redirect extends PHPUnit_Framework_Constraint
  35. {
  36. /**#@+
  37. * Assertion type constants
  38. */
  39. const ASSERT_REDIRECT = 'assertRedirect';
  40. const ASSERT_REDIRECT_TO = 'assertRedirectTo';
  41. const ASSERT_REDIRECT_REGEX = 'assertRedirectRegex';
  42. /**#@-*/
  43. /**
  44. * Current assertion type
  45. * @var string
  46. */
  47. protected $_assertType = null;
  48. /**
  49. * Available assertion types
  50. * @var array
  51. */
  52. protected $_assertTypes = array(
  53. self::ASSERT_REDIRECT,
  54. self::ASSERT_REDIRECT_TO,
  55. self::ASSERT_REDIRECT_REGEX,
  56. );
  57. /**
  58. * Pattern to match against
  59. * @var string
  60. */
  61. protected $_match = null;
  62. /**
  63. * What is actual redirect
  64. */
  65. protected $_actual = null;
  66. /**
  67. * Whether or not assertion is negated
  68. * @var bool
  69. */
  70. protected $_negate = false;
  71. /**
  72. * Constructor; setup constraint state
  73. *
  74. * @return void
  75. */
  76. public function __construct()
  77. {
  78. }
  79. /**
  80. * Indicate negative match
  81. *
  82. * @param bool $flag
  83. * @return void
  84. */
  85. public function setNegate($flag = true)
  86. {
  87. $this->_negate = $flag;
  88. }
  89. /**
  90. * Evaluate an object to see if it fits the constraints
  91. *
  92. * @param string $other String to examine
  93. * @param null|string Assertion type
  94. * @return bool
  95. */
  96. public function evaluate($other, $assertType = null)
  97. {
  98. if (!$other instanceof Zend_Controller_Response_Abstract) {
  99. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  100. throw new Zend_Test_PHPUnit_Constraint_Exception('Redirect constraint assertions require a response object');
  101. }
  102. if (strstr($assertType, 'Not')) {
  103. $this->setNegate(true);
  104. $assertType = str_replace('Not', '', $assertType);
  105. }
  106. if (!in_array($assertType, $this->_assertTypes)) {
  107. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  108. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  109. }
  110. $this->_assertType = $assertType;
  111. $response = $other;
  112. $argv = func_get_args();
  113. $argc = func_num_args();
  114. switch ($assertType) {
  115. case self::ASSERT_REDIRECT_TO:
  116. if (3 > $argc) {
  117. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  118. throw new Zend_Test_PHPUnit_Constraint_Exception('No redirect URL provided against which to match');
  119. }
  120. $this->_match = $match = $argv[2];
  121. return ($this->_negate)
  122. ? $this->_notMatch($response, $match)
  123. : $this->_match($response, $match);
  124. case self::ASSERT_REDIRECT_REGEX:
  125. if (3 > $argc) {
  126. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  127. throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match redirect');
  128. }
  129. $this->_match = $match = $argv[2];
  130. return ($this->_negate)
  131. ? $this->_notRegex($response, $match)
  132. : $this->_regex($response, $match);
  133. case self::ASSERT_REDIRECT:
  134. default:
  135. $headers = $response->sendHeaders();
  136. if (isset($headers['location'])) {
  137. $redirect = $headers['location'];
  138. $redirect = str_replace('Location: ', '', $redirect);
  139. $this->_actual = $redirect;
  140. }
  141. return ($this->_negate) ? !$response->isRedirect() : $response->isRedirect();
  142. }
  143. }
  144. /**
  145. * Report Failure
  146. *
  147. * @see PHPUnit_Framework_Constraint for implementation details
  148. * @param mixed $other
  149. * @param string $description Additional message to display
  150. * @param bool $not
  151. * @return void
  152. * @throws PHPUnit_Framework_ExpectationFailedException
  153. */
  154. public function fail($other, $description, $not = false)
  155. {
  156. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  157. switch ($this->_assertType) {
  158. case self::ASSERT_REDIRECT_TO:
  159. $failure = 'Failed asserting response redirects to "%s"';
  160. if ($this->_negate) {
  161. $failure = 'Failed asserting response DOES NOT redirect to "%s"';
  162. }
  163. $failure = sprintf($failure, $this->_match);
  164. if (!$this->_negate && $this->_actual) {
  165. $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
  166. }
  167. break;
  168. case self::ASSERT_REDIRECT_REGEX:
  169. $failure = 'Failed asserting response redirects to URL MATCHING "%s"';
  170. if ($this->_negate) {
  171. $failure = 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"';
  172. }
  173. $failure = sprintf($failure, $this->_match);
  174. if ($this->_actual) {
  175. $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
  176. }
  177. break;
  178. case self::ASSERT_REDIRECT:
  179. default:
  180. $failure = 'Failed asserting response is a redirect';
  181. if ($this->_negate) {
  182. $failure = 'Failed asserting response is NOT a redirect';
  183. if ($this->_actual) {
  184. $failure .= sprintf(PHP_EOL . 'It redirects to "%s"', $this->_actual);
  185. }
  186. }
  187. break;
  188. }
  189. if (!empty($description)) {
  190. $failure = $description . "\n" . $failure;
  191. }
  192. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  193. }
  194. /**
  195. * Complete implementation
  196. *
  197. * @return string
  198. */
  199. public function toString()
  200. {
  201. return '';
  202. }
  203. /**
  204. * Check to see if content is matched in selected nodes
  205. *
  206. * @param Zend_Controller_Response_HttpTestCase $response
  207. * @param string $match Content to match
  208. * @return bool
  209. */
  210. protected function _match($response, $match)
  211. {
  212. if (!$response->isRedirect()) {
  213. return false;
  214. }
  215. $headers = $response->sendHeaders();
  216. $redirect = $headers['location'];
  217. $redirect = str_replace('Location: ', '', $redirect);
  218. $this->_actual = $redirect;
  219. return ($redirect == $match);
  220. }
  221. /**
  222. * Check to see if content is NOT matched in selected nodes
  223. *
  224. * @param Zend_Controller_Response_HttpTestCase $response
  225. * @param string $match
  226. * @return bool
  227. */
  228. protected function _notMatch($response, $match)
  229. {
  230. if (!$response->isRedirect()) {
  231. return true;
  232. }
  233. $headers = $response->sendHeaders();
  234. $redirect = $headers['location'];
  235. $redirect = str_replace('Location: ', '', $redirect);
  236. $this->_actual = $redirect;
  237. return ($redirect != $match);
  238. }
  239. /**
  240. * Check to see if content is matched by regex in selected nodes
  241. *
  242. * @param Zend_Controller_Response_HttpTestCase $response
  243. * @param string $pattern
  244. * @return bool
  245. */
  246. protected function _regex($response, $pattern)
  247. {
  248. if (!$response->isRedirect()) {
  249. return false;
  250. }
  251. $headers = $response->sendHeaders();
  252. $redirect = $headers['location'];
  253. $redirect = str_replace('Location: ', '', $redirect);
  254. $this->_actual = $redirect;
  255. return preg_match($pattern, $redirect);
  256. }
  257. /**
  258. * Check to see if content is NOT matched by regex in selected nodes
  259. *
  260. * @param Zend_Controller_Response_HttpTestCase $response
  261. * @param string $pattern
  262. * @return bool
  263. */
  264. protected function _notRegex($response, $pattern)
  265. {
  266. if (!$response->isRedirect()) {
  267. return true;
  268. }
  269. $headers = $response->sendHeaders();
  270. $redirect = $headers['location'];
  271. $redirect = str_replace('Location: ', '', $redirect);
  272. $this->_actual = $redirect;
  273. return !preg_match($pattern, $redirect);
  274. }
  275. }