1212namespace CodeIgniter \Test ;
1313
1414use CodeIgniter \Controller ;
15+ use CodeIgniter \HTTP \Exceptions \HTTPException ;
1516use CodeIgniter \HTTP \IncomingRequest ;
1617use CodeIgniter \HTTP \Response ;
1718use 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 /**
0 commit comments