fix: update configuration of TrackCreateUpdateSubscriber.php

- Update config to be conform with symfony 7.3
- Create unit test for TrackCreateUpdateSubscriber.php
This commit is contained in:
2025-09-10 13:24:50 +02:00
parent 4123aa6550
commit 3480495be1
5 changed files with 210 additions and 6 deletions

View File

@@ -2,3 +2,4 @@
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
DEFAULT_CARRIER_CODE=TEST

View File

@@ -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']

View File

@@ -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

View File

@@ -0,0 +1,192 @@
<?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\MainBundle\Tests\Doctrine\Event;
use Chill\MainBundle\Doctrine\Event\TrackCreateUpdateSubscriber;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\SecurityBundle\Security;
final class TrackCreateUpdateSubscriberTest extends TestCase
{
use ProphecyTrait;
private TrackCreateUpdateSubscriber $subscriber;
private $security;
private $entityManager;
protected function setUp(): void
{
$this->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;
}
}

View File

@@ -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\: