File indexing completed on 2024-06-23 05:55:48

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_Redirect34 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      */
0103     public function evaluate($other, $assertType = null)
0104     {
0105         if (!$other instanceof Zend_Controller_Response_Abstract) {
0106             // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
0107             throw new Zend_Test_PHPUnit_Constraint_Exception('Redirect constraint assertions require a response object');
0108         }
0109 
0110         if (strstr($assertType, 'Not')) {
0111             $this->setNegate(true);
0112             $assertType = str_replace('Not', '', $assertType);
0113         }
0114 
0115         if (!in_array($assertType, $this->_assertTypes)) {
0116             // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
0117             throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
0118         }
0119 
0120         $this->_assertType = $assertType;
0121 
0122         $response = $other;
0123         $argv     = func_get_args();
0124         $argc     = func_num_args();
0125 
0126         switch ($assertType) {
0127             case self::ASSERT_REDIRECT_TO:
0128                 if (3 > $argc) {
0129                     // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
0130                     throw new Zend_Test_PHPUnit_Constraint_Exception('No redirect URL provided against which to match');
0131                 }
0132                 $this->_match = $match = $argv[2];
0133                 return ($this->_negate)
0134                     ? $this->_notMatch($response, $match)
0135                     : $this->_match($response, $match);
0136             case self::ASSERT_REDIRECT_REGEX:
0137                 if (3 > $argc) {
0138                     // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
0139                     throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match redirect');
0140                 }
0141                 $this->_match = $match = $argv[2];
0142                 return ($this->_negate)
0143                     ? $this->_notRegex($response, $match)
0144                     : $this->_regex($response, $match);
0145             case self::ASSERT_REDIRECT:
0146             default:
0147                 $headers  = $response->sendHeaders();
0148                 if (isset($headers['location'])) {
0149                     $redirect = $headers['location'];
0150                     $redirect = str_replace('Location: ', '', $redirect);
0151                     $this->_actual = $redirect;
0152                 }
0153                 return ($this->_negate) ? !$response->isRedirect() : $response->isRedirect();
0154         }
0155     }
0156 
0157     /**
0158      * Report Failure
0159      *
0160      * @see    PHPUnit_Framework_Constraint for implementation details
0161      * @param  mixed $other
0162      * @param  string $description Additional message to display
0163      * @param  bool $not
0164      * @return void
0165      * @throws PHPUnit_Framework_ExpectationFailedException
0166      */
0167     public function fail($other, $description, $not = false)
0168     {
0169         // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
0170         switch ($this->_assertType) {
0171             case self::ASSERT_REDIRECT_TO:
0172                 $failure = 'Failed asserting response redirects to "%s"';
0173                 if ($this->_negate) {
0174                     $failure = 'Failed asserting response DOES NOT redirect to "%s"';
0175                 }
0176                 $failure = sprintf($failure, $this->_match);
0177                 if (!$this->_negate && $this->_actual) {
0178                     $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
0179                 }
0180                 break;
0181             case self::ASSERT_REDIRECT_REGEX:
0182                 $failure = 'Failed asserting response redirects to URL MATCHING "%s"';
0183                 if ($this->_negate) {
0184                     $failure = 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"';
0185                 }
0186                 $failure = sprintf($failure, $this->_match);
0187                 if ($this->_actual) {
0188                     $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
0189                 }
0190                 break;
0191             case self::ASSERT_REDIRECT:
0192             default:
0193                 $failure = 'Failed asserting response is a redirect';
0194                 if ($this->_negate) {
0195                     $failure = 'Failed asserting response is NOT a redirect';
0196                     if ($this->_actual) {
0197                         $failure .= sprintf(PHP_EOL . 'It redirects to "%s"', $this->_actual);
0198                     }
0199                 }
0200                 break;
0201         }
0202 
0203         if (!empty($description)) {
0204             $failure = $description . "\n" . $failure;
0205         }
0206 
0207         throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
0208     }
0209 
0210     /**
0211      * Complete implementation
0212      *
0213      * @return string
0214      */
0215     public function toString()
0216     {
0217         return '';
0218     }
0219 
0220     /**
0221      * Check to see if content is matched in selected nodes
0222      *
0223      * @param  Zend_Controller_Response_HttpTestCase $response
0224      * @param  string $match Content to match
0225      * @return bool
0226      */
0227     protected function _match($response, $match)
0228     {
0229         if (!$response->isRedirect()) {
0230             return false;
0231         }
0232 
0233         $headers  = $response->sendHeaders();
0234         $redirect = $headers['location'];
0235         $redirect = str_replace('Location: ', '', $redirect);
0236         $this->_actual = $redirect;
0237 
0238         return ($redirect == $match);
0239     }
0240 
0241     /**
0242      * Check to see if content is NOT matched in selected nodes
0243      *
0244      * @param  Zend_Controller_Response_HttpTestCase $response
0245      * @param  string $match
0246      * @return bool
0247      */
0248     protected function _notMatch($response, $match)
0249     {
0250         if (!$response->isRedirect()) {
0251             return true;
0252         }
0253 
0254         $headers  = $response->sendHeaders();
0255         $redirect = $headers['location'];
0256         $redirect = str_replace('Location: ', '', $redirect);
0257         $this->_actual = $redirect;
0258 
0259         return ($redirect != $match);
0260     }
0261 
0262     /**
0263      * Check to see if content is matched by regex in selected nodes
0264      *
0265      * @param  Zend_Controller_Response_HttpTestCase $response
0266      * @param  string $pattern
0267      * @return bool
0268      */
0269     protected function _regex($response, $pattern)
0270     {
0271         if (!$response->isRedirect()) {
0272             return false;
0273         }
0274 
0275         $headers  = $response->sendHeaders();
0276         $redirect = $headers['location'];
0277         $redirect = str_replace('Location: ', '', $redirect);
0278         $this->_actual = $redirect;
0279 
0280         return preg_match($pattern, $redirect);
0281     }
0282 
0283     /**
0284      * Check to see if content is NOT matched by regex in selected nodes
0285      *
0286      * @param  Zend_Controller_Response_HttpTestCase $response
0287      * @param  string $pattern
0288      * @return bool
0289      */
0290     protected function _notRegex($response, $pattern)
0291     {
0292         if (!$response->isRedirect()) {
0293             return true;
0294         }
0295 
0296         $headers  = $response->sendHeaders();
0297         $redirect = $headers['location'];
0298         $redirect = str_replace('Location: ', '', $redirect);
0299         $this->_actual = $redirect;
0300 
0301         return !preg_match($pattern, $redirect);
0302     }
0303 }