0
votes

I'm setting up phpunit for testing, and started with the Controller routes. When I test the 'GET' methods, they work fine. However, the 'DELETE' methods throw an MethodNotAllowedHttpException. NOTE: I'm not testing adding/removing database entities yet, just validating routes.

* @Route("/deviceprofile/{deviceprofile_id}/delete",
*     name="deviceprofile_delete",
*     methods={"DELETE"})

The test class:

class DeviceProfileControllerTest extends WebTestCase
{
    public function testRouteDelete()
    {
        $client = static::createClient();
        $client->followRedirects(true);
        $client->request('DELETE', '/deviceprofile/1/delete');
        $this->assertResponseIsSuccessful();
    }
}

The phpunit error:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: No route found for "GET /1/delete": Method Not Allowed (Allow: DELETE)

Based on the error, it appears that my $client->request is only doing 'GETs' regardless of the method listed.

So, what function should I be calling to test a 'DELETE' method?

EDIT: here is the bin/console debug:router listing:

deviceprofile_delete    DELETE    ANY    ANY 
   /deviceprofile/{deviceprofile_id}/delete

UPDATE: it also appears that 'POST' method only routes also fail.

1
There shouldn't be any difference between testing DELETE or any other method. Try using php bin/console debug:router to debug routes. - Leprechaun
That's a good suggestion. Edited the question - profm2
The issue isn't the route itself, the route works great. The problem is that when calling the WebTestCase::createClient()->request, it doesn't appear to like 'DELETE' as a method. As the phpunit error suggests, it's still trying to do a 'GET' for that route, not a 'DELETE' - profm2
Maybe it's a cache issue? Try clearing cache for with --env=test - Leprechaun
Cleared the cache, phpunit still spits out: No route found for "GET /deviceprofile/1/delete": Method Not Allowed (Allow: DELETE) The request SHOULD be: "DELETE /deviceprofile/1/delete" - profm2

1 Answers

0
votes

Without the $client->followRedirects(true);, phpunit fails assertions of 301 redirects.

And with those problems now visible, and it showed that I was not properly authenticating.

Ended up finding this document that helped: https://symfony.com/doc/4.4/testing/http_authentication.html