ResponseTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Unit tests for response class
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.response
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @copyright (c) 2008-2012 Kohana Team
  13. * @license http://kohanaframework.org/license
  14. */
  15. class Kohana_ResponseTest extends Unittest_TestCase
  16. {
  17. /**
  18. * Provider for test_body
  19. *
  20. * @return array
  21. */
  22. public function provider_body()
  23. {
  24. $view = $this->getMock('View');
  25. $view->expects($this->any())
  26. ->method('__toString')
  27. ->will($this->returnValue('foo'));
  28. return array(
  29. array('unit test', 'unit test'),
  30. array($view, 'foo'),
  31. );
  32. }
  33. /**
  34. * Tests that we can set and read a body of a response
  35. *
  36. * @test
  37. * @dataProvider provider_body
  38. *
  39. * @return null
  40. */
  41. public function test_body($source, $expected)
  42. {
  43. $response = new Response;
  44. $response->body($source);
  45. $this->assertSame($response->body(), $expected);
  46. $response = (string) $response;
  47. $this->assertSame($response, $expected);
  48. }
  49. /**
  50. * Provides data for test_body_string_zero()
  51. *
  52. * @return array
  53. */
  54. public function provider_body_string_zero()
  55. {
  56. return array(
  57. array('0', '0'),
  58. array("0", '0'),
  59. array(0, '0')
  60. );
  61. }
  62. /**
  63. * Test that Response::body() handles numerics correctly
  64. *
  65. * @test
  66. * @dataProvider provider_body_string_zero
  67. * @param string $string
  68. * @param string $expected
  69. * @return void
  70. */
  71. public function test_body_string_zero($string, $expected)
  72. {
  73. $response = new Response;
  74. $response->body($string);
  75. $this->assertSame($expected, $response->body());
  76. }
  77. /**
  78. * provider for test_cookie_set()
  79. *
  80. * @return array
  81. */
  82. public function provider_cookie_set()
  83. {
  84. return array(
  85. array(
  86. 'test1',
  87. 'foo',
  88. array(
  89. 'test1' => array(
  90. 'value' => 'foo',
  91. 'expiration' => Cookie::$expiration
  92. ),
  93. )
  94. ),
  95. array(
  96. array(
  97. 'test2' => 'stfu',
  98. 'test3' => array(
  99. 'value' => 'snafu',
  100. 'expiration' => 123456789
  101. )
  102. ),
  103. NULL,
  104. array(
  105. 'test2' => array(
  106. 'value' => 'stfu',
  107. 'expiration' => Cookie::$expiration
  108. ),
  109. 'test3' => array(
  110. 'value' => 'snafu',
  111. 'expiration' => 123456789
  112. )
  113. )
  114. )
  115. );
  116. }
  117. /**
  118. * Tests the Response::cookie() method, ensures
  119. * correct values are set, including defaults
  120. *
  121. * @test
  122. * @dataProvider provider_cookie_set
  123. * @param string $key
  124. * @param string $value
  125. * @param string $expected
  126. * @return void
  127. */
  128. public function test_cookie_set($key, $value, $expected)
  129. {
  130. // Setup the Response and apply cookie
  131. $response = new Response;
  132. $response->cookie($key, $value);
  133. foreach ($expected as $_key => $_value)
  134. {
  135. $cookie = $response->cookie($_key);
  136. $this->assertSame($_value['value'], $cookie['value']);
  137. $this->assertSame($_value['expiration'], $cookie['expiration']);
  138. }
  139. }
  140. /**
  141. * Tests the Response::cookie() get functionality
  142. *
  143. * @return void
  144. */
  145. public function test_cookie_get()
  146. {
  147. $response = new Response;
  148. // Test for empty cookies
  149. $this->assertSame(array(), $response->cookie());
  150. // Test for no specific cookie
  151. $this->assertNull($response->cookie('foobar'));
  152. $response->cookie('foo', 'bar');
  153. $cookie = $response->cookie('foo');
  154. $this->assertSame('bar', $cookie['value']);
  155. $this->assertSame(Cookie::$expiration, $cookie['expiration']);
  156. }
  157. /**
  158. * Tests that the headers are not sent by PHP in CLI mode
  159. *
  160. * @return void
  161. */
  162. public function test_send_headers_cli()
  163. {
  164. if (headers_sent())
  165. {
  166. $this->markTestSkipped('Cannot test this feature as headers have already been sent!');
  167. }
  168. $content_type = 'application/json';
  169. $response = new Response;
  170. $response->headers('content-type', $content_type)
  171. ->send_headers();
  172. $this->assertFalse(headers_sent());
  173. }
  174. /**
  175. * Test the content type is sent when set
  176. *
  177. * @test
  178. */
  179. public function test_content_type_when_set()
  180. {
  181. $content_type = 'application/json';
  182. $response = new Response;
  183. $response->headers('content-type', $content_type);
  184. $headers = $response->send_headers()->headers();
  185. $this->assertSame($content_type, (string) $headers['content-type']);
  186. }
  187. }