Testing Controllers in Symfony 4.4 while using annotations

I’m facing a problem of testing my Controllers in Symfony 4.4 with FOSRestBundle & JMSSserializer. My controllers are pretty simple, usually containing nothing but call to another services, but I’m using ParamConverter, Serializer, Deserializer etc. I’m never sure if fields returned are the ones I expect.

I want to test how serialization/deserialization is handling my entities. Whenever I add a field in my entities, or change field groups, tests should fail.

Ideally, I would mock my services and call Action directly, but I can’t find anywhere, how can I call an Action method all annotations firing.

Is there a way to test other than functional testing whole requests?

Controller action I want to test:

     * @RestPost("/entity")
     * @RestPut("/entity/{entityId<d+>?}")
     * @ParamConverter(name="entity", converter="app.request_body",options={
     *         "deserializationContext"={"groups"={
     *             "DetailsGroup",
     *             "nested"={"IdGroup"},
     *             "owner"={"IdGroup"}
     *         }}
     *     }
     * )
     * @RestView(serializerGroups={"IdGroup"}, statusCode=Response::HTTP_CREATED)
     * @param int|null $entityId
     * @param Entity $entity
     * @param ConstraintViolationListInterface $validationErrors
     * @return Entity
     * @throws InvalidArgumentException
     * @throws Exception
    public function setEntityAction(?int $entityId, Entity $entity, ConstraintViolationListInterface $validationErrors): Entity
        if ($validationErrors->count() > 0) {
            throw new InvalidArgumentException('...');

        return $this->entityService->setEntity($entity, $this->getUser());

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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