Skip to content

Commit 57b0bc4

Browse files
authored
Merge pull request #1855 from jim-parry/testing29
Fix: ControllerTester::execute. Fixes #1834
2 parents f5563e6 + e8bbe4b commit 57b0bc4

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

system/Test/ControllerTester.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,17 @@ public function execute(string $method, ...$params)
158158
if (is_string($response))
159159
{
160160
$output = $response;
161+
$result->response()->setBody($output);
162+
$result->setBody($output);
163+
}
164+
elseif (! empty($response) && ! empty($response->getBody()))
165+
{
166+
$result->setBody($response->getBody());
167+
}
168+
else
169+
{
170+
$result->setBody('');
161171
}
162-
163-
$result->response()->setBody($output);
164-
$result->setBody($output);
165172
}
166173

167174
// If not response code has been sent, assume a success

tests/_support/Controllers/Popcorn.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function popper()
3131

3232
public function weasel()
3333
{
34-
$this->respond(['Nothing to see here'], 200);
34+
$this->respond('', 200);
3535
}
3636

3737
public function oops()
@@ -43,4 +43,16 @@ public function goaway()
4343
{
4444
return redirect()->to('/');
4545
}
46+
47+
// @see https://github.com/codeigniter4/CodeIgniter4/issues/1834
48+
public function index3()
49+
{
50+
$response = $this->response->setJSON([
51+
'lang' => $this->request->getLocale(),
52+
]);
53+
54+
// echo var_dump($this->response->getBody());
55+
return $response;
56+
}
57+
4658
}

tests/system/Test/ControllerTesterTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ public function testPopcornFailure()
9595
->controller(\Tests\Support\Controllers\Popcorn::class)
9696
->execute('pop');
9797

98-
$body = $result->getBody();
9998
$this->assertEquals(567, $result->response()->getStatusCode());
10099
}
101100

@@ -107,7 +106,6 @@ public function testPopcornException()
107106
->controller(\Tests\Support\Controllers\Popcorn::class)
108107
->execute('popper');
109108

110-
$body = $result->getBody();
111109
$this->assertEquals(500, $result->response()->getStatusCode());
112110
}
113111

@@ -163,6 +161,7 @@ public function testEmptyResponse()
163161
->execute('weasel');
164162

165163
$body = $result->getBody(); // empty
164+
$this->assertEmpty($body);
166165
$this->assertFalse($result->isOK());
167166
}
168167

@@ -200,4 +199,15 @@ public function testFailsForward()
200199
$this->assertNull($result->ohno('Hi'));
201200
}
202201

202+
// @see https://github.com/codeigniter4/CodeIgniter4/issues/1834
203+
public function testResponseOverriding()
204+
{
205+
$result = $this->withURI('http://example.com/rest/')
206+
->controller(\Tests\Support\Controllers\Popcorn::class)
207+
->execute('index3');
208+
209+
$response = json_decode($result->getBody());
210+
$this->assertEquals('en', $response->lang);
211+
}
212+
203213
}

0 commit comments

Comments
 (0)