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

https://bitbucket.org/hamidrezas/melobit · PHP · 282 lines · 140 code · 30 blank · 112 comment · 17 complexity · 27aefd6d7075cd9e49fa60ecc4ccbc5f 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: Redirect.php 24594 2012-01-05 21:27:01Z matthew $
  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. * Whether or not assertion is negated
  64. * @var bool
  65. */
  66. protected $_negate = false;
  67. /**
  68. * Constructor; setup constraint state
  69. *
  70. * @return void
  71. */
  72. public function __construct()
  73. {
  74. }
  75. /**
  76. * Indicate negative match
  77. *
  78. * @param bool $flag
  79. * @return void
  80. */
  81. public function setNegate($flag = true)
  82. {
  83. $this->_negate = $flag;
  84. }
  85. /**
  86. * Evaluate an object to see if it fits the constraints
  87. *
  88. * @param string $other String to examine
  89. * @param null|string Assertion type
  90. * @return bool
  91. */
  92. public function evaluate($other, $assertType = null)
  93. {
  94. if (!$other instanceof Zend_Controller_Response_Abstract) {
  95. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  96. throw new Zend_Test_PHPUnit_Constraint_Exception('Redirect constraint assertions require a response object');
  97. }
  98. if (strstr($assertType, 'Not')) {
  99. $this->setNegate(true);
  100. $assertType = str_replace('Not', '', $assertType);
  101. }
  102. if (!in_array($assertType, $this->_assertTypes)) {
  103. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  104. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  105. }
  106. $this->_assertType = $assertType;
  107. $response = $other;
  108. $argv = func_get_args();
  109. $argc = func_num_args();
  110. switch ($assertType) {
  111. case self::ASSERT_REDIRECT_TO:
  112. if (3 > $argc) {
  113. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  114. throw new Zend_Test_PHPUnit_Constraint_Exception('No redirect URL provided against which to match');
  115. }
  116. $this->_match = $match = $argv[2];
  117. return ($this->_negate)
  118. ? $this->_notMatch($response, $match)
  119. : $this->_match($response, $match);
  120. case self::ASSERT_REDIRECT_REGEX:
  121. if (3 > $argc) {
  122. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  123. throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match redirect');
  124. }
  125. $this->_match = $match = $argv[2];
  126. return ($this->_negate)
  127. ? $this->_notRegex($response, $match)
  128. : $this->_regex($response, $match);
  129. case self::ASSERT_REDIRECT:
  130. default:
  131. return ($this->_negate) ? !$response->isRedirect() : $response->isRedirect();
  132. }
  133. }
  134. /**
  135. * Report Failure
  136. *
  137. * @see PHPUnit_Framework_Constraint for implementation details
  138. * @param mixed $other
  139. * @param string $description Additional message to display
  140. * @param bool $not
  141. * @return void
  142. * @throws PHPUnit_Framework_ExpectationFailedException
  143. */
  144. public function fail($other, $description, $not = false)
  145. {
  146. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  147. switch ($this->_assertType) {
  148. case self::ASSERT_REDIRECT_TO:
  149. $failure = 'Failed asserting response redirects to "%s"';
  150. if ($this->_negate) {
  151. $failure = 'Failed asserting response DOES NOT redirect to "%s"';
  152. }
  153. $failure = sprintf($failure, $this->_match);
  154. break;
  155. case self::ASSERT_REDIRECT_REGEX:
  156. $failure = 'Failed asserting response redirects to URL MATCHING "%s"';
  157. if ($this->_negate) {
  158. $failure = 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"';
  159. }
  160. $failure = sprintf($failure, $this->_match);
  161. break;
  162. case self::ASSERT_REDIRECT:
  163. default:
  164. $failure = 'Failed asserting response is a redirect';
  165. if ($this->_negate) {
  166. $failure = 'Failed asserting response is NOT a redirect';
  167. }
  168. break;
  169. }
  170. if (!empty($description)) {
  171. $failure = $description . "\n" . $failure;
  172. }
  173. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  174. }
  175. /**
  176. * Complete implementation
  177. *
  178. * @return string
  179. */
  180. public function toString()
  181. {
  182. return '';
  183. }
  184. /**
  185. * Check to see if content is matched in selected nodes
  186. *
  187. * @param Zend_Controller_Response_HttpTestCase $response
  188. * @param string $match Content to match
  189. * @return bool
  190. */
  191. protected function _match($response, $match)
  192. {
  193. if (!$response->isRedirect()) {
  194. return false;
  195. }
  196. $headers = $response->sendHeaders();
  197. $redirect = $headers['location'];
  198. $redirect = str_replace('Location: ', '', $redirect);
  199. return ($redirect == $match);
  200. }
  201. /**
  202. * Check to see if content is NOT matched in selected nodes
  203. *
  204. * @param Zend_Controller_Response_HttpTestCase $response
  205. * @param string $match
  206. * @return bool
  207. */
  208. protected function _notMatch($response, $match)
  209. {
  210. if (!$response->isRedirect()) {
  211. return true;
  212. }
  213. $headers = $response->sendHeaders();
  214. $redirect = $headers['location'];
  215. $redirect = str_replace('Location: ', '', $redirect);
  216. return ($redirect != $match);
  217. }
  218. /**
  219. * Check to see if content is matched by regex in selected nodes
  220. *
  221. * @param Zend_Controller_Response_HttpTestCase $response
  222. * @param string $pattern
  223. * @return bool
  224. */
  225. protected function _regex($response, $pattern)
  226. {
  227. if (!$response->isRedirect()) {
  228. return false;
  229. }
  230. $headers = $response->sendHeaders();
  231. $redirect = $headers['location'];
  232. $redirect = str_replace('Location: ', '', $redirect);
  233. return preg_match($pattern, $redirect);
  234. }
  235. /**
  236. * Check to see if content is NOT matched by regex in selected nodes
  237. *
  238. * @param Zend_Controller_Response_HttpTestCase $response
  239. * @param string $pattern
  240. * @return bool
  241. */
  242. protected function _notRegex($response, $pattern)
  243. {
  244. if (!$response->isRedirect()) {
  245. return true;
  246. }
  247. $headers = $response->sendHeaders();
  248. $redirect = $headers['location'];
  249. $redirect = str_replace('Location: ', '', $redirect);
  250. return !preg_match($pattern, $redirect);
  251. }
  252. }