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_Redirect41 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      * NOTE 2:
0179      * Interface changed again in PHPUnit 4.1.0 because of refactoring to SebastianBergmann\Comparator
0180      */
0181     public function fail($other, $description, \SebastianBergmann\Comparator\ComparisonFailure $cannot_be_used = NULL)
0182     {
0183         // require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
0184         switch ($this->_assertType) {
0185             case self::ASSERT_REDIRECT_TO:
0186                 $failure = 'Failed asserting response redirects to "%s"';
0187                 if ($this->_negate) {
0188                     $failure = 'Failed asserting response DOES NOT redirect to "%s"';
0189                 }
0190                 $failure = sprintf($failure, $this->_match);
0191                 if (!$this->_negate && $this->_actual) {
0192                     $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
0193                 }
0194                 break;
0195             case self::ASSERT_REDIRECT_REGEX:
0196                 $failure = 'Failed asserting response redirects to URL MATCHING "%s"';
0197                 if ($this->_negate) {
0198                     $failure = 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"';
0199                 }
0200                 $failure = sprintf($failure, $this->_match);
0201                 if ($this->_actual) {
0202                     $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
0203                 }
0204                 break;
0205             case self::ASSERT_REDIRECT:
0206             default:
0207                 $failure = 'Failed asserting response is a redirect';
0208                 if ($this->_negate) {
0209                     $failure = 'Failed asserting response is NOT a redirect';
0210                     if ($this->_actual) {
0211                         $failure .= sprintf(PHP_EOL . 'It redirects to "%s"', $this->_actual);
0212                     }
0213                 }
0214                 break;
0215         }
0216 
0217         if (!empty($description)) {
0218             $failure = $description . "\n" . $failure;
0219         }
0220 
0221         throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
0222     }
0223 
0224     /**
0225      * Complete implementation
0226      *
0227      * @return string
0228      */
0229     public function toString()
0230     {
0231         return '';
0232     }
0233 
0234     /**
0235      * Check to see if content is matched in selected nodes
0236      *
0237      * @param  Zend_Controller_Response_HttpTestCase $response
0238      * @param  string $match Content to match
0239      * @return bool
0240      */
0241     protected function _match($response, $match)
0242     {
0243         if (!$response->isRedirect()) {
0244             return false;
0245         }
0246 
0247         $headers  = $response->sendHeaders();
0248         $redirect = $headers['location'];
0249         $redirect = str_replace('Location: ', '', $redirect);
0250         $this->_actual = $redirect;
0251 
0252         return ($redirect == $match);
0253     }
0254 
0255     /**
0256      * Check to see if content is NOT matched in selected nodes
0257      *
0258      * @param  Zend_Controller_Response_HttpTestCase $response
0259      * @param  string $match
0260      * @return bool
0261      */
0262     protected function _notMatch($response, $match)
0263     {
0264         if (!$response->isRedirect()) {
0265             return true;
0266         }
0267 
0268         $headers  = $response->sendHeaders();
0269         $redirect = $headers['location'];
0270         $redirect = str_replace('Location: ', '', $redirect);
0271         $this->_actual = $redirect;
0272 
0273         return ($redirect != $match);
0274     }
0275 
0276     /**
0277      * Check to see if content is matched by regex in selected nodes
0278      *
0279      * @param  Zend_Controller_Response_HttpTestCase $response
0280      * @param  string $pattern
0281      * @return bool
0282      */
0283     protected function _regex($response, $pattern)
0284     {
0285         if (!$response->isRedirect()) {
0286             return false;
0287         }
0288 
0289         $headers  = $response->sendHeaders();
0290         $redirect = $headers['location'];
0291         $redirect = str_replace('Location: ', '', $redirect);
0292         $this->_actual = $redirect;
0293 
0294         return preg_match($pattern, $redirect);
0295     }
0296 
0297     /**
0298      * Check to see if content is NOT matched by regex in selected nodes
0299      *
0300      * @param  Zend_Controller_Response_HttpTestCase $response
0301      * @param  string $pattern
0302      * @return bool
0303      */
0304     protected function _notRegex($response, $pattern)
0305     {
0306         if (!$response->isRedirect()) {
0307             return true;
0308         }
0309 
0310         $headers  = $response->sendHeaders();
0311         $redirect = $headers['location'];
0312         $redirect = str_replace('Location: ', '', $redirect);
0313         $this->_actual = $redirect;
0314 
0315         return !preg_match($pattern, $redirect);
0316     }
0317 }