From dbfd8ff6edeab4e9bd19f6a046d017f3df96375d Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Jun 2023 18:18:27 +0900 Subject: [PATCH 1/5] test: change test method names Add the method name to test. --- tests/system/Test/TestResponseTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/system/Test/TestResponseTest.php b/tests/system/Test/TestResponseTest.php index 84275808a7..0291c8cd66 100644 --- a/tests/system/Test/TestResponseTest.php +++ b/tests/system/Test/TestResponseTest.php @@ -305,7 +305,7 @@ final class TestResponseTest extends CIUnitTestCase $this->assertSame($formatter->format(['foo' => 'bar']), $this->testResponse->getJSON()); } - public function testEmptyJSON() + public function testGetJSONEmptyJSON() { $this->getTestResponse('

Hello World

'); $this->response->setJSON('', true); @@ -314,7 +314,7 @@ final class TestResponseTest extends CIUnitTestCase $this->assertSame('""', $this->testResponse->getJSON()); } - public function testFalseJSON() + public function testGetJSONFalseJSON() { $this->getTestResponse('

Hello World

'); $this->response->setJSON(false, true); @@ -323,7 +323,7 @@ final class TestResponseTest extends CIUnitTestCase $this->assertSame('false', $this->testResponse->getJSON()); } - public function testTrueJSON() + public function testGetJSONTrueJSON() { $this->getTestResponse('

Hello World

'); $this->response->setJSON(true, true); @@ -332,7 +332,7 @@ final class TestResponseTest extends CIUnitTestCase $this->assertSame('true', $this->testResponse->getJSON()); } - public function testInvalidJSON() + public function testGetJSONInvalidJSON() { $tmp = ' test " case '; $this->getTestResponse('

Hello World

'); @@ -350,7 +350,7 @@ final class TestResponseTest extends CIUnitTestCase $this->assertSame($formatter->format(['foo' => 'bar']), $this->testResponse->getXML()); } - public function testJsonFragment() + public function testAssertJSONFragment() { $this->getTestResponse([ 'config' => [ @@ -383,7 +383,7 @@ final class TestResponseTest extends CIUnitTestCase $this->testResponse->assertJSONFragment(['foo' => 'bar']); } - public function testJsonExact() + public function testAssertJsonExactArray() { $data = [ 'config' => [ @@ -397,7 +397,7 @@ final class TestResponseTest extends CIUnitTestCase $this->testResponse->assertJSONExact($data); } - public function testJsonExactString() + public function testAssertJsonExactString() { $data = [ 'config' => [ From e5343329058b2055ff0cf0e4da818d623e4dd17e Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Jun 2023 18:19:16 +0900 Subject: [PATCH 2/5] test: add param type for Response body --- tests/system/Test/TestResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Test/TestResponseTest.php b/tests/system/Test/TestResponseTest.php index 0291c8cd66..1159de9ead 100644 --- a/tests/system/Test/TestResponseTest.php +++ b/tests/system/Test/TestResponseTest.php @@ -412,7 +412,7 @@ final class TestResponseTest extends CIUnitTestCase $this->testResponse->assertJSONExact($formatter->format($data)); } - protected function getTestResponse($body = null, array $responseOptions = [], array $headers = []) + protected function getTestResponse(?string $body = null, array $responseOptions = [], array $headers = []) { $this->response = new Response(new App()); $this->response->setBody($body); From 66526a8d5ea29082b3e28fad0622ec4327d5f372 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Jun 2023 18:29:29 +0900 Subject: [PATCH 3/5] fix: incorrect tests Cannot set non-string to Response body. --- tests/system/Test/TestResponseTest.php | 36 ++++++++++++++++---------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/system/Test/TestResponseTest.php b/tests/system/Test/TestResponseTest.php index 1159de9ead..0b01c52a0b 100644 --- a/tests/system/Test/TestResponseTest.php +++ b/tests/system/Test/TestResponseTest.php @@ -299,10 +299,12 @@ final class TestResponseTest extends CIUnitTestCase public function testGetJSON() { - $this->getTestResponse(['foo' => 'bar']); - $formatter = Services::format()->getFormatter('application/json'); + $data = ['foo' => 'bar']; + $this->getTestResponse(''); + $this->response->setJSON($data, true); - $this->assertSame($formatter->format(['foo' => 'bar']), $this->testResponse->getJSON()); + $formatter = Services::format()->getFormatter('application/json'); + $this->assertSame($formatter->format($data), $this->testResponse->getJSON()); } public function testGetJSONEmptyJSON() @@ -344,20 +346,24 @@ final class TestResponseTest extends CIUnitTestCase public function testGetXML() { - $this->getTestResponse(['foo' => 'bar']); - $formatter = Services::format()->getFormatter('application/xml'); + $data = ['foo' => 'bar']; + $this->getTestResponse(''); + $this->response->setXML($data); - $this->assertSame($formatter->format(['foo' => 'bar']), $this->testResponse->getXML()); + $formatter = Services::format()->getFormatter('application/xml'); + $this->assertSame($formatter->format($data), $this->testResponse->getXML()); } public function testAssertJSONFragment() { - $this->getTestResponse([ + $data = [ 'config' => [ 'key-a', 'key-b', ], - ]); + ]; + $this->getTestResponse(''); + $this->response->setJSON($data, true); $this->testResponse->assertJSONFragment(['config' => ['key-a']]); $this->testResponse->assertJSONFragment(['config' => ['key-a']], true); @@ -365,9 +371,11 @@ final class TestResponseTest extends CIUnitTestCase public function testAssertJSONFragmentFollowingAssertArraySubset() { - $this->getTestResponse([ + $data = [ 'config' => '124', - ]); + ]; + $this->getTestResponse(''); + $this->response->setJSON($data, true); $this->testResponse->assertJSONFragment(['config' => 124]); // must fail on strict $this->testResponse->assertJSONFragment(['config' => '124'], true); @@ -391,8 +399,8 @@ final class TestResponseTest extends CIUnitTestCase 'key-b', ], ]; - - $this->getTestResponse($data); + $this->getTestResponse(''); + $this->response->setJSON($data, true); $this->testResponse->assertJSONExact($data); } @@ -405,10 +413,10 @@ final class TestResponseTest extends CIUnitTestCase 'key-b', ], ]; + $this->getTestResponse(''); + $this->response->setJSON($data, true); - $this->getTestResponse($data); $formatter = Services::format()->getFormatter('application/json'); - $this->testResponse->assertJSONExact($formatter->format($data)); } From 2bcebad8e3412387161500641701e1c3aecb3303 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Jun 2023 18:35:23 +0900 Subject: [PATCH 4/5] test: remove testCallWithJsonRequestObject() and add testAssertJsonExactObject() testCallWithJsonRequestObject() is not a test for FeatureTestTrait. --- tests/system/Test/FeatureTestTraitTest.php | 14 -------------- tests/system/Test/TestResponseTest.php | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/system/Test/FeatureTestTraitTest.php b/tests/system/Test/FeatureTestTraitTest.php index d622a00aae..0d0237301f 100644 --- a/tests/system/Test/FeatureTestTraitTest.php +++ b/tests/system/Test/FeatureTestTraitTest.php @@ -372,20 +372,6 @@ final class FeatureTestTraitTest extends CIUnitTestCase $response->assertJSONExact(['foo' => 'bar']); } - public function testCallWithJsonRequestObject() - { - $this->withRoutes([ - [ - 'post', - 'home', - '\Tests\Support\Controllers\Popcorn::echoJson', - ], - ]); - $response = $this->withBodyFormat('json')->call('post', 'home', ['foo' => 'bar']); - $response->assertOK(); - $response->assertJSONExact((object) ['foo' => 'bar']); - } - public function testSetupRequestBodyWithParams() { $request = $this->setupRequest('post', 'home'); diff --git a/tests/system/Test/TestResponseTest.php b/tests/system/Test/TestResponseTest.php index 0b01c52a0b..8abc71c6d9 100644 --- a/tests/system/Test/TestResponseTest.php +++ b/tests/system/Test/TestResponseTest.php @@ -405,6 +405,20 @@ final class TestResponseTest extends CIUnitTestCase $this->testResponse->assertJSONExact($data); } + public function testAssertJsonExactObject() + { + $data = (object) [ + 'config' => [ + 'key-a', + 'key-b', + ], + ]; + $this->getTestResponse(''); + $this->response->setJSON($data, true); + + $this->testResponse->assertJSONExact($data); + } + public function testAssertJsonExactString() { $data = [ From 202b4678834d4d92442b2c78c27e1428dea10669 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Jun 2023 18:36:46 +0900 Subject: [PATCH 5/5] docs: fix @param types --- system/Format/FormatterInterface.php | 2 +- system/HTTP/ResponseTrait.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/system/Format/FormatterInterface.php b/system/Format/FormatterInterface.php index 277299eea2..6e8e9bd503 100644 --- a/system/Format/FormatterInterface.php +++ b/system/Format/FormatterInterface.php @@ -19,7 +19,7 @@ interface FormatterInterface /** * Takes the given data and formats it. * - * @param array|string $data + * @param array|object|string $data * * @return false|string */ diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index 133d1b193b..7fe5f7c2a8 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -239,7 +239,7 @@ trait ResponseTrait /** * Converts the $body into JSON and sets the Content Type header. * - * @param array|string $body + * @param array|object|string $body * * @return $this */ @@ -304,8 +304,8 @@ trait ResponseTrait * Handles conversion of the data into the appropriate format, * and sets the correct Content-Type header for our response. * - * @param array|string $body - * @param string $format Valid: json, xml + * @param array|object|string $body + * @param string $format Valid: json, xml * * @return mixed *