web/lib/Zend/Test/PHPUnit/Constraint/ResponseHeader.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Test
    16  * @package    Zend_Test
    17  * @subpackage PHPUnit
    17  * @subpackage PHPUnit
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: ResponseHeader.php 25205 2013-01-10 11:23:25Z frosch $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /** @see PHPUnit_Framework_Constraint */
    23 if (version_compare(PHPUnit_Runner_Version::id(), '4.1', '>=')) {
    24 require_once 'PHPUnit/Framework/Constraint.php';
    24     include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ResponseHeader41.php');
    25 
    25 
    26 /**
    26     class Zend_Test_PHPUnit_Constraint_ResponseHeader extends Zend_Test_PHPUnit_Constraint_ResponseHeader41
    27  * Response header PHPUnit Constraint
    27     {}
    28  *
    28 } elseif (version_compare(PHPUnit_Runner_Version::id(), '3.5', '>=')) {
    29  * @uses       PHPUnit_Framework_Constraint
    29     include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ResponseHeader37.php');
    30  * @category   Zend
       
    31  * @package    Zend_Test
       
    32  * @subpackage PHPUnit
       
    33  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    35  */
       
    36 class Zend_Test_PHPUnit_Constraint_ResponseHeader extends PHPUnit_Framework_Constraint
       
    37 {
       
    38     /**#@+
       
    39      * Assertion type constants
       
    40      */
       
    41     const ASSERT_RESPONSE_CODE   = 'assertResponseCode';
       
    42     const ASSERT_HEADER          = 'assertHeader';
       
    43     const ASSERT_HEADER_CONTAINS = 'assertHeaderContains';
       
    44     const ASSERT_HEADER_REGEX    = 'assertHeaderRegex';
       
    45     /**#@-*/
       
    46 
    30 
    47     /**
    31     class Zend_Test_PHPUnit_Constraint_ResponseHeader extends Zend_Test_PHPUnit_Constraint_ResponseHeader37
    48      * Current assertion type
    32     {}
    49      * @var string
    33 } else {
    50      */
    34     include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ResponseHeader34.php');
    51     protected $_assertType      = null;
       
    52 
    35 
    53     /**
    36     class Zend_Test_PHPUnit_Constraint_ResponseHeader extends Zend_Test_PHPUnit_Constraint_ResponseHeader34
    54      * Available assertion types
    37     {}
    55      * @var array
       
    56      */
       
    57     protected $_assertTypes     = array(
       
    58         self::ASSERT_RESPONSE_CODE,
       
    59         self::ASSERT_HEADER,
       
    60         self::ASSERT_HEADER_CONTAINS,
       
    61         self::ASSERT_HEADER_REGEX,
       
    62     );
       
    63 
       
    64     /**
       
    65      * @var int Response code
       
    66      */
       
    67     protected $_code              = 200;
       
    68     
       
    69     /**
       
    70      * @var int Actual response code
       
    71      */
       
    72     protected $_actualCode        = null;
       
    73 
       
    74     /**
       
    75      * @var string Header
       
    76      */
       
    77     protected $_header            = null;
       
    78 
       
    79     /**
       
    80      * @var string pattern against which to compare header content
       
    81      */
       
    82     protected $_match             = null;
       
    83 
       
    84     /**
       
    85      * Whether or not assertion is negated
       
    86      * @var bool
       
    87      */
       
    88     protected $_negate            = false;
       
    89 
       
    90     /**
       
    91      * Constructor; setup constraint state
       
    92      *
       
    93      * @return void
       
    94      */
       
    95     public function __construct()
       
    96     {
       
    97     }
       
    98 
       
    99     /**
       
   100      * Indicate negative match
       
   101      *
       
   102      * @param  bool $flag
       
   103      * @return void
       
   104      */
       
   105     public function setNegate($flag = true)
       
   106     {
       
   107         $this->_negate = $flag;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Evaluate an object to see if it fits the constraints
       
   112      *
       
   113      * @param  Zend_Controller_Response_Abstract $other String to examine
       
   114      * @param  null|string Assertion type
       
   115      * @return bool
       
   116      */
       
   117     public function evaluate($other, $assertType = null)
       
   118     {
       
   119         if (!$other instanceof Zend_Controller_Response_Abstract) {
       
   120             require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   121             throw new Zend_Test_PHPUnit_Constraint_Exception('Header constraint assertions require a response object');
       
   122         }
       
   123 
       
   124         if (strstr($assertType, 'Not')) {
       
   125             $this->setNegate(true);
       
   126             $assertType = str_replace('Not', '', $assertType);
       
   127         }
       
   128 
       
   129         if (!in_array($assertType, $this->_assertTypes)) {
       
   130             require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   131             throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
       
   132         }
       
   133 
       
   134         $this->_assertType = $assertType;
       
   135 
       
   136         $response = $other;
       
   137         $argv     = func_get_args();
       
   138         $argc     = func_num_args();
       
   139 
       
   140         switch ($assertType) {
       
   141             case self::ASSERT_RESPONSE_CODE:
       
   142                 if (3 > $argc) {
       
   143                     require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   144                     throw new Zend_Test_PHPUnit_Constraint_Exception('No response code provided against which to match');
       
   145                 }
       
   146                 $this->_code = $code = $argv[2];
       
   147                 return ($this->_negate)
       
   148                     ? $this->_notCode($response, $code)
       
   149                     : $this->_code($response, $code);
       
   150             case self::ASSERT_HEADER:
       
   151                 if (3 > $argc) {
       
   152                     require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   153                     throw new Zend_Test_PHPUnit_Constraint_Exception('No header provided against which to match');
       
   154                 }
       
   155                 $this->_header = $header = $argv[2];
       
   156                 return ($this->_negate)
       
   157                     ? $this->_notHeader($response, $header)
       
   158                     : $this->_header($response, $header);
       
   159             case self::ASSERT_HEADER_CONTAINS:
       
   160                 if (4 > $argc) {
       
   161                     require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   162                     throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__);
       
   163                 }
       
   164                 $this->_header = $header = $argv[2];
       
   165                 $this->_match  = $match  = $argv[3];
       
   166                 return ($this->_negate)
       
   167                     ? $this->_notHeaderContains($response, $header, $match)
       
   168                     : $this->_headerContains($response, $header, $match);
       
   169             case self::ASSERT_HEADER_REGEX:
       
   170                 if (4 > $argc) {
       
   171                     require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   172                     throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__);
       
   173                 }
       
   174                 $this->_header = $header = $argv[2];
       
   175                 $this->_match  = $match  = $argv[3];
       
   176                 return ($this->_negate)
       
   177                     ? $this->_notHeaderRegex($response, $header, $match)
       
   178                     : $this->_headerRegex($response, $header, $match);
       
   179             default:
       
   180                 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   181                 throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
       
   182         }
       
   183     }
       
   184 
       
   185     /**
       
   186      * Report Failure
       
   187      *
       
   188      * @see    PHPUnit_Framework_Constraint for implementation details
       
   189      * @param  mixed $other
       
   190      * @param  string $description Additional message to display
       
   191      * @param  bool $not
       
   192      * @return void
       
   193      * @throws PHPUnit_Framework_ExpectationFailedException
       
   194      */
       
   195     public function fail($other, $description, $not = false)
       
   196     {
       
   197         require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
       
   198         switch ($this->_assertType) {
       
   199             case self::ASSERT_RESPONSE_CODE:
       
   200                 $failure = 'Failed asserting response code "%s"';
       
   201                 if ($this->_negate) {
       
   202                     $failure = 'Failed asserting response code IS NOT "%s"';
       
   203                 }
       
   204                 $failure = sprintf($failure, $this->_code);
       
   205                 if (!$this->_negate && $this->_actualCode) {
       
   206                     $failure .= sprintf(PHP_EOL . 'Was "%s"', $this->_actualCode);
       
   207                 }
       
   208                 break;
       
   209             case self::ASSERT_HEADER:
       
   210                 $failure = 'Failed asserting response header "%s" found';
       
   211                 if ($this->_negate) {
       
   212                     $failure = 'Failed asserting response response header "%s" WAS NOT found';
       
   213                 }
       
   214                 $failure = sprintf($failure, $this->_header);
       
   215                 break;
       
   216             case self::ASSERT_HEADER_CONTAINS:
       
   217                 $failure = 'Failed asserting response header "%s" exists and contains "%s"';
       
   218                 if ($this->_negate) {
       
   219                     $failure = 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"';
       
   220                 }
       
   221                 $failure = sprintf($failure, $this->_header, $this->_match);
       
   222                 break;
       
   223             case self::ASSERT_HEADER_REGEX:
       
   224                 $failure = 'Failed asserting response header "%s" exists and matches regex "%s"';
       
   225                 if ($this->_negate) {
       
   226                     $failure = 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"';
       
   227                 }
       
   228                 $failure = sprintf($failure, $this->_header, $this->_match);
       
   229                 break;
       
   230             default:
       
   231                 throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
       
   232         }
       
   233 
       
   234         if (!empty($description)) {
       
   235             $failure = $description . "\n" . $failure;
       
   236         }
       
   237 
       
   238         throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
       
   239     }
       
   240 
       
   241     /**
       
   242      * Complete implementation
       
   243      *
       
   244      * @return string
       
   245      */
       
   246     public function toString()
       
   247     {
       
   248         return '';
       
   249     }
       
   250 
       
   251     /**
       
   252      * Compare response code for positive match
       
   253      *
       
   254      * @param  Zend_Controller_Response_Abstract $response
       
   255      * @param  int $code
       
   256      * @return bool
       
   257      */
       
   258     protected function _code(Zend_Controller_Response_Abstract $response, $code)
       
   259     {
       
   260         $test = $this->_getCode($response);
       
   261         $this->_actualCode = $test;
       
   262         return ($test == $code);
       
   263     }
       
   264 
       
   265     /**
       
   266      * Compare response code for negative match
       
   267      *
       
   268      * @param  Zend_Controller_Response_Abstract $response
       
   269      * @param  int $code
       
   270      * @return bool
       
   271      */
       
   272     protected function _notCode(Zend_Controller_Response_Abstract $response, $code)
       
   273     {
       
   274         $test = $this->_getCode($response);
       
   275         return ($test != $code);
       
   276     }
       
   277 
       
   278     /**
       
   279      * Retrieve response code
       
   280      *
       
   281      * @param  Zend_Controller_Response_Abstract $response
       
   282      * @return int
       
   283      */
       
   284     protected function _getCode(Zend_Controller_Response_Abstract $response)
       
   285     {
       
   286         $test = $response->getHttpResponseCode();
       
   287         if (null === $test) {
       
   288             $test = 200;
       
   289         }
       
   290         return $test;
       
   291     }
       
   292 
       
   293     /**
       
   294      * Positive check for response header presence
       
   295      *
       
   296      * @param  Zend_Controller_Response_Abstract $response
       
   297      * @param  string $header
       
   298      * @return bool
       
   299      */
       
   300     protected function _header(Zend_Controller_Response_Abstract $response, $header)
       
   301     {
       
   302         return (null !== $this->_getHeader($response, $header));
       
   303     }
       
   304 
       
   305     /**
       
   306      * Negative check for response header presence
       
   307      *
       
   308      * @param  Zend_Controller_Response_Abstract $response
       
   309      * @param  string $header
       
   310      * @return bool
       
   311      */
       
   312     protected function _notHeader(Zend_Controller_Response_Abstract $response, $header)
       
   313     {
       
   314         return (null === $this->_getHeader($response, $header));
       
   315     }
       
   316 
       
   317     /**
       
   318      * Retrieve response header
       
   319      *
       
   320      * @param  Zend_Controller_Response_Abstract $response
       
   321      * @param  string $header
       
   322      * @return string|null
       
   323      */
       
   324     protected function _getHeader(Zend_Controller_Response_Abstract $response, $header)
       
   325     {
       
   326         $headers = $response->sendHeaders();
       
   327         $header  = strtolower($header);
       
   328         if (array_key_exists($header, $headers)) {
       
   329             return $headers[$header];
       
   330         }
       
   331         return null;
       
   332     }
       
   333 
       
   334     /**
       
   335      * Positive check for header contents matching pattern
       
   336      *
       
   337      * @param  Zend_Controller_Response_Abstract $response
       
   338      * @param  string $header
       
   339      * @param  string $match
       
   340      * @return bool
       
   341      */
       
   342     protected function _headerContains(Zend_Controller_Response_Abstract $response, $header, $match)
       
   343     {
       
   344         if (null === ($fullHeader = $this->_getHeader($response, $header))) {
       
   345             return false;
       
   346         }
       
   347 
       
   348         $contents = str_replace($header . ': ', '', $fullHeader);
       
   349 
       
   350         return (strstr($contents, $match) !== false);
       
   351     }
       
   352 
       
   353     /**
       
   354      * Negative check for header contents matching pattern
       
   355      *
       
   356      * @param  Zend_Controller_Response_Abstract $response
       
   357      * @param  string $header
       
   358      * @param  string $match
       
   359      * @return bool
       
   360      */
       
   361     protected function _notHeaderContains(Zend_Controller_Response_Abstract $response, $header, $match)
       
   362     {
       
   363         if (null === ($fullHeader = $this->_getHeader($response, $header))) {
       
   364             return true;
       
   365         }
       
   366 
       
   367         $contents = str_replace($header . ': ', '', $fullHeader);
       
   368 
       
   369         return (strstr($contents, $match) === false);
       
   370     }
       
   371 
       
   372     /**
       
   373      * Positive check for header contents matching regex
       
   374      *
       
   375      * @param  Zend_Controller_Response_Abstract $response
       
   376      * @param  string $header
       
   377      * @param  string $pattern
       
   378      * @return bool
       
   379      */
       
   380     protected function _headerRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
       
   381     {
       
   382         if (null === ($fullHeader = $this->_getHeader($response, $header))) {
       
   383             return false;
       
   384         }
       
   385 
       
   386         $contents = str_replace($header . ': ', '', $fullHeader);
       
   387 
       
   388         return preg_match($pattern, $contents);
       
   389     }
       
   390 
       
   391     /**
       
   392      * Negative check for header contents matching regex
       
   393      *
       
   394      * @param  Zend_Controller_Response_Abstract $response
       
   395      * @param  string $header
       
   396      * @param  string $pattern
       
   397      * @return bool
       
   398      */
       
   399     protected function _notHeaderRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
       
   400     {
       
   401         if (null === ($fullHeader = $this->_getHeader($response, $header))) {
       
   402             return true;
       
   403         }
       
   404 
       
   405         $contents = str_replace($header . ': ', '', $fullHeader);
       
   406 
       
   407         return !preg_match($pattern, $contents);
       
   408     }
       
   409 }
    38 }