diff --git a/.env.test b/.env.test index 24a43c03b..e4403cfff 100644 --- a/.env.test +++ b/.env.test @@ -2,3 +2,4 @@ KERNEL_CLASS='App\Kernel' APP_SECRET='$ecretf0rt3st' SYMFONY_DEPRECATIONS_HELPER=999999 +DEFAULT_CARRIER_CODE=TEST diff --git a/config/packages/notifier.yaml b/config/packages/notifier.yaml index 201ed6911..1d69a18e3 100644 --- a/config/packages/notifier.yaml +++ b/config/packages/notifier.yaml @@ -1,9 +1,8 @@ framework: notifier: texter_transports: - ovhcloud: '%env(OVHCLOUD_DSN)%' - #ovhcloud: '%env(OVHCLOUD_DSN)%' - #ovhcloud: '%env(SHORT_MESSAGE_DSN)%' + #ovhcloud: '%env(OVHCLOUD_DSN)%' + #ovhcloud: '%env(SHORT_MESSAGE_DSN)%' channel_policy: # use chat/slack, chat/telegram, sms/twilio or sms/nexmo urgent: ['email'] diff --git a/src/Bundle/ChillMainBundle/Doctrine/Event/TrackCreateUpdateSubscriber.php b/src/Bundle/ChillMainBundle/Doctrine/Event/TrackCreateUpdateSubscriber.php index ed630c6c8..423f0050a 100644 --- a/src/Bundle/ChillMainBundle/Doctrine/Event/TrackCreateUpdateSubscriber.php +++ b/src/Bundle/ChillMainBundle/Doctrine/Event/TrackCreateUpdateSubscriber.php @@ -21,9 +21,14 @@ use Doctrine\ORM\Events; use Doctrine\Persistence\Event\LifecycleEventArgs; use Symfony\Bundle\SecurityBundle\Security; -readonly class TrackCreateUpdateSubscriber implements EventSubscriber +class TrackCreateUpdateSubscriber implements EventSubscriber { - public function __construct(private Security $security) {} + private Security $security; + + public function __construct(Security $security) + { + $this->security = $security; + } public function getSubscribedEvents(): array { @@ -45,7 +50,7 @@ readonly class TrackCreateUpdateSubscriber implements EventSubscriber } } - $this->onUpdate($object, $args); + $this->onUpdate($object); } public function preUpdate(PreUpdateEventArgs $args): void diff --git a/src/Bundle/ChillMainBundle/Tests/Doctrine/Event/TrackCreateUpdateSubscriberTest.php b/src/Bundle/ChillMainBundle/Tests/Doctrine/Event/TrackCreateUpdateSubscriberTest.php new file mode 100644 index 000000000..babd28670 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Doctrine/Event/TrackCreateUpdateSubscriberTest.php @@ -0,0 +1,192 @@ +security = $this->prophesize(Security::class); + $this->entityManager = $this->prophesize(EntityManagerInterface::class); + $this->subscriber = new TrackCreateUpdateSubscriber($this->security->reveal()); + } + + public function testGetSubscribedEvents(): void + { + $events = $this->subscriber->getSubscribedEvents(); + + self::assertSame([Events::prePersist, Events::preUpdate], $events); + } + + public function testPrePersistSetsCreatedAtAndCreatedBy(): void + { + $user = new User(); + $user->setUsername('testuser'); + + $entity = new TestTrackCreationEntity(); + + $this->security->getUser()->willReturn($user); + + $args = new PrePersistEventArgs($entity, $this->entityManager->reveal()); + + $this->subscriber->prePersist($args); + + self::assertInstanceOf(\DateTimeImmutable::class, $entity->getCreatedAt()); + self::assertSame($user, $entity->getCreatedBy()); + self::assertInstanceOf(\DateTimeImmutable::class, $entity->getUpdatedAt()); + self::assertSame($user, $entity->getUpdatedBy()); + } + + public function testPrePersistWithoutUser(): void + { + $entity = new TestTrackCreationEntity(); + + $this->security->getUser()->willReturn(null); + + $args = new PrePersistEventArgs($entity, $this->entityManager->reveal()); + + $this->subscriber->prePersist($args); + + self::assertInstanceOf(\DateTimeImmutable::class, $entity->getCreatedAt()); + self::assertNull($entity->getCreatedBy()); + self::assertInstanceOf(\DateTimeImmutable::class, $entity->getUpdatedAt()); + self::assertNull($entity->getUpdatedBy()); + } + + public function testPrePersistWithNonTrackableEntity(): void + { + $entity = new \stdClass(); + + $args = new PrePersistEventArgs($entity, $this->entityManager->reveal()); + + // Should not throw any exception + $this->subscriber->prePersist($args); + + self::assertTrue(true); // Just to have an assertion + } + + public function testPreUpdateSetsUpdatedAtAndUpdatedBy(): void + { + $user = new User(); + $user->setUsername('testuser'); + + $entity = new TestTrackUpdateEntity(); + + $this->security->getUser()->willReturn($user); + + $changeSet = []; + $args = new PreUpdateEventArgs($entity, $this->entityManager->reveal(), $changeSet); + + $this->subscriber->preUpdate($args); + + self::assertInstanceOf(\DateTimeImmutable::class, $entity->getUpdatedAt()); + self::assertSame($user, $entity->getUpdatedBy()); + } +} + +class TestTrackCreationEntity implements TrackCreationInterface, TrackUpdateInterface +{ + private ?\DateTimeImmutable $createdAt = null; + private ?User $createdBy = null; + private ?\DateTimeImmutable $updatedAt = null; + private ?User $updatedBy = null; + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(\DateTimeInterface $datetime): self + { + $this->createdAt = $datetime instanceof \DateTime ? \DateTimeImmutable::createFromMutable($datetime) : $datetime; + return $this; + } + + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + + public function setCreatedBy(User $user): self + { + $this->createdBy = $user; + return $this; + } + + public function getUpdatedAt(): ?\DateTimeInterface + { + return $this->updatedAt; + } + + public function setUpdatedAt(\DateTimeInterface $datetime): self + { + $this->updatedAt = $datetime instanceof \DateTime ? \DateTimeImmutable::createFromMutable($datetime) : $datetime; + return $this; + } + + public function getUpdatedBy(): ?User + { + return $this->updatedBy; + } + + public function setUpdatedBy(User $user): self + { + $this->updatedBy = $user; + return $this; + } +} + +class TestTrackUpdateEntity implements TrackUpdateInterface +{ + private ?\DateTimeImmutable $updatedAt = null; + private ?User $updatedBy = null; + + public function getUpdatedAt(): ?\DateTimeInterface + { + return $this->updatedAt; + } + + public function setUpdatedAt(\DateTimeInterface $datetime): self + { + $this->updatedAt = $datetime instanceof \DateTime ? \DateTimeImmutable::createFromMutable($datetime) : $datetime; + return $this; + } + + public function getUpdatedBy(): ?User + { + return $this->updatedBy; + } + + public function setUpdatedBy(User $user): self + { + $this->updatedBy = $user; + return $this; + } +} diff --git a/src/Bundle/ChillMainBundle/config/services.yaml b/src/Bundle/ChillMainBundle/config/services.yaml index 17009a172..15ba0a635 100644 --- a/src/Bundle/ChillMainBundle/config/services.yaml +++ b/src/Bundle/ChillMainBundle/config/services.yaml @@ -29,6 +29,13 @@ services: tags: - { name: 'doctrine.event_subscriber' } + # Explicit registration for TrackCreateUpdateSubscriber + Chill\MainBundle\Doctrine\Event\TrackCreateUpdateSubscriber: + public: true + tags: + - { name: 'doctrine.event_listener', event: 'prePersist', method: 'prePersist' } + - { name: 'doctrine.event_listener', event: 'preUpdate', method: 'preUpdate' } + # workflow related Chill\MainBundle\Workflow\: