mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-29 10:05:03 +00:00
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:
@@ -2,3 +2,4 @@
|
|||||||
KERNEL_CLASS='App\Kernel'
|
KERNEL_CLASS='App\Kernel'
|
||||||
APP_SECRET='$ecretf0rt3st'
|
APP_SECRET='$ecretf0rt3st'
|
||||||
SYMFONY_DEPRECATIONS_HELPER=999999
|
SYMFONY_DEPRECATIONS_HELPER=999999
|
||||||
|
DEFAULT_CARRIER_CODE=TEST
|
||||||
|
@@ -1,9 +1,8 @@
|
|||||||
framework:
|
framework:
|
||||||
notifier:
|
notifier:
|
||||||
texter_transports:
|
texter_transports:
|
||||||
ovhcloud: '%env(OVHCLOUD_DSN)%'
|
#ovhcloud: '%env(OVHCLOUD_DSN)%'
|
||||||
#ovhcloud: '%env(OVHCLOUD_DSN)%'
|
#ovhcloud: '%env(SHORT_MESSAGE_DSN)%'
|
||||||
#ovhcloud: '%env(SHORT_MESSAGE_DSN)%'
|
|
||||||
channel_policy:
|
channel_policy:
|
||||||
# use chat/slack, chat/telegram, sms/twilio or sms/nexmo
|
# use chat/slack, chat/telegram, sms/twilio or sms/nexmo
|
||||||
urgent: ['email']
|
urgent: ['email']
|
||||||
|
@@ -21,9 +21,14 @@ use Doctrine\ORM\Events;
|
|||||||
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
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
|
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
|
public function preUpdate(PreUpdateEventArgs $args): void
|
||||||
|
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
@@ -29,6 +29,13 @@ services:
|
|||||||
tags:
|
tags:
|
||||||
- { name: 'doctrine.event_subscriber' }
|
- { 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
|
# workflow related
|
||||||
Chill\MainBundle\Workflow\:
|
Chill\MainBundle\Workflow\:
|
||||||
|
Reference in New Issue
Block a user