HttpTestCaseTest.php 9.9 KB

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