Skip to content

Commit

Permalink
Merge pull request #6843 from MGatner/controller-test-with-body
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner authored Nov 13, 2022
2 parents a9859e8 + 3f5e063 commit 5a77d11
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion system/Test/ControllerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -156,6 +156,7 @@ public function execute(string $method, ...$params)
}

$response = null;
$this->request->setBody($this->body);

try {
ob_start();
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Test/ControllerTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 5a77d11

Please sign in to comment.