Skip to content

Commit

Permalink
Ensure Kernel is shutdown - prevent error for Symfony 5 (#41)
Browse files Browse the repository at this point in the history
* ensure kernel is shutdown - prevent error for Symfony5

* Allow prevention of errors on Symfony 5 in WebTestCase

---------

Co-authored-by: Hugo Gonçalves <[email protected]>
  • Loading branch information
1 parent 6c7c4ff commit a36bd8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ public function withServerParameter(string $parameterName, string $parameterValu
This bundle exposes the [WebTestCase](https://github.com/kununu/testing-bundle/blob/master/src/Test/WebTestCase.php) that you can extend which exposes a method that helps you testing your controllers without having to care about create the kernel. This class also allows you load fixtures in your tests.
```php
protected function doRequest(RequestBuilder $builder): Symfony\Component\HttpFoundation\Response
final protected function doRequest(RequestBuilder $builder, bool $shutdown = true): Symfony\Component\HttpFoundation\Response
```
The `shutdown` parameter, if set to true (default), will ensure that Symfony kernel is shutdown after the request, which is required to avoid exceptions on Symfony 5 and up.

Internally this method calls the Symfony client with:

```php
Expand All @@ -146,7 +148,7 @@ $client->request($builder->method, $builder->uri, $builder->parameters, $builder

## Example

Lets imagine that you have a route named *company_create* which is protected (A valid access token needs to be provided) and expects a json to be provided in the body of the request with the data required to create a new company.
Let's imagine that you have a route named *company_create* which is protected (A valid access token needs to be provided) and expects a json to be provided in the body of the request with the data required to create a new company.

```yaml
# routes.yaml
Expand Down
6 changes: 5 additions & 1 deletion src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class WebTestCase extends FixturesAwareTestCase
/** @var KernelBrowser */
private $client;

final protected function doRequest(RequestBuilder $builder): Response
final protected function doRequest(RequestBuilder $builder, bool $shutdown = true): Response
{
$this->initClient();

Expand All @@ -29,6 +29,10 @@ final protected function doRequest(RequestBuilder $builder): Response
);
}

if ($shutdown) {
$this->ensureKernelShutdown();
}

return $response;
}

Expand Down

0 comments on commit a36bd8a

Please sign in to comment.