123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- * Unit tests for response class
- *
- * @group ko7
- * @group ko7.core
- * @group ko7.core.response
- *
- * @package KO7
- * @category Tests
- *
- * @copyright (c) 2007-2016 Kohana Team
- * @copyright (c) since 2016 Koseven Team
- * @license https://koseven.dev/LICENSE
- */
- class KO7_ResponseTest extends Unittest_TestCase
- {
- /**
- * Provider for test_body
- *
- * @return array
- */
- public function provider_body()
- {
- $view = $this->createMock('View');
- $view->expects($this->any())
- ->method('__toString')
- ->will($this->returnValue('foo'));
- return [
- ['unit test', 'unit test'],
- [$view, 'foo'],
- ];
- }
- /**
- * Tests that we can set and read a body of a response
- *
- * @test
- * @dataProvider provider_body
- *
- * @return null
- */
- public function test_body($source, $expected)
- {
- $response = new Response;
- $response->body($source);
- $this->assertSame($response->body(), $expected);
- $response = (string) $response;
- $this->assertSame($response, $expected);
- }
- /**
- * Provides data for test_body_string_zero()
- *
- * @return array
- */
- public function provider_body_string_zero()
- {
- return [
- ['0', '0'],
- ["0", '0'],
- [0, '0']
- ];
- }
- /**
- * Test that Response::body() handles numerics correctly
- *
- * @test
- * @dataProvider provider_body_string_zero
- * @param string $string
- * @param string $expected
- * @return void
- */
- public function test_body_string_zero($string, $expected)
- {
- $response = new Response;
- $response->body($string);
- $this->assertSame($expected, $response->body());
- }
- /**
- * provider for test_cookie_set()
- *
- * @return array
- */
- public function provider_cookie_set()
- {
- return [
- [
- 'test1',
- 'foo',
- [
- 'test1' => [
- 'value' => 'foo',
- 'expiration' => Cookie::$expiration
- ],
- ]
- ],
- [
- [
- 'test2' => 'stfu',
- 'test3' => [
- 'value' => 'snafu',
- 'expiration' => 123456789
- ]
- ],
- NULL,
- [
- 'test2' => [
- 'value' => 'stfu',
- 'expiration' => Cookie::$expiration
- ],
- 'test3' => [
- 'value' => 'snafu',
- 'expiration' => 123456789
- ]
- ]
- ]
- ];
- }
- /**
- * Tests the Response::cookie() method, ensures
- * correct values are set, including defaults
- *
- * @test
- * @dataProvider provider_cookie_set
- * @param string $key
- * @param string $value
- * @param string $expected
- * @return void
- */
- public function test_cookie_set($key, $value, $expected)
- {
- // Setup the Response and apply cookie
- $response = new Response;
- $response->cookie($key, $value);
- foreach ($expected as $_key => $_value)
- {
- $cookie = $response->cookie($_key);
- $this->assertSame($_value['value'], $cookie['value']);
- $this->assertSame($_value['expiration'], $cookie['expiration']);
- }
- }
- /**
- * Tests the Response::cookie() get functionality
- *
- * @return void
- */
- public function test_cookie_get()
- {
- $response = new Response;
- // Test for empty cookies
- $this->assertSame([], $response->cookie());
- // Test for no specific cookie
- $this->assertNull($response->cookie('foobar'));
- $response->cookie('foo', 'bar');
- $cookie = $response->cookie('foo');
- $this->assertSame('bar', $cookie['value']);
- $this->assertSame(Cookie::$expiration, $cookie['expiration']);
- }
- /**
- * Test the content type is sent when set
- *
- * @test
- */
- public function test_content_type_when_set()
- {
- $content_type = 'application/json';
- $response = new Response;
- $response->headers('content-type', $content_type);
- $headers = $response->send_headers()->headers();
- $this->assertSame($content_type, (string) $headers['content-type']);
- }
- }
|