Skip to content

Commit 042ceee

Browse files
committed
Rework controller execute
1 parent 2332593 commit 042ceee

2 files changed

Lines changed: 53 additions & 40 deletions

File tree

system/Test/ControllerTestTrait.php

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Test;
1313

1414
use CodeIgniter\Controller;
15+
use CodeIgniter\HTTP\Exceptions\HTTPException;
1516
use CodeIgniter\HTTP\IncomingRequest;
1617
use CodeIgniter\HTTP\Response;
1718
use CodeIgniter\HTTP\URI;
@@ -79,7 +80,7 @@ trait ControllerTestTrait
7980
protected $uri = 'http://example.com';
8081

8182
/**
82-
* Request or response body.
83+
* Request body.
8384
*
8485
* @var string|null
8586
*/
@@ -88,8 +89,11 @@ trait ControllerTestTrait
8889
/**
8990
* Initializes required components.
9091
*/
91-
protected function setUpControllerTester(): void
92+
protected function setUpControllerTestTrait(): void
9293
{
94+
// The URL helper is always loaded by the system so ensure it is available.
95+
helper('url');
96+
9397
if (empty($this->appConfig))
9498
{
9599
$this->appConfig = config('App');
@@ -160,19 +164,11 @@ public function execute(string $method, ...$params)
160164
throw new InvalidArgumentException('Method does not exist or is not callable in controller: ' . $method);
161165
}
162166

163-
// The URL helper is always loaded by the system
164-
// so ensure it's available.
165-
helper('url');
166-
167-
$result = (new TestResponse())
168-
->setRequest($this->request)
169-
->setResponse($this->response);
170-
171167
$response = null;
168+
172169
try
173170
{
174171
ob_start();
175-
176172
$response = $this->controller->{$method}(...$params);
177173
}
178174
catch (Throwable $e)
@@ -184,43 +180,60 @@ public function execute(string $method, ...$params)
184180
{
185181
throw $e;
186182
}
187-
188-
$result->response()->setStatusCode($code);
189183
}
190184
finally
191185
{
192186
$output = ob_get_clean();
187+
}
193188

194-
// If the controller returned a response, use it
195-
if (isset($response) && $response instanceof Response)
196-
{
197-
$result->setResponse($response);
198-
}
189+
// If the controller returned a view then add it to the output
190+
if (is_string($response))
191+
{
192+
$output = is_string($output) ? $output . $response : $response;
193+
}
199194

200-
// check if controller returned a view rather than echoing it
201-
if (is_string($response))
202-
{
203-
$output = $response;
204-
$result->response()->setBody($output);
205-
$result->setBody($output);
206-
}
207-
elseif (! empty($response) && ! empty($response->getBody()))
195+
// If the controller did not return a response then start one
196+
if (! $response instanceof Response)
197+
{
198+
$response = $this->response;
199+
}
200+
201+
// Check for output to set or prepend
202+
// @see \CodeIgniter\CodeIgniter::gatherOutput()
203+
if (is_string($output))
204+
{
205+
if (is_string($response->getBody()))
208206
{
209-
$result->setBody($response->getBody());
207+
$response->setBody($output . $response->getBody());
210208
}
211209
else
212210
{
213-
$result->setBody('');
211+
$response->setBody($output);
214212
}
215213
}
216214

217-
// If not response code has been sent, assume a success
218-
if (empty($result->response()->getStatusCode()))
215+
// Check for an overriding code from exceptions
216+
if (isset($code))
219217
{
220-
$result->response()->setStatusCode(200);
218+
$response->setStatusCode($code);
219+
}
220+
// Otherwise ensure there is a status code
221+
else
222+
{
223+
// getStatusCode() throws for empty codes
224+
try
225+
{
226+
$response->getStatusCode();
227+
}
228+
catch (HTTPException $e)
229+
{
230+
// If no code has been set then assume success
231+
$response->setStatusCode(200);
232+
}
221233
}
222234

223-
return $result;
235+
// Create the result and add the Request for reference
236+
return (new TestResponse($response))->setRequest($this->request);
224237
}
225238

226239
/**

tests/system/Test/ControllerTestTraitTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testController()
4747
->controller(Home::class)
4848
->execute('index');
4949

50-
$body = $result->getBody();
50+
$body = $result->response()->getBody();
5151
$this->assertTrue($result->isOK());
5252
}
5353

@@ -57,7 +57,7 @@ public function testControllerWithoutLogger()
5757
->controller(Home::class)
5858
->execute('index');
5959

60-
$body = $result->getBody();
60+
$body = $result->response()->getBody();
6161
$this->assertTrue($result->isOK());
6262
}
6363

@@ -69,7 +69,7 @@ public function testPopcornIndex()
6969
->controller(Popcorn::class)
7070
->execute('index');
7171

72-
$body = $result->getBody();
72+
$body = $result->response()->getBody();
7373
$this->assertTrue($result->isOK());
7474
}
7575

@@ -81,7 +81,7 @@ public function testPopcornIndex2()
8181
->controller(Popcorn::class)
8282
->execute('index');
8383

84-
$body = $result->getBody();
84+
$body = $result->response()->getBody();
8585
$this->assertEquals('Hi there', $body);
8686
}
8787

@@ -122,7 +122,7 @@ public function testPopcornIndexWithSupport()
122122
->controller(Popcorn::class)
123123
->execute('index');
124124

125-
$body = $result->getBody();
125+
$body = $result->response()->getBody();
126126
$this->assertEquals('Hi there', $body);
127127
}
128128

@@ -158,7 +158,7 @@ public function testEmptyResponse()
158158
->controller(Popcorn::class)
159159
->execute('weasel');
160160

161-
$body = $result->getBody(); // empty
161+
$body = $result->response()->getBody(); // empty
162162
$this->assertEmpty($body);
163163
$this->assertFalse($result->isOK());
164164
}
@@ -204,7 +204,7 @@ public function testResponseOverriding()
204204
->controller(Popcorn::class)
205205
->execute('index3');
206206

207-
$response = json_decode($result->getBody());
207+
$response = json_decode($result->response()->getBody());
208208
$this->assertEquals('en', $response->lang);
209209
}
210210

@@ -216,7 +216,7 @@ public function testControllerNoURI()
216216
->controller(Home::class)
217217
->execute('index');
218218

219-
$body = $result->getBody();
219+
$body = $result->response()->getBody();
220220
$this->assertTrue($result->isOK());
221221
}
222222

0 commit comments

Comments
 (0)