Symfony voter – POST opreration sends null object

I’m working with Symfony5, ApiPlatform and phpunit

Here is my entity POST rule with annotation :

 *        "post"={
 *              "method"="POST",
 *              "validation_groups"={"create"},
 *              "security"="is_granted('POST', object)",
 *              "normalization_context"={"groups"={"entityname:write"}},
 *        }

I’m running a test on a POST operation:

$body = '{
  "debut": "2020-11-17T14:03:10.208Z",
  "fin": "2020-11-17T14:03:10.208Z"
}';

$res = $this->buildPostPutRequest(
  Actions::POST,
  GenericRoutes::ROUTE_NAME,
  $body,
  GenericCredentials::USER_CREDENTIALS
);

$this->assertResponseStatusCodeSame(403);

And here is my voter:

protected function supports($attribute, $subject)
{
    return parent::supports($attribute, $subject)
        && ($subject instanceof AppEntityEntityType || 
        $this->arrayOf($subject, AppEntityEntityType::class));
}

protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
  // Some code
}

Now I do receive a 403 but for the wrong reasons.

The test do not even enter the voteOnAttribute function, because the supports function return false.

If I dump the $attribute and $subjet var I receive in supports, I get :

'POST'
null

when I should receive:

'POST'
EntityType

Does anyone have any idea on why I receive null instead of the entity type ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *