File indexing completed on 2024-12-22 05:36:33

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_Controller
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Controller_Request_Http
0024  */
0025 // require_once 'Zend/Controller/Request/Http.php';
0026 
0027 /**
0028  * Zend_Controller_Request_HttpTestCase
0029  *
0030  * HTTP request object for use with Zend_Controller family.
0031  *
0032  * @uses Zend_Controller_Request_Http
0033  * @package Zend_Controller
0034  * @subpackage Request
0035  */
0036 class Zend_Controller_Request_HttpTestCase extends Zend_Controller_Request_Http
0037 {
0038     /**
0039      * Request headers
0040      * @var array
0041      */
0042     protected $_headers = array();
0043 
0044     /**
0045      * Request method
0046      * @var string
0047      */
0048     protected $_method = 'GET';
0049 
0050     /**
0051      * Raw POST body
0052      * @var string|null
0053      */
0054     protected $_rawBody;
0055 
0056     /**
0057      * Valid request method types
0058      * @var array
0059      */
0060     protected $_validMethodTypes = array(
0061         'DELETE',
0062         'GET',
0063         'HEAD',
0064         'OPTIONS',
0065         'PATCH',
0066         'POST',
0067         'PUT',
0068     );
0069 
0070     /**
0071      * Clear GET values
0072      *
0073      * @return Zend_Controller_Request_HttpTestCase
0074      */
0075     public function clearQuery()
0076     {
0077         $_GET = array();
0078         return $this;
0079     }
0080 
0081     /**
0082      * Clear POST values
0083      *
0084      * @return Zend_Controller_Request_HttpTestCase
0085      */
0086     public function clearPost()
0087     {
0088         $_POST = array();
0089         return $this;
0090     }
0091 
0092     /**
0093      * Set raw POST body
0094      *
0095      * @param  string $content
0096      * @return Zend_Controller_Request_HttpTestCase
0097      */
0098     public function setRawBody($content)
0099     {
0100         $this->_rawBody = (string) $content;
0101         return $this;
0102     }
0103 
0104     /**
0105      * Get RAW POST body
0106      *
0107      * @return string|null
0108      */
0109     public function getRawBody()
0110     {
0111         return $this->_rawBody;
0112     }
0113 
0114     /**
0115      * Clear raw POST body
0116      *
0117      * @return Zend_Controller_Request_HttpTestCase
0118      */
0119     public function clearRawBody()
0120     {
0121         $this->_rawBody = null;
0122         return $this;
0123     }
0124 
0125     /**
0126      * Set a cookie
0127      *
0128      * @param  string $key
0129      * @param  mixed $value
0130      * @return Zend_Controller_Request_HttpTestCase
0131      */
0132     public function setCookie($key, $value)
0133     {
0134         $_COOKIE[(string) $key] = $value;
0135         return $this;
0136     }
0137 
0138     /**
0139      * Set multiple cookies at once
0140      *
0141      * @param array $cookies
0142      * @return void
0143      */
0144     public function setCookies(array $cookies)
0145     {
0146         foreach ($cookies as $key => $value) {
0147             $_COOKIE[$key] = $value;
0148         }
0149         return $this;
0150     }
0151 
0152     /**
0153      * Clear all cookies
0154      *
0155      * @return Zend_Controller_Request_HttpTestCase
0156      */
0157     public function clearCookies()
0158     {
0159         $_COOKIE = array();
0160         return $this;
0161     }
0162 
0163     /**
0164      * Set request method
0165      *
0166      * @param  string $type
0167      * @return Zend_Controller_Request_HttpTestCase
0168      */
0169     public function setMethod($type)
0170     {
0171         $type = strtoupper(trim((string) $type));
0172         if (!in_array($type, $this->_validMethodTypes)) {
0173             // require_once 'Zend/Controller/Exception.php';
0174             throw new Zend_Controller_Exception('Invalid request method specified');
0175         }
0176         $this->_method = $type;
0177         return $this;
0178     }
0179 
0180     /**
0181      * Get request method
0182      *
0183      * @return string|null
0184      */
0185     public function getMethod()
0186     {
0187         return $this->_method;
0188     }
0189 
0190     /**
0191      * Set a request header
0192      *
0193      * @param  string $key
0194      * @param  string $value
0195      * @return Zend_Controller_Request_HttpTestCase
0196      */
0197     public function setHeader($key, $value)
0198     {
0199         $key = $this->_normalizeHeaderName($key);
0200         $this->_headers[$key] = (string) $value;
0201         return $this;
0202     }
0203 
0204     /**
0205      * Set request headers
0206      *
0207      * @param  array $headers
0208      * @return Zend_Controller_Request_HttpTestCase
0209      */
0210     public function setHeaders(array $headers)
0211     {
0212         foreach ($headers as $key => $value) {
0213             $this->setHeader($key, $value);
0214         }
0215         return $this;
0216     }
0217 
0218     /**
0219      * Get request header
0220      *
0221      * @param  string $header
0222      * @param  mixed $default
0223      * @return string|null
0224      */
0225     public function getHeader($header, $default = null)
0226     {
0227         $header = $this->_normalizeHeaderName($header);
0228         if (array_key_exists($header, $this->_headers)) {
0229             return $this->_headers[$header];
0230         }
0231         return $default;
0232     }
0233 
0234     /**
0235      * Get all request headers
0236      *
0237      * @return array
0238      */
0239     public function getHeaders()
0240     {
0241         return $this->_headers;
0242     }
0243 
0244     /**
0245      * Clear request headers
0246      *
0247      * @return Zend_Controller_Request_HttpTestCase
0248      */
0249     public function clearHeaders()
0250     {
0251         $this->_headers = array();
0252         return $this;
0253     }
0254 
0255     /**
0256      * Get REQUEST_URI
0257      *
0258      * @return null|string
0259      */
0260     public function getRequestUri()
0261     {
0262         return $this->_requestUri;
0263     }
0264 
0265     /**
0266      * Normalize a header name for setting and retrieval
0267      *
0268      * @param  string $name
0269      * @return string
0270      */
0271     protected function _normalizeHeaderName($name)
0272     {
0273         $name = strtoupper((string) $name);
0274         $name = str_replace('-', '_', $name);
0275         return $name;
0276     }
0277 }