ResponseTest.php 3.6 KB

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