HttpTestCaseTest.php 8.8 KB

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