/Zend/Controller/Request/HttpTestCase.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 281 lines · 112 code · 23 blank · 146 comment · 2 complexity · 762b9bff420134794b220fc843ef34df MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: HttpTestCase.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Controller\Request;
  25. /**
  26. * @see Zend_Controller_Request_Http
  27. */
  28. require_once 'Zend/Controller/Request/Http.php';
  29. /**
  30. * Zend_Controller_Request_HttpTestCase
  31. *
  32. * HTTP request object for use with Zend_Controller family.
  33. *
  34. * @uses Zend_Controller_Request_Http
  35. * @package Zend_Controller
  36. * @subpackage Request
  37. */
  38. class HttpTestCase extends Http
  39. {
  40. /**
  41. * Request headers
  42. * @var array
  43. */
  44. protected $_headers = array();
  45. /**
  46. * Request method
  47. * @var string
  48. */
  49. protected $_method = 'GET';
  50. /**
  51. * Raw POST body
  52. * @var string|null
  53. */
  54. protected $_rawBody;
  55. /**
  56. * Valid request method types
  57. * @var array
  58. */
  59. protected $_validMethodTypes = array(
  60. 'DELETE',
  61. 'GET',
  62. 'HEAD',
  63. 'OPTIONS',
  64. 'POST',
  65. 'PUT',
  66. );
  67. /**
  68. * Clear GET values
  69. *
  70. * @return Zend_Controller_Request_HttpTestCase
  71. */
  72. public function clearQuery()
  73. {
  74. $_GET = array();
  75. return $this;
  76. }
  77. /**
  78. * Clear POST values
  79. *
  80. * @return Zend_Controller_Request_HttpTestCase
  81. */
  82. public function clearPost()
  83. {
  84. $_POST = array();
  85. return $this;
  86. }
  87. /**
  88. * Set raw POST body
  89. *
  90. * @param string $content
  91. * @return Zend_Controller_Request_HttpTestCase
  92. */
  93. public function setRawBody($content)
  94. {
  95. $this->_rawBody = (string) $content;
  96. return $this;
  97. }
  98. /**
  99. * Get RAW POST body
  100. *
  101. * @return string|null
  102. */
  103. public function getRawBody()
  104. {
  105. return $this->_rawBody;
  106. }
  107. /**
  108. * Clear raw POST body
  109. *
  110. * @return Zend_Controller_Request_HttpTestCase
  111. */
  112. public function clearRawBody()
  113. {
  114. $this->_rawBody = null;
  115. return $this;
  116. }
  117. /**
  118. * Set a cookie
  119. *
  120. * @param string $key
  121. * @param mixed $value
  122. * @return Zend_Controller_Request_HttpTestCase
  123. */
  124. public function setCookie($key, $value)
  125. {
  126. $_COOKIE[(string) $key] = $value;
  127. return $this;
  128. }
  129. /**
  130. * Set multiple cookies at once
  131. *
  132. * @param array $cookies
  133. * @return void
  134. */
  135. public function setCookies(array $cookies)
  136. {
  137. foreach ($cookies as $key => $value) {
  138. $_COOKIE[$key] = $value;
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Clear all cookies
  144. *
  145. * @return Zend_Controller_Request_HttpTestCase
  146. */
  147. public function clearCookies()
  148. {
  149. $_COOKIE = array();
  150. return $this;
  151. }
  152. /**
  153. * Set request method
  154. *
  155. * @param string $type
  156. * @return Zend_Controller_Request_HttpTestCase
  157. */
  158. public function setMethod($type)
  159. {
  160. $type = strtoupper(trim((string) $type));
  161. if (!in_array($type, $this->_validMethodTypes)) {
  162. require_once 'Zend/Controller/Exception.php';
  163. throw new \Zend\Controller\Exception('Invalid request method specified');
  164. }
  165. $this->_method = $type;
  166. return $this;
  167. }
  168. /**
  169. * Get request method
  170. *
  171. * @return string|null
  172. */
  173. public function getMethod()
  174. {
  175. return $this->_method;
  176. }
  177. /**
  178. * Set a request header
  179. *
  180. * @param string $key
  181. * @param string $value
  182. * @return Zend_Controller_Request_HttpTestCase
  183. */
  184. public function setHeader($key, $value)
  185. {
  186. $key = $this->_normalizeHeaderName($key);
  187. $this->_headers[$key] = (string) $value;
  188. return $this;
  189. }
  190. /**
  191. * Set request headers
  192. *
  193. * @param array $headers
  194. * @return Zend_Controller_Request_HttpTestCase
  195. */
  196. public function setHeaders(array $headers)
  197. {
  198. foreach ($headers as $key => $value) {
  199. $this->setHeader($key, $value);
  200. }
  201. return $this;
  202. }
  203. /**
  204. * Get request header
  205. *
  206. * @param string $header
  207. * @param mixed $default
  208. * @return string|null
  209. */
  210. public function getHeader($header, $default = null)
  211. {
  212. $header = $this->_normalizeHeaderName($header);
  213. if (array_key_exists($header, $this->_headers)) {
  214. return $this->_headers[$header];
  215. }
  216. return $default;
  217. }
  218. /**
  219. * Get all request headers
  220. *
  221. * @return array
  222. */
  223. public function getHeaders()
  224. {
  225. return $this->_headers;
  226. }
  227. /**
  228. * Clear request headers
  229. *
  230. * @return Zend_Controller_Request_HttpTestCase
  231. */
  232. public function clearHeaders()
  233. {
  234. $this->_headers = array();
  235. return $this;
  236. }
  237. /**
  238. * Get REQUEST_URI
  239. *
  240. * @return null|string
  241. */
  242. public function getRequestUri()
  243. {
  244. return $this->_requestUri;
  245. }
  246. /**
  247. * Normalize a header name for setting and retrieval
  248. *
  249. * @param string $name
  250. * @return string
  251. */
  252. protected function _normalizeHeaderName($name)
  253. {
  254. $name = strtoupper((string) $name);
  255. $name = str_replace('-', '_', $name);
  256. return $name;
  257. }
  258. }