diff --git a/system/Test/ControllerTestTrait.php b/system/Test/ControllerTestTrait.php index 6a784eb8c7e9..da53c7eb6c3d 100644 --- a/system/Test/ControllerTestTrait.php +++ b/system/Test/ControllerTestTrait.php @@ -108,7 +108,7 @@ protected function setUpControllerTestTrait(): void $tempUri = Services::uri(); Services::injectMock('uri', $this->uri); - $this->withRequest(Services::request($this->appConfig, false)->setBody($this->body)); + $this->withRequest(Services::request($this->appConfig, false)); // Restore the URI service Services::injectMock('uri', $tempUri); @@ -156,6 +156,7 @@ public function execute(string $method, ...$params) } $response = null; + $this->request->setBody($this->body); try { ob_start(); diff --git a/tests/system/Test/ControllerTestTraitTest.php b/tests/system/Test/ControllerTestTraitTest.php index 84892479fee9..1ddb5fb1c9b9 100644 --- a/tests/system/Test/ControllerTestTraitTest.php +++ b/tests/system/Test/ControllerTestTraitTest.php @@ -13,10 +13,12 @@ use App\Controllers\Home; use App\Controllers\NeverHeardOfIt; +use CodeIgniter\Controller; use CodeIgniter\Log\Logger; use CodeIgniter\Test\Mock\MockLogger as LoggerConfig; use Config\App; use Config\Services; +use Exception; use Tests\Support\Controllers\Popcorn; /** @@ -241,4 +243,19 @@ public function testRedirectRoute() ->execute('toindex'); $this->assertTrue($result->isRedirect()); } + + public function testUsesRequestBody() + { + $this->controller = new class () extends Controller { + public function throwsBody(): void { + throw new Exception($this->request->getBody()); + } + }; + $this->controller->initController($this->request, $this->response, $this->logger); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('banana'); + + $this->withBody('banana')->execute('throwsBody'); + } }