ResponseTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  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) Kohana Team
  13. * @license https://koseven.ga/LICENSE.md
  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->createMock('View');
  25. $view->expects($this->any())
  26. ->method('__toString')
  27. ->will($this->returnValue('foo'));
  28. return [
  29. ['unit test', 'unit test'],
  30. [$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 [
  57. ['0', '0'],
  58. ["0", '0'],
  59. [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 [
  85. [
  86. 'test1',
  87. 'foo',
  88. [
  89. 'test1' => [
  90. 'value' => 'foo',
  91. 'expiration' => Cookie::$expiration
  92. ],
  93. ]
  94. ],
  95. [
  96. [
  97. 'test2' => 'stfu',
  98. 'test3' => [
  99. 'value' => 'snafu',
  100. 'expiration' => 123456789
  101. ]
  102. ],
  103. NULL,
  104. [
  105. 'test2' => [
  106. 'value' => 'stfu',
  107. 'expiration' => Cookie::$expiration
  108. ],
  109. 'test3' => [
  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([], $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. * Test the content type is sent when set
  159. *
  160. * @test
  161. */
  162. public function test_content_type_when_set()
  163. {
  164. $content_type = 'application/json';
  165. $response = new Response;
  166. $response->headers('content-type', $content_type);
  167. $headers = $response->send_headers()->headers();
  168. $this->assertSame($content_type, (string) $headers['content-type']);
  169. }
  170. }