From 3f5e063cdf607b6f22294a65ce48af7974a52533 Mon Sep 17 00:00:00 2001 From: MGatner Date: Sat, 12 Nov 2022 17:02:07 +0000 Subject: [PATCH] fix: Apply body to controller under test --- system/Test/ControllerTestTrait.php | 3 ++- tests/system/Test/ControllerTestTraitTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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..de8e8fbc4777 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,20 @@ 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'); + } }