From e8bbe4b892daed200aa58ef9ffeacab33a856b1b Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Thu, 21 Mar 2019 23:58:51 -0700 Subject: [PATCH] Fix: ControllerTester::execute. Fixes #1834 --- system/Test/ControllerTester.php | 13 ++++++++++--- tests/_support/Controllers/Popcorn.php | 14 +++++++++++++- tests/system/Test/ControllerTesterTest.php | 14 ++++++++++++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/system/Test/ControllerTester.php b/system/Test/ControllerTester.php index 1016d130369a..9d9270d8663a 100644 --- a/system/Test/ControllerTester.php +++ b/system/Test/ControllerTester.php @@ -158,10 +158,17 @@ public function execute(string $method, ...$params) if (is_string($response)) { $output = $response; + $result->response()->setBody($output); + $result->setBody($output); + } + elseif (! empty($response) && ! empty($response->getBody())) + { + $result->setBody($response->getBody()); + } + else + { + $result->setBody(''); } - - $result->response()->setBody($output); - $result->setBody($output); } // If not response code has been sent, assume a success diff --git a/tests/_support/Controllers/Popcorn.php b/tests/_support/Controllers/Popcorn.php index 7cc3a7966592..b3826d398fa8 100644 --- a/tests/_support/Controllers/Popcorn.php +++ b/tests/_support/Controllers/Popcorn.php @@ -31,7 +31,7 @@ public function popper() public function weasel() { - $this->respond(['Nothing to see here'], 200); + $this->respond('', 200); } public function oops() @@ -43,4 +43,16 @@ public function goaway() { return redirect()->to('/'); } + + // @see https://github.com/codeigniter4/CodeIgniter4/issues/1834 + public function index3() + { + $response = $this->response->setJSON([ + 'lang' => $this->request->getLocale(), + ]); + + // echo var_dump($this->response->getBody()); + return $response; + } + } diff --git a/tests/system/Test/ControllerTesterTest.php b/tests/system/Test/ControllerTesterTest.php index 220bcf2c5cc1..3c828970a160 100644 --- a/tests/system/Test/ControllerTesterTest.php +++ b/tests/system/Test/ControllerTesterTest.php @@ -95,7 +95,6 @@ public function testPopcornFailure() ->controller(\Tests\Support\Controllers\Popcorn::class) ->execute('pop'); - $body = $result->getBody(); $this->assertEquals(567, $result->response()->getStatusCode()); } @@ -107,7 +106,6 @@ public function testPopcornException() ->controller(\Tests\Support\Controllers\Popcorn::class) ->execute('popper'); - $body = $result->getBody(); $this->assertEquals(500, $result->response()->getStatusCode()); } @@ -163,6 +161,7 @@ public function testEmptyResponse() ->execute('weasel'); $body = $result->getBody(); // empty + $this->assertEmpty($body); $this->assertFalse($result->isOK()); } @@ -200,4 +199,15 @@ public function testFailsForward() $this->assertNull($result->ohno('Hi')); } + // @see https://github.com/codeigniter4/CodeIgniter4/issues/1834 + public function testResponseOverriding() + { + $result = $this->withURI('http://example.com/rest/') + ->controller(\Tests\Support\Controllers\Popcorn::class) + ->execute('index3'); + + $response = json_decode($result->getBody()); + $this->assertEquals('en', $response->lang); + } + }