Simple Apitte Controllers testing.
composer require adbros/controller-tester --dev
Just register ControllerTester in config.neon.
services:
- Adbros\Tester\ControllerTester\ControllerTester
public function testPostHelloWorld(): void
{
$controllerTester = $this->container->getByType(ControllerTester::class);
$request = $controllerTester->createRequest('/api/v1/dummy/hello-world')
->withMethod('POST')
->withJsonBody([
'foo' => 'bar',
]);
$result = $controllerTester->execute($request);
$result->assertJson([
'status' => 'ok',
'payload' => [
'foo' => 'bar',
],
]);
$result->assertStatusCode(200);
}
TestControllerRequest is immutable object.
Add QUERY parameters.
Set HTTP method. Default method is GET
.
Set request RAW body.
Set request JSON body.
Set POST request with parsed body like x-www-form-urlencoded.
Add file - Psr7UploadedFile
Add HTTP headers.
Set HTTP protocol version. Default protocol version is 1.1
.
Add SERVER parameters.