PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/share/test/web/response/response-test.php

https://github.com/php-tox/tox
PHP | 303 lines | 222 code | 25 blank | 56 comment | 0 complexity | 5984ccb7945b4833ada81c09a809b68e MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Defines the test case for Tox\Web\Response\Response.
  4. *
  5. * This file is part of Tox.
  6. *
  7. * Tox is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @copyright Š 2012-2013 PHP-Tox.org
  21. * @license GNU General Public License, version 3
  22. */
  23. namespace Tox\Web\Response;
  24. use PHPUnit_Framework_TestCase;
  25. require_once __DIR__ . '/../../../../src/core/assembly.php';
  26. require_once __DIR__ . '/../../../../src/application/ioutput.php';
  27. require_once __DIR__ . '/../../../../src/application/output/output.php';
  28. require_once __DIR__ . '/../../../../src/web/iresponse.php';
  29. require_once __DIR__ . '/../../../../src/web/response/response.php';
  30. require_once __DIR__ . '/../../../../src/core/exception.php';
  31. require_once __DIR__ . '/../../../../src/application/output/closedoutputexception.php';
  32. require_once __DIR__ . '/../../../../src/web/response/headersreadonlyexception.php';
  33. require_once __DIR__ . '/../../../../src/web/response/illegalhttpstatuscodeexception.php';
  34. require_once __DIR__ . '/../../../../src/application/ioutputtask.php';
  35. require_once __DIR__ . '/../../../../src/web/ihttpheadersprocessor.php';
  36. require_once __DIR__ . '/../../../../src/web/response/httpheadersprocessor.php';
  37. require_once __DIR__ . '/../../../../src/web/ipagecacheagent.php';
  38. require_once __DIR__ . '/../../../../src/web/response/pagecacheagent.php';
  39. require_once __DIR__ . '/../../../../src/application/iview.php';
  40. require_once __DIR__ . '/../../../../src/application/view/view.php';
  41. require_once __DIR__ . '/../../../../src/application/istreamingview.php';
  42. require_once __DIR__ . '/../../../../src/application/view/streamingview.php';
  43. require_once __DIR__ . '/../../../../src/web/ipagecache.php';
  44. use Exception as PHPException;
  45. use Tox;
  46. /**
  47. * Tests Tox\Web\Response\Response.
  48. *
  49. * @internal
  50. *
  51. * @package tox.web.response
  52. * @author Snakevil Zen <zsnakevil@gmail.com>
  53. */
  54. class ResponseTest extends PHPUnit_Framework_TestCase
  55. {
  56. public function testAddingHeaders()
  57. {
  58. $o_out = new Response;
  59. $this->assertSame($o_out, $o_out->addHeader('foo', microtime()));
  60. $o_out->addHeader('foo', 'bar')->addHeader('bar', 'blah.1')->addHeader('bar', 'blah.2', false);
  61. $this->assertEquals(array('foo' => array('bar'), 'bar' => array('blah.1', 'blah.2')), $o_out->getHeaders());
  62. }
  63. public function testHeadersSentOnOutputtingButNotBuffering()
  64. {
  65. $o_out = new Response;
  66. $o_hhp = $this->getMock('Tox\\Web\\IHTTPHeadersProcessor');
  67. $o_hhp->expects($this->never())->method('preOutput');
  68. $this->assertSame($o_out, $o_out->setHeadersProcessor($o_hhp));
  69. $o_out = new Response;
  70. $o_hhp = $this->getMock('Tox\\Web\\IHTTPHeadersProcessor');
  71. $o_hhp->expects($this->once())->method('preOutput');
  72. ob_start();
  73. $o_out->setHeadersProcessor($o_hhp)->close();
  74. ob_end_clean();
  75. }
  76. /**
  77. * @depends testHeadersSentOnOutputtingButNotBuffering
  78. */
  79. public function testHeadersSentBeforeOutputting()
  80. {
  81. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('getHeaders'));
  82. $o_hhp = $this->getMock(
  83. 'Tox\\Web\\Response\\HTTPHeadersProcessor',
  84. array('postOutput', 'sendHeader'),
  85. array($o_out)
  86. );
  87. $o_out->setHeadersProcessor($o_hhp)
  88. ->expects($this->once())->method('getHeaders')->will($this->returnValue(array()));
  89. ob_start();
  90. $o_out->close();
  91. ob_end_clean();
  92. }
  93. /**
  94. * @depends testHeadersSentBeforeOutputting
  95. * @expectedException Tox\Web\Response\HeadersReadonlyException
  96. */
  97. public function testHeadersReadonly()
  98. {
  99. $o_out = new Response;
  100. $o_out->setHeaders(array());
  101. }
  102. public function testHeadersAccessableViaMagicMethods()
  103. {
  104. $a_values = array(microtime());
  105. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('getHeaders', 'setHeaders'));
  106. $o_out->expects($this->once())->method('getHeaders');
  107. $o_out->headers;
  108. $o_out->expects($this->once())->method('setHeaders')->with($this->equalTo($a_values));
  109. $o_out->headers = $a_values;
  110. }
  111. public function testPageCacheSetAfterOutputting()
  112. {
  113. $o_out = new Response;
  114. $o_pca = $this->getMock('Tox\\Web\\Response\\PageCacheAgent', array('preOutput'), array($o_out));
  115. $this->assertSame($o_out, $o_out->setPageCacheAgent($o_pca));
  116. $o_pc = $this->getMock('Tox\\Web\\IPageCache');
  117. $o_pc->expects($this->once())->method('put');
  118. $this->assertSame($o_out, $o_out->cacheTo($o_pc));
  119. ob_start();
  120. $o_out->close();
  121. ob_end_clean();
  122. }
  123. /**
  124. * @depends testPageCacheSetAfterOutputting
  125. */
  126. public function testGivenPageCacheWouldBeLostOnChangineAgent()
  127. {
  128. $o_out = new Response;
  129. $o_pc = $this->getMock('Tox\\Web\\IPageCache');
  130. $o_pc->expects($this->never())->method('put');
  131. ob_start();
  132. $o_out->cacheTo($o_pc)
  133. ->setPageCacheAgent($this->getMock('Tox\\Web\\Response\\PageCacheAgent', array(), array($o_out)))
  134. ->close();
  135. ob_end_clean();
  136. }
  137. /**
  138. * @depends testPageCacheSetAfterOutputting
  139. */
  140. public function testEmptyPageWouldBeCachedToo()
  141. {
  142. $o_out = new Response;
  143. $o_pc = $this->getMock('Tox\\Web\\IPageCache');
  144. $o_pc->expects($this->once())->method('put')->with($this->equalTo(''));
  145. ob_start();
  146. $o_out->cacheTo($o_pc)->close();
  147. ob_end_clean();
  148. }
  149. public function test301Redirecting()
  150. {
  151. $s_url = microtime();
  152. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('addHeader'));
  153. $o_out->expects($this->at(0))->method('addHeader')
  154. ->with($this->equalTo('Status'), $this->equalTo('301 Moved Permanently'))
  155. ->will($this->returnSelf());
  156. $o_out->expects($this->at(1))->method('addHeader')
  157. ->with($this->equalTo('Location'), $this->equalTo($s_url))
  158. ->will($this->returnSelf());
  159. ob_start();
  160. try {
  161. $this->assertSame($o_out, $o_out->redirect($s_url, true));
  162. $o_out->write('foo');
  163. } catch (Tox\Application\Output\ClosedOutputException $ex) {
  164. } catch (PHPException $ex) {
  165. $this->fail();
  166. }
  167. ob_end_clean();
  168. }
  169. /**
  170. * @depends test301Redirecting
  171. */
  172. public function test302Redirecting()
  173. {
  174. $s_url = microtime();
  175. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('addHeader'));
  176. $o_out->expects($this->at(0))->method('addHeader')
  177. ->with($this->equalTo('Status'), $this->equalTo('302 Found'))
  178. ->will($this->returnSelf());
  179. $o_out->expects($this->at(1))->method('addHeader')
  180. ->with($this->equalTo('Location'), $this->equalTo($s_url))
  181. ->will($this->returnSelf());
  182. ob_start();
  183. $o_out->redirect($s_url);
  184. ob_end_clean();
  185. }
  186. /**
  187. * @depends test301Redirecting
  188. */
  189. public function testRedirectingWouldDropAllBufferAndHeaders()
  190. {
  191. $o_out = new Response;
  192. $o_hhp = $this->getMock('Tox\\Web\\Response\\HTTPHeadersProcessor', array('sendHeader'), array($o_out));
  193. $o_hhp->expects($this->exactly(2))->method('sendHeader');
  194. $o_out->setHeadersProcessor($o_hhp)->write(microtime())->addHeader('foo', 'bar');
  195. ob_start();
  196. $o_out->redirect('blah');
  197. $this->assertEquals('', ob_get_clean());
  198. }
  199. /**
  200. * @dataProvider provideStatus
  201. */
  202. public function testSettingStatus($code, $title)
  203. {
  204. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('addHeader'));
  205. $o_out->expects($this->at(0))->method('addHeader')
  206. ->with($this->equalTo('Status'), $this->equalTo($title));
  207. $o_out->setStatus($code);
  208. }
  209. /**
  210. * @depends testSettingStatus
  211. * @expectedException Tox\Web\Response\IllegalHTTPStatusCodeException
  212. */
  213. public function testSettingIllegalStatus()
  214. {
  215. $o_out = new Response;
  216. $o_out->setStatus(987);
  217. }
  218. public function testSetAndGetCookie()
  219. {
  220. $o_out = new Response;
  221. $this->assertTrue($o_out->setCookie('test', 'testvalue', 0, '/', 'test.dev'));
  222. $this->assertTrue($o_out->setCookie('test1', 'testvalue', 10));
  223. $this->assertEmpty($o_out->exportCookie());
  224. $this->assertEmpty($o_out->getCookie('test'));
  225. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('getCookie'));
  226. $o_out->expects($this->at(0))
  227. ->method('getCookie')
  228. ->with($this->equalTo('test1'))
  229. ->will($this->returnValue(base64_encode('testvalue')));
  230. $this->assertEquals('testvalue', base64_decode($o_out->getCookie('test1')));
  231. }
  232. public function provideStatus()
  233. {
  234. return array(
  235. array('100', '100 Continue'),
  236. array('101', '101 Switching Protocols'),
  237. array('200', '200 OK'),
  238. array('201', '201 Created'),
  239. array('202', '202 Accepted'),
  240. array('203', '203 Non-Authoritative Information'),
  241. array('204', '204 No Content'),
  242. array('205', '205 Reset Content'),
  243. array('206', '206 Partial Content'),
  244. array('300', '300 Multiple Choices'),
  245. array('301', '301 Moved Permanently'),
  246. array('302', '302 Found'),
  247. array('303', '303 See Other'),
  248. array('304', '304 Not Modified'),
  249. array('305', '305 Use Proxy'),
  250. array('307', '307 Temporary Redirect'),
  251. array('400', '400 Bad Request'),
  252. array('401', '401 Unauthorized'),
  253. array('402', '402 Payment Required'),
  254. array('403', '403 Forbidden'),
  255. array('404', '404 Not Found'),
  256. array('405', '405 Method Not Allowed'),
  257. array('406', '406 Not Acceptable'),
  258. array('407', '407 Proxy Authentication Required'),
  259. array('408', '408 Request Timeout'),
  260. array('409', '409 Conflict'),
  261. array('410', '410 Gone'),
  262. array('411', '411 Length Required'),
  263. array('412', '412 Precondition Failed'),
  264. array('413', '413 Request Entity Too Large'),
  265. array('414', '414 Request-URI Too Long'),
  266. array('415', '415 Unsupported Media Type'),
  267. array('416', '416 Requested Range Not Satisfiable'),
  268. array('417', '417 Expectation Failed'),
  269. array('500', '500 Internal Server Error'),
  270. array('501', '501 Not Implemented'),
  271. array('502', '502 Bad Gateway'),
  272. array('503', '503 Service Unavailable'),
  273. array('504', '504 Gateway Timeout'),
  274. array('505', '505 HTTP Version Not Supported')
  275. );
  276. }
  277. }
  278. // vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120