diff --git a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php index f23f07610..24cd0f758 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php +++ b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php @@ -44,6 +44,9 @@ use function in_array; * uniqueConstraints={@ORM\UniqueConstraint(name="idx_calendar_remote", columns={"remoteId"}, options={"where": "remoteId <> ''"})} * ) * @ORM\Entity + * @Serializer\DiscriminatorMap(typeProperty="type", mapping={ + * "chill_calendar_calendar": Calendar::class + * }) */ class Calendar implements TrackCreationInterface, TrackUpdateInterface { @@ -109,13 +112,13 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\Embedded(class=CommentEmbeddable::class, columnPrefix="comment_") - * @Serializer\Groups({"calendar:read", "read"}) + * @Serializer\Groups({"calendar:read", "read", "docgen:read"}) */ private CommentEmbeddable $comment; /** * @ORM\Column(type="datetime_immutable", nullable=false) - * @Serializer\Groups({"calendar:read", "read", "calendar:light"}) + * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) * @Assert\NotNull(message="calendar.An end date is required") */ private ?DateTimeImmutable $endDate = null; @@ -124,7 +127,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"calendar:read", "read", "calendar:light"}) + * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) */ private ?int $id = null; @@ -136,20 +139,20 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface * cascade={"persist", "remove", "merge", "detach"} * ) * @ORM\JoinTable(name="chill_calendar.calendar_to_invites") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private Collection $invites; /** * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Location") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Assert\NotNull(message="calendar.A location is required") */ private ?Location $location = null; /** * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User") - * @Serializer\Groups({"calendar:read", "read", "calendar:light"}) + * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"}) * @Assert\NotNull(message="calendar.A main user is mandatory") */ @@ -158,7 +161,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person", inversedBy="calendars") * @ORM\JoinTable(name="chill_calendar.calendar_to_persons") - * @Serializer\Groups({"calendar:read", "read", "calendar:light"}) + * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"}) * @Assert\Count(min=1, minMessage="calendar.At least {{ limit }} person is required.") */ @@ -173,13 +176,14 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty") * @ORM\JoinTable(name="chill_calendar.calendar_to_thirdparties") - * @Serializer\Groups({"calendar:read", "read", "calendar:light"}) + * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"}) */ private Collection $professionals; /** * @ORM\Column(type="boolean", nullable=true) + * @Serializer\Groups({"docgen:read"}) */ private ?bool $sendSMS = false; @@ -190,7 +194,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\Column(type="datetime_immutable", nullable=false) - * @Serializer\Groups({"calendar:read", "read", "calendar:light"}) + * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"}) * @Assert\NotNull(message="calendar.A start date is required") */ @@ -205,6 +209,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\Column(type="boolean", nullable=true) + * @Serializer\Groups({"docgen:read"}) */ private ?bool $urgent = false; @@ -282,6 +287,9 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface return $this->comment; } + /** + * @Serializer\Groups({"docgen:read"}) + */ public function getDuration(): ?DateInterval { if ($this->getStartDate() === null || $this->getEndDate() === null) { diff --git a/src/Bundle/ChillCalendarBundle/Tests/Serializer/Normalizer/CalendarNormalizerTest.php b/src/Bundle/ChillCalendarBundle/Tests/Serializer/Normalizer/CalendarNormalizerTest.php new file mode 100644 index 000000000..2a0975206 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Tests/Serializer/Normalizer/CalendarNormalizerTest.php @@ -0,0 +1,109 @@ +normalizer = self::$container->get(NormalizerInterface::class); + } + + public function testNormalizationCalendar() + { + $calendar = (new Calendar()) + ->setComment( + $comment = new CommentEmbeddable() + ) + ->setStartDate(DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, '2020-10-15T15:00:00+0000')) + ->setEndDate(DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, '2020-15-15T15:30:00+0000')) + ->addPerson(new Person()) + ->addPerson(new Person()) + ->addUser(new User()) + ->addProfessional(new ThirdParty()); + + $expected = [ + 'type' => 'chill_calendar_calendar', + 'isNull' => false, + 'urgent' => false, + 'sendSMS' => false, + ]; + + $actual = $this->normalizer->normalize( + $calendar, + 'docgen', + ['groups' => ['docgen:read'], 'docgen:expects' => Calendar::class] + ); + + // we first check for the known key/value... + foreach ($expected as $key => $value) { + $this->assertArrayHasKey($key, $actual); + $this->assertEquals($value, $actual[$key]); + } + + // ... and then check for some other values + $this->assertArrayHasKey('persons', $actual); + $this->assertIsArray($actual['persons']); + $this->assertArrayHasKey('invites', $actual); + $this->assertIsArray($actual['invites']); + $this->assertArrayHasKey('startDate', $actual); + $this->assertIsArray($actual['startDate']); + $this->assertArrayHasKey('endDate', $actual); + $this->assertIsArray($actual['endDate']); + $this->assertArrayHasKey('professionals', $actual); + $this->assertIsArray($actual['professionals']); + $this->assertArrayHasKey('location', $actual); + $this->assertIsArray($actual['location']); + $this->assertArrayHasKey('mainUser', $actual); + $this->assertIsArray($actual['mainUser']); + $this->assertArrayHasKey('comment', $actual); + $this->assertIsArray($actual['comment']); + $this->assertArrayHasKey('duration', $actual); + $this->assertIsArray($actual['duration']); + } + + public function testNormalizationOnNullHasSameKeys() + { + $calendar = new Calendar(); + + $notNullCalendar = $this->normalizer->normalize( + $calendar, + 'docgen', + ['groups' => ['docgen:read'], 'docgen:expects' => Calendar::class] + ); + + $isNullCalendar = $this->normalizer->normalize( + null, + 'docgen', + ['groups' => ['docgen:read'], 'docgen:expects' => Calendar::class] + ); + + $this->assertEqualsCanonicalizing(array_keys($notNullCalendar), array_keys($isNullCalendar)); + } +}