/tests/Zend/Controller/Request/HttpTestCaseTest.php

https://github.com/jupeter/zf1 · PHP · 316 lines · 229 code · 33 blank · 54 comment · 3 complexity · 265c177790e6c2f12610de9cb0230628 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. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Controller_Request_HttpTestCaseTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Request_HttpTestCaseTest::main");
  25. }
  26. /** Zend_Controller_Request_HttpTestCase */
  27. require_once 'Zend/Controller/Request/HttpTestCase.php';
  28. /**
  29. * Test class for Zend_Controller_Request_HttpTestCase.
  30. *
  31. * @category Zend
  32. * @package Zend_Controller
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Controller
  37. * @group Zend_Controller_Request
  38. */
  39. class Zend_Controller_Request_HttpTestCaseTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Request_HttpTestCaseTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Sets up the fixture, for example, open a network connection.
  53. * This method is called before a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. $this->request = new Zend_Controller_Request_HttpTestCase();
  60. $_GET = array();
  61. $_POST = array();
  62. $_COOKIE = array();
  63. }
  64. /**
  65. * Tears down the fixture, for example, close a network connection.
  66. * This method is called after a test is executed.
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. }
  73. public function testGetRequestUriShouldNotAttemptToAutoDiscoverFromEnvironment()
  74. {
  75. $this->assertNull($this->request->getRequestUri());
  76. }
  77. public function testGetPathInfoShouldNotAttemptToAutoDiscoverFromEnvironment()
  78. {
  79. $pathInfo = $this->request->getPathInfo();
  80. $this->assertTrue(empty($pathInfo));
  81. }
  82. public function testGetShouldBeEmptyByDefault()
  83. {
  84. $post = $this->request->getQuery();
  85. $this->assertTrue(is_array($post));
  86. $this->assertTrue(empty($post));
  87. }
  88. public function testShouldAllowSpecifyingGetParameters()
  89. {
  90. $this->testGetShouldBeEmptyByDefault();
  91. $expected = array(
  92. 'foo' => 'bar',
  93. 'bar' => 'baz',
  94. 'baz' => 'bat',
  95. );
  96. $this->request->setQuery($expected);
  97. $test = $this->request->getQuery();
  98. $this->assertSame($expected, $test);
  99. $this->request->setQuery('bat', 'bogus');
  100. $this->assertEquals('bogus', $this->request->getQuery('bat'));
  101. $test = $this->request->getQuery();
  102. $this->assertEquals(4, count($test));
  103. foreach ($expected as $key => $value) {
  104. $this->assertEquals($value, $test[$key]);
  105. }
  106. }
  107. public function testShouldPopulateGetSuperglobal()
  108. {
  109. $this->testShouldAllowSpecifyingGetParameters();
  110. $expected = array(
  111. 'foo' => 'bar',
  112. 'bar' => 'baz',
  113. 'baz' => 'bat',
  114. 'bat' => 'bogus',
  115. );
  116. $this->assertEquals($expected, $_GET);
  117. }
  118. public function testShouldAllowClearingQuery()
  119. {
  120. $this->testShouldPopulateGetSuperglobal();
  121. $this->request->clearQuery();
  122. $test = $this->request->getQuery();
  123. $this->assertTrue(is_array($test));
  124. $this->assertTrue(empty($test));
  125. }
  126. public function testPostShouldBeEmptyByDefault()
  127. {
  128. $post = $this->request->getPost();
  129. $this->assertTrue(is_array($post));
  130. $this->assertTrue(empty($post));
  131. }
  132. public function testShouldAllowSpecifyingPostParameters()
  133. {
  134. $this->testPostShouldBeEmptyByDefault();
  135. $expected = array(
  136. 'foo' => 'bar',
  137. 'bar' => 'baz',
  138. 'baz' => 'bat',
  139. );
  140. $this->request->setPost($expected);
  141. $test = $this->request->getPost();
  142. $this->assertSame($expected, $test);
  143. $this->request->setPost('bat', 'bogus');
  144. $this->assertEquals('bogus', $this->request->getPost('bat'));
  145. $test = $this->request->getPost();
  146. $this->assertEquals(4, count($test));
  147. foreach ($expected as $key => $value) {
  148. $this->assertEquals($value, $test[$key]);
  149. }
  150. }
  151. public function testShouldPopulatePostSuperglobal()
  152. {
  153. $this->testShouldAllowSpecifyingPostParameters();
  154. $expected = array(
  155. 'foo' => 'bar',
  156. 'bar' => 'baz',
  157. 'baz' => 'bat',
  158. 'bat' => 'bogus',
  159. );
  160. $this->assertEquals($expected, $_POST);
  161. }
  162. public function testShouldAllowClearingPost()
  163. {
  164. $this->testShouldPopulatePostSuperglobal();
  165. $this->request->clearPost();
  166. $test = $this->request->getPost();
  167. $this->assertTrue(is_array($test));
  168. $this->assertTrue(empty($test));
  169. }
  170. public function testRawPostBodyShouldBeNullByDefault()
  171. {
  172. $this->assertNull($this->request->getRawBody());
  173. }
  174. public function testShouldAllowSpecifyingRawPostBody()
  175. {
  176. $this->request->setRawBody('Some content for the body');
  177. $this->assertEquals('Some content for the body', $this->request->getRawBody());
  178. }
  179. public function testShouldAllowClearingRawPostBody()
  180. {
  181. $this->testShouldAllowSpecifyingRawPostBody();
  182. $this->request->clearRawBody();
  183. $this->assertNull($this->request->getRawBody());
  184. }
  185. public function testHeadersShouldBeEmptyByDefault()
  186. {
  187. $headers = $this->request->getHeaders();
  188. $this->assertTrue(is_array($headers));
  189. $this->assertTrue(empty($headers));
  190. }
  191. public function testShouldAllowSpecifyingRequestHeaders()
  192. {
  193. $headers = array(
  194. 'Content-Type' => 'text/html',
  195. 'Content-Encoding' => 'utf-8',
  196. );
  197. $this->request->setHeaders($headers);
  198. $test = $this->request->getHeaders();
  199. $this->assertTrue(is_array($test));
  200. $this->assertEquals(2, count($test));
  201. foreach ($headers as $key => $value) {
  202. $this->assertEquals($value, $this->request->getHeader($key));
  203. }
  204. $this->request->setHeader('X-Requested-With', 'XMLHttpRequest');
  205. $test = $this->request->getHeaders();
  206. $this->assertTrue(is_array($test));
  207. $this->assertEquals(3, count($test));
  208. $this->assertEquals('XMLHttpRequest', $this->request->getHeader('X-Requested-With'));
  209. }
  210. public function testShouldAllowClearingRequestHeaders()
  211. {
  212. $this->testShouldAllowSpecifyingRequestHeaders();
  213. $this->request->clearHeaders();
  214. $headers = $this->request->getHeaders();
  215. $this->assertTrue(is_array($headers));
  216. $this->assertTrue(empty($headers));
  217. }
  218. public function testCookiesShouldBeEmptyByDefault()
  219. {
  220. $cookies = $this->request->getCookie();
  221. $this->assertTrue(is_array($cookies));
  222. $this->assertTrue(empty($cookies));
  223. }
  224. public function testShouldAllowSpecifyingCookies()
  225. {
  226. $cookies = array(
  227. 'foo' => 'bar',
  228. 'bar' => 'baz',
  229. 'baz' => 'bat'
  230. );
  231. $this->request->setCookies($cookies);
  232. $test = $this->request->getCookie();
  233. $this->assertEquals($cookies, $test);
  234. $this->request->setCookie('bat', 'bogus');
  235. $this->assertEquals('bogus', $this->request->getCookie('bat'));
  236. }
  237. public function testShouldPopulateCookieSuperGlobal()
  238. {
  239. $cookies = array(
  240. 'foo' => 'bar',
  241. 'bar' => 'baz',
  242. 'baz' => 'bat',
  243. 'bat' => 'bogus',
  244. );
  245. $this->testShouldAllowSpecifyingCookies();
  246. $this->assertEquals($cookies, $_COOKIE);
  247. }
  248. public function testShouldAllowClearingAllCookies()
  249. {
  250. $this->testShouldAllowSpecifyingCookies();
  251. $this->request->clearCookies();
  252. $test = $this->request->getCookie();
  253. $this->assertTrue(is_array($test));
  254. $this->assertTrue(empty($test));
  255. }
  256. /**
  257. * @group ZF-6162
  258. */
  259. public function testRequestMethodShouldBeGetByDefault()
  260. {
  261. $this->assertEquals('GET', $this->request->getMethod());
  262. }
  263. public function testShouldAllowSpecifyingRequestMethod()
  264. {
  265. $this->testRequestMethodShouldBeGetByDefault();
  266. $this->request->setMethod('POST');
  267. $this->assertTrue($this->request->isPost());
  268. $this->request->setMethod('GET');
  269. $this->assertTrue($this->request->isGet());
  270. $this->request->setMethod('PUT');
  271. $this->assertTrue($this->request->isPut());
  272. $this->request->setMethod('OPTIONS');
  273. $this->assertTrue($this->request->isOptions());
  274. $this->request->setMethod('HEAD');
  275. $this->assertTrue($this->request->isHead());
  276. $this->request->setMethod('DELETE');
  277. $this->assertTrue($this->request->isDelete());
  278. }
  279. }
  280. // Call Zend_Controller_Request_HttpTestCaseTest::main() if this source file is executed directly.
  281. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Request_HttpTestCaseTest::main") {
  282. Zend_Controller_Request_HttpTestCaseTest::main();
  283. }