Functional Test For POST Request Not Working After Adding denormalization_context groups
/**
* @ApiResource(
* collectionOperations={
* "register"={
* "method"="POST",
* "path"="/candidates/register",
* "denormalization_context"={"groups"={"user:register"}},
* "normalization_context"={"groups"={"user:get"}}
* }
* },
* itemOperations={
* "get"
* }
* )
* Class Candidate
* @package App\Entity
* @ORM\Entity
*/
class Candidate extends User
{
public function getRoles()
{
return ['ROLE_CANDIDATE'];
}
}
this is my User class :
/**
* @ApiResource(
* collectionOperations={
* "get",
* }
* )
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"advertiser"="App\Entity\Advertiser", "candidate"="App\Entity\Candidate"})
* @UniqueEntity(fields="email", entityClass="App\Entity\User")
*/
abstract class User implements UserInterface
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
protected UuidInterface $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:register", "user:get"})
* @Assert\NotBlank
*/
protected string $firstName = "";
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:register", "user:get"})
* @Assert\NotBlank
*/
protected string $lastName = "";
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Groups({"user:register", "user:get"})
* @Assert\NotBlank
* @Assert\Email
*/
protected string $email = "";
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:register"})
* @Assert\NotBlank
* @Assert\Regex(pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{6,}$/")
*/
protected string $password = "";
/**
* @Groups({"user:register"})
* @Assert\NotBlank
* @Assert\EqualTo(propertyPath="password")
*/
protected string $confirmPassword = "";
/**
* @ORM\Column(type="boolean")
*/
protected bool $isActivated;
/**
* @ORM\Embedded(class="UserToken", columnPrefix=false)
* @Groups({"user:register"})
*/
protected ?UserToken $userToken;
and this is my functional test :
class UserRegisterApiTest extends ApiTestCase
{
public function testRegisterCandidates(): void
{
$client = static::createClient()->request('POST', '/api/candidates/register', [
'headers' => ['Content-Type' => 'application/json'],
'json' => [
'firstName' => 'test',
'lastName' => 'test',
'email' => '[email protected]',
'password' => 'TestTest20@',
'confirmPassword' => 'TestTest20@'
]
]);
$this->assertResponseStatusCodeSame(201);
}
}
and i get ConstraintViolation NotBlank Error , that seems that denormalization_context is not working, but it works with POSTMAN .
Any help please ?
You really should avoid inheritance. Then, what's the property that is concerned by the validation error?
Hi @soyuka thank you for response, why i should avoid inheritance ?
For the errors i have all the validation errors of all properties.
For the errors i have all the validation errors of all properties.
Then the request is malformed, check the symfony profiler to help you debug why.
why i should avoid inheritance ?
Because it leads to weird bugs and overall increased complexity for not much gain. There are lots of issues on github regarding this subject.
Yes but with POSTMAN it works, its seems like when i add the denormlization_context, the apiTest client doesn't read the properties decorated by the group "user:register"
Works when i delete the test cache folder /var/cache/test