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

  • I had the issue even though i had monolog installed. What worked for me was the following.

    Before fix.

    $response = $client->request('PATCH', '/users/' . $user->getId(), [
        'json' => [
            'email' => 'invalid-email'
        ]
    ]);
    
    // Assert validation error response (422 Unprocessable Entity)
    $this->assertResponseStatusCodeSame(422);
    
    $data = $response->toArray();
    

    after fix

    $response = $client->request('PATCH', '/users/' . $user->getId(), [
        'json' => [
            'email' => 'invalid-email'
        ]
    ]);
    
    // Assert validation error response (422 Unprocessable Entity)
    $this->assertResponseStatusCodeSame(422);
    
    $data = $response->toArray(false);
    

    In my case the exception was coming from the toArray method.

    Inside the API platform bundle test code there is a check for the status code. So You can expect by the exceptions api platform is throwing or the status code like in my case.

    To skip this status check pass false to the toArray method.

    below is the method that check the status code in api platform.

        /**
         * Checks the status, and try to extract message if appropriate.
         */
        private function checkStatusCode(): void
        {
            if (500 <= $this->info['http_code']) {
                throw new ServerException($this);
            }
    
            if (400 <= $this->info['http_code']) {
                throw new ClientException($this);
            }
    
            if (300 <= $this->info['http_code']) {
                throw new RedirectionException($this);
            }
        }