Skip to content

Commit 3f5e063

Browse files
committed
fix: Apply body to controller under test
1 parent a9859e8 commit 3f5e063

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

system/Test/ControllerTestTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected function setUpControllerTestTrait(): void
108108
$tempUri = Services::uri();
109109
Services::injectMock('uri', $this->uri);
110110

111-
$this->withRequest(Services::request($this->appConfig, false)->setBody($this->body));
111+
$this->withRequest(Services::request($this->appConfig, false));
112112

113113
// Restore the URI service
114114
Services::injectMock('uri', $tempUri);
@@ -156,6 +156,7 @@ public function execute(string $method, ...$params)
156156
}
157157

158158
$response = null;
159+
$this->request->setBody($this->body);
159160

160161
try {
161162
ob_start();

tests/system/Test/ControllerTestTraitTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use App\Controllers\Home;
1515
use App\Controllers\NeverHeardOfIt;
16+
use CodeIgniter\Controller;
1617
use CodeIgniter\Log\Logger;
1718
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
1819
use Config\App;
1920
use Config\Services;
21+
use Exception;
2022
use Tests\Support\Controllers\Popcorn;
2123

2224
/**
@@ -241,4 +243,20 @@ public function testRedirectRoute()
241243
->execute('toindex');
242244
$this->assertTrue($result->isRedirect());
243245
}
246+
247+
public function testUsesRequestBody()
248+
{
249+
$this->controller = new class () extends Controller {
250+
public function throwsBody(): void
251+
{
252+
throw new Exception($this->request->getBody());
253+
}
254+
};
255+
$this->controller->initController($this->request, $this->response, $this->logger);
256+
257+
$this->expectException(Exception::class);
258+
$this->expectExceptionMessage('banana');
259+
260+
$this->withBody('banana')->execute('throwsBody');
261+
}
244262
}

0 commit comments

Comments
 (0)