Search code examples
symfonyphpunitapi-platform.com

Symfony disable exception output in ApiPlatform ApiTestCase


I'm trying to write API test for controller in php Symfony framework using the ApiPlatform\Symfony\Bundle\Test\ApiTestCase, but when I throw an Exception (constraint violation) in the controller, I get the following output in the console:

...[error] Uncaught PHP Exception ApiPlatform\Symfony\Validator\Exception\ValidationException: "Exception message" at /srv/src/Repository/MyRepository line 244
string(0) ""
.......                                                        10 / 10 (100%)

which as you can see, breaks the dotted lines and writes the error message.

The problem is only visual, but it still bothers me as it will make the test output unreadable when more tests like this are done.

My code:

 $response = $this->client->request('POST', '/api/test', [
            'headers' => [
                'Authorization' => $this->token,
            ],
            'json' => [
                'data1' => 1,
                'data2' => 1
            ],
        ]);


        $this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
        $this->assertEquals('Exception message', $response->toArray(false)['violations'][0]['message']);

Code that throws the exception:


            $violation = new ConstraintViolation('Exception message', '', [], '', '', '');
            throw new ValidationException((new ConstraintViolationList([$violation])));
        

The tests go on normally, the response is correct, the HTTP code is also alright, but this output to the console shouldn't be there in my opinion.

Can I disable it somehow?


Solution

  • Installing monolog and configuring it so it dumps the errors into log file solved the issue.