DX: [calendar] docgen normalization on Calendar entity

This commit is contained in:
Julien Fastré 2022-10-19 22:02:43 +02:00
parent 7e65e97caf
commit b580bda2fb
2 changed files with 126 additions and 9 deletions

View File

@ -44,6 +44,9 @@ use function in_array;
* uniqueConstraints={@ORM\UniqueConstraint(name="idx_calendar_remote", columns={"remoteId"}, options={"where": "remoteId <> ''"})} * uniqueConstraints={@ORM\UniqueConstraint(name="idx_calendar_remote", columns={"remoteId"}, options={"where": "remoteId <> ''"})}
* ) * )
* @ORM\Entity * @ORM\Entity
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
* "chill_calendar_calendar": Calendar::class
* })
*/ */
class Calendar implements TrackCreationInterface, TrackUpdateInterface class Calendar implements TrackCreationInterface, TrackUpdateInterface
{ {
@ -109,13 +112,13 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
/** /**
* @ORM\Embedded(class=CommentEmbeddable::class, columnPrefix="comment_") * @ORM\Embedded(class=CommentEmbeddable::class, columnPrefix="comment_")
* @Serializer\Groups({"calendar:read", "read"}) * @Serializer\Groups({"calendar:read", "read", "docgen:read"})
*/ */
private CommentEmbeddable $comment; private CommentEmbeddable $comment;
/** /**
* @ORM\Column(type="datetime_immutable", nullable=false) * @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") * @Assert\NotNull(message="calendar.An end date is required")
*/ */
private ?DateTimeImmutable $endDate = null; private ?DateTimeImmutable $endDate = null;
@ -124,7 +127,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
* @Serializer\Groups({"calendar:read", "read", "calendar:light"}) * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"})
*/ */
private ?int $id = null; private ?int $id = null;
@ -136,20 +139,20 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
* cascade={"persist", "remove", "merge", "detach"} * cascade={"persist", "remove", "merge", "detach"}
* ) * )
* @ORM\JoinTable(name="chill_calendar.calendar_to_invites") * @ORM\JoinTable(name="chill_calendar.calendar_to_invites")
* @Serializer\Groups({"read"}) * @Serializer\Groups({"read", "docgen:read"})
*/ */
private Collection $invites; private Collection $invites;
/** /**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Location") * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Location")
* @Serializer\Groups({"read"}) * @Serializer\Groups({"read", "docgen:read"})
* @Assert\NotNull(message="calendar.A location is required") * @Assert\NotNull(message="calendar.A location is required")
*/ */
private ?Location $location = null; private ?Location $location = null;
/** /**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User") * @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"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
* @Assert\NotNull(message="calendar.A main user is mandatory") * @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\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person", inversedBy="calendars")
* @ORM\JoinTable(name="chill_calendar.calendar_to_persons") * @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"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
* @Assert\Count(min=1, minMessage="calendar.At least {{ limit }} person is required.") * @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\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty")
* @ORM\JoinTable(name="chill_calendar.calendar_to_thirdparties") * @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"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
*/ */
private Collection $professionals; private Collection $professionals;
/** /**
* @ORM\Column(type="boolean", nullable=true) * @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"docgen:read"})
*/ */
private ?bool $sendSMS = false; private ?bool $sendSMS = false;
@ -190,7 +194,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
/** /**
* @ORM\Column(type="datetime_immutable", nullable=false) * @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"}) * @Serializer\Context(normalizationContext={"read"}, groups={"calendar:light"})
* @Assert\NotNull(message="calendar.A start date is required") * @Assert\NotNull(message="calendar.A start date is required")
*/ */
@ -205,6 +209,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
/** /**
* @ORM\Column(type="boolean", nullable=true) * @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"docgen:read"})
*/ */
private ?bool $urgent = false; private ?bool $urgent = false;
@ -282,6 +287,9 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
return $this->comment; return $this->comment;
} }
/**
* @Serializer\Groups({"docgen:read"})
*/
public function getDuration(): ?DateInterval public function getDuration(): ?DateInterval
{ {
if ($this->getStartDate() === null || $this->getEndDate() === null) { if ($this->getStartDate() === null || $this->getEndDate() === null) {

View File

@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\CalendarBundle\Tests\Serializer\Normalizer;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateTimeImmutable;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
* @coversNothing
*/
final class CalendarNormalizerTest extends KernelTestCase
{
private NormalizerInterface $normalizer;
protected function setUp(): void
{
self::bootKernel();
$this->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));
}
}