File indexing completed on 2025-01-26 05:29:58
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Test 0017 * @subpackage PHPUnit 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * Redirection constraints 0025 * 0026 * @uses PHPUnit_Framework_Constraint 0027 * @category Zend 0028 * @package Zend_Test 0029 * @subpackage PHPUnit 0030 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0031 * @license http://framework.zend.com/license/new-bsd New BSD License 0032 */ 0033 class Zend_Test_PHPUnit_Constraint_Redirect37 extends PHPUnit_Framework_Constraint 0034 { 0035 /**#@+ 0036 * Assertion type constants 0037 */ 0038 const ASSERT_REDIRECT = 'assertRedirect'; 0039 const ASSERT_REDIRECT_TO = 'assertRedirectTo'; 0040 const ASSERT_REDIRECT_REGEX = 'assertRedirectRegex'; 0041 /**#@-*/ 0042 0043 /** 0044 * Current assertion type 0045 * @var string 0046 */ 0047 protected $_assertType = null; 0048 0049 /** 0050 * Available assertion types 0051 * @var array 0052 */ 0053 protected $_assertTypes = array( 0054 self::ASSERT_REDIRECT, 0055 self::ASSERT_REDIRECT_TO, 0056 self::ASSERT_REDIRECT_REGEX, 0057 ); 0058 0059 /** 0060 * Pattern to match against 0061 * @var string 0062 */ 0063 protected $_match = null; 0064 0065 /** 0066 * What is actual redirect 0067 */ 0068 protected $_actual = null; 0069 0070 /** 0071 * Whether or not assertion is negated 0072 * @var bool 0073 */ 0074 protected $_negate = false; 0075 0076 /** 0077 * Constructor; setup constraint state 0078 * 0079 * @return void 0080 */ 0081 public function __construct() 0082 { 0083 } 0084 0085 /** 0086 * Indicate negative match 0087 * 0088 * @param bool $flag 0089 * @return void 0090 */ 0091 public function setNegate($flag = true) 0092 { 0093 $this->_negate = $flag; 0094 } 0095 0096 /** 0097 * Evaluate an object to see if it fits the constraints 0098 * 0099 * @param string $other String to examine 0100 * @param null|string Assertion type 0101 * @return bool 0102 * NOTE: 0103 * Drastic changes up to PHPUnit 3.5.15 this was: 0104 * public function evaluate($other, $assertType = null) 0105 * In PHPUnit 3.6.0 they changed the interface into this: 0106 * public function evaluate($other, $description = '', $returnResult = FALSE) 0107 * We use the new interface for PHP-strict checking, but emulate the old one 0108 */ 0109 public function evaluate($other, $assertType = null, $variable = FALSE) 0110 { 0111 if (!$other instanceof Zend_Controller_Response_Abstract) { 0112 // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; 0113 throw new Zend_Test_PHPUnit_Constraint_Exception('Redirect constraint assertions require a response object'); 0114 } 0115 0116 if (strstr($assertType, 'Not')) { 0117 $this->setNegate(true); 0118 $assertType = str_replace('Not', '', $assertType); 0119 } 0120 0121 if (!in_array($assertType, $this->_assertTypes)) { 0122 // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; 0123 throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__)); 0124 } 0125 0126 $this->_assertType = $assertType; 0127 0128 $response = $other; 0129 $argv = func_get_args(); 0130 $argc = func_num_args(); 0131 0132 switch ($assertType) { 0133 case self::ASSERT_REDIRECT_TO: 0134 if (3 > $argc) { 0135 // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; 0136 throw new Zend_Test_PHPUnit_Constraint_Exception('No redirect URL provided against which to match'); 0137 } 0138 $this->_match = $match = $argv[2]; 0139 return ($this->_negate) 0140 ? $this->_notMatch($response, $match) 0141 : $this->_match($response, $match); 0142 case self::ASSERT_REDIRECT_REGEX: 0143 if (3 > $argc) { 0144 // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; 0145 throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match redirect'); 0146 } 0147 $this->_match = $match = $argv[2]; 0148 return ($this->_negate) 0149 ? $this->_notRegex($response, $match) 0150 : $this->_regex($response, $match); 0151 case self::ASSERT_REDIRECT: 0152 default: 0153 $headers = $response->sendHeaders(); 0154 if (isset($headers['location'])) { 0155 $redirect = $headers['location']; 0156 $redirect = str_replace('Location: ', '', $redirect); 0157 $this->_actual = $redirect; 0158 } 0159 return ($this->_negate) ? !$response->isRedirect() : $response->isRedirect(); 0160 } 0161 } 0162 0163 /** 0164 * Report Failure 0165 * 0166 * @see PHPUnit_Framework_Constraint for implementation details 0167 * @param mixed $other 0168 * @param string $description Additional message to display 0169 * @param bool $not 0170 * @return void 0171 * @throws PHPUnit_Framework_ExpectationFailedException 0172 * NOTE: 0173 * Drastic changes up to PHPUnit 3.5.15 this was: 0174 * public function fail($other, $description, $not = false) 0175 * In PHPUnit 3.6.0 they changed the interface into this: 0176 * protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL) 0177 * We use the new interface for PHP-strict checking 0178 */ 0179 public function fail($other, $description, PHPUnit_Framework_ComparisonFailure $cannot_be_used = NULL) 0180 { 0181 // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; 0182 switch ($this->_assertType) { 0183 case self::ASSERT_REDIRECT_TO: 0184 $failure = 'Failed asserting response redirects to "%s"'; 0185 if ($this->_negate) { 0186 $failure = 'Failed asserting response DOES NOT redirect to "%s"'; 0187 } 0188 $failure = sprintf($failure, $this->_match); 0189 if (!$this->_negate && $this->_actual) { 0190 $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual); 0191 } 0192 break; 0193 case self::ASSERT_REDIRECT_REGEX: 0194 $failure = 'Failed asserting response redirects to URL MATCHING "%s"'; 0195 if ($this->_negate) { 0196 $failure = 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"'; 0197 } 0198 $failure = sprintf($failure, $this->_match); 0199 if ($this->_actual) { 0200 $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual); 0201 } 0202 break; 0203 case self::ASSERT_REDIRECT: 0204 default: 0205 $failure = 'Failed asserting response is a redirect'; 0206 if ($this->_negate) { 0207 $failure = 'Failed asserting response is NOT a redirect'; 0208 if ($this->_actual) { 0209 $failure .= sprintf(PHP_EOL . 'It redirects to "%s"', $this->_actual); 0210 } 0211 } 0212 break; 0213 } 0214 0215 if (!empty($description)) { 0216 $failure = $description . "\n" . $failure; 0217 } 0218 0219 throw new Zend_Test_PHPUnit_Constraint_Exception($failure); 0220 } 0221 0222 /** 0223 * Complete implementation 0224 * 0225 * @return string 0226 */ 0227 public function toString() 0228 { 0229 return ''; 0230 } 0231 0232 /** 0233 * Check to see if content is matched in selected nodes 0234 * 0235 * @param Zend_Controller_Response_HttpTestCase $response 0236 * @param string $match Content to match 0237 * @return bool 0238 */ 0239 protected function _match($response, $match) 0240 { 0241 if (!$response->isRedirect()) { 0242 return false; 0243 } 0244 0245 $headers = $response->sendHeaders(); 0246 $redirect = $headers['location']; 0247 $redirect = str_replace('Location: ', '', $redirect); 0248 $this->_actual = $redirect; 0249 0250 return ($redirect == $match); 0251 } 0252 0253 /** 0254 * Check to see if content is NOT matched in selected nodes 0255 * 0256 * @param Zend_Controller_Response_HttpTestCase $response 0257 * @param string $match 0258 * @return bool 0259 */ 0260 protected function _notMatch($response, $match) 0261 { 0262 if (!$response->isRedirect()) { 0263 return true; 0264 } 0265 0266 $headers = $response->sendHeaders(); 0267 $redirect = $headers['location']; 0268 $redirect = str_replace('Location: ', '', $redirect); 0269 $this->_actual = $redirect; 0270 0271 return ($redirect != $match); 0272 } 0273 0274 /** 0275 * Check to see if content is matched by regex in selected nodes 0276 * 0277 * @param Zend_Controller_Response_HttpTestCase $response 0278 * @param string $pattern 0279 * @return bool 0280 */ 0281 protected function _regex($response, $pattern) 0282 { 0283 if (!$response->isRedirect()) { 0284 return false; 0285 } 0286 0287 $headers = $response->sendHeaders(); 0288 $redirect = $headers['location']; 0289 $redirect = str_replace('Location: ', '', $redirect); 0290 $this->_actual = $redirect; 0291 0292 return preg_match($pattern, $redirect); 0293 } 0294 0295 /** 0296 * Check to see if content is NOT matched by regex in selected nodes 0297 * 0298 * @param Zend_Controller_Response_HttpTestCase $response 0299 * @param string $pattern 0300 * @return bool 0301 */ 0302 protected function _notRegex($response, $pattern) 0303 { 0304 if (!$response->isRedirect()) { 0305 return true; 0306 } 0307 0308 $headers = $response->sendHeaders(); 0309 $redirect = $headers['location']; 0310 $redirect = str_replace('Location: ', '', $redirect); 0311 $this->_actual = $redirect; 0312 0313 return !preg_match($pattern, $redirect); 0314 } 0315 }