Skip to content

Commit

Permalink
Replace doctrine subscribers with listeners (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
jskowronski39 authored Jun 27, 2024
2 parents b86dea6 + 78587bb commit 9568cff
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 220 deletions.
1 change: 0 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ services:
arguments:
- '%env(DATABASE_URL)%'

# Register Doctrine subscribers
App\Shared\Service\Mission\MissionClient:
arguments:
$missionApiUrl: '%env(APP_URL_API_MISSION)%'
Expand Down
31 changes: 31 additions & 0 deletions src/Mods/EventListener/Doctrine/ModGroupUpdatedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Mods\EventListener\Doctrine;

use App\Mods\Entity\ModGroup\ModGroup;
use App\Mods\Service\ModListUpdateService\ModListUpdateServiceInterface;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;

#[AsEntityListener(event: Events::preUpdate, method: 'preUpdate', entity: ModGroup::class)]
class ModGroupUpdatedListener
{
public function __construct(
private EntityManagerInterface $entityManager,
private ModListUpdateServiceInterface $modListUpdateService,
) {
}

public function preUpdate(ModGroup $modGroup): void
{
// Do nothing if updated entity is not a Mod Group or no changes were made to the entity
if (!$this->entityManager->getUnitOfWork()->getEntityChangeSet($modGroup)) {
return;
}

$this->modListUpdateService->updateModListsAssociatedWithModGroup($modGroup);
}
}
42 changes: 0 additions & 42 deletions src/Mods/EventSubscriber/Doctrine/ModGroupUpdatedSubscriber.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Shared\EventListener\Doctrine;

use App\Shared\Entity\Common\AbstractBlamableEntity;
use App\Users\Entity\User\User;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Symfony\Bundle\SecurityBundle\Security;

#[AsEntityListener(event: Events::prePersist, method: 'prePersist', entity: AbstractBlamableEntity::class)]
class BlamableEntityCreatedListener
{
public function __construct(
private Security $security
) {
}

public function prePersist(AbstractBlamableEntity $entity): void
{
$currentUser = $this->security->getUser();
if ($currentUser instanceof User) {
$entity->created($currentUser);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Shared\EventListener\Doctrine;

use App\Shared\Entity\Common\AbstractBlamableEntity;
use App\Users\Entity\User\User;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Symfony\Bundle\SecurityBundle\Security;

#[AsEntityListener(event: Events::preUpdate, method: 'preUpdate', entity: AbstractBlamableEntity::class)]
class BlamableEntityUpdatedListener
{
public function __construct(
private Security $security
) {
}

public function preUpdate(AbstractBlamableEntity $entity): void
{
$currentUser = $this->security->getUser();
if ($currentUser instanceof User) {
$entity->updated($currentUser);
}
}
}
51 changes: 0 additions & 51 deletions src/Shared/EventSubscriber/Doctrine/EntityBlamableSubscriber.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace App\Tests\Unit\Mods\EventListener\Doctrine;

use App\Mods\Entity\ModGroup\ModGroup;
use App\Mods\EventListener\Doctrine\ModGroupUpdatedListener;
use App\Mods\Service\ModListUpdateService\ModListUpdateServiceInterface;
use Codeception\Test\Unit;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;

class ModGroupUpdatedListenerTest extends Unit
{
public function testMarkAssociatedModListsAsUpdated(): void
{
$changeSet = [
'name' => [
'old name',
'new name',
],
];

$modGroup = $this->createMock(ModGroup::class);
$entityManager = $this->createEntityManagerMock($changeSet);

$modListUpdateService = $this->createMock(ModListUpdateServiceInterface::class);
$modListUpdateService
->expects(self::once())
->method('updateModListsAssociatedWithModGroup')
->with($modGroup)
;

$modGroupUpdatedListener = new ModGroupUpdatedListener($entityManager, $modListUpdateService);
$modGroupUpdatedListener->preUpdate($modGroup);
}

public function testDoNotMarkAssociatedModListsAsUpdatedIfThereAreNoChangesToEntity(): void
{
$changeSet = [];

$modGroup = $this->createMock(ModGroup::class);
$entityManager = $this->createEntityManagerMock($changeSet);

$modListUpdateService = $this->createMock(ModListUpdateServiceInterface::class);
$modListUpdateService
->expects(self::never())
->method('updateModListsAssociatedWithModGroup')
->with($modGroup)
;

$modGroupUpdatedListener = new ModGroupUpdatedListener($entityManager, $modListUpdateService);
$modGroupUpdatedListener->preUpdate($modGroup);
}

private function createEntityManagerMock(array $changeSet): EntityManagerInterface
{
$unitOfWork = $this->createMock(UnitOfWork::class);
$unitOfWork->method('getEntityChangeSet')->willReturn($changeSet);

$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getUnitOfWork')->willReturn($unitOfWork);

return $entityManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Tests\Unit\Shared\EventListener\Doctrine;

use App\Shared\Entity\Common\AbstractBlamableEntity;
use App\Shared\EventListener\Doctrine\BlamableEntityCreatedListener;
use App\Users\Entity\User\User;
use Codeception\Test\Unit;
use Symfony\Bundle\SecurityBundle\Security;

class BlamableEntityCreatedListenerTest extends Unit
{
public function testMarkEntityAsCreated(): void
{
$user = $this->createMock(User::class);
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn($user);

$entity = $this->createMock(AbstractBlamableEntity::class);
$entity
->expects(self::once())
->method('created')
->with(self::isInstanceOf(User::class))
;

$blamableEntityCreatedListener = new BlamableEntityCreatedListener($security);
$blamableEntityCreatedListener->prePersist($entity);
}

public function testDoNotMarkEntityAsCreated(): void
{
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn(null);

$entity = $this->createMock(AbstractBlamableEntity::class);
$entity
->expects(self::never())
->method('created')
;

$blamableEntityCreatedListener = new BlamableEntityCreatedListener($security);
$blamableEntityCreatedListener->prePersist($entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Tests\Unit\Shared\EventListener\Doctrine;

use App\Shared\Entity\Common\AbstractBlamableEntity;
use App\Shared\EventListener\Doctrine\BlamableEntityUpdatedListener;
use App\Users\Entity\User\User;
use Codeception\Test\Unit;
use Symfony\Bundle\SecurityBundle\Security;

class BlamableEntityUpdatedListenerTest extends Unit
{
public function testMarkEntityAsUpdated(): void
{
$user = $this->createMock(User::class);
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn($user);

$entity = $this->createMock(AbstractBlamableEntity::class);
$entity
->expects(self::once())
->method('updated')
->with(self::isInstanceOf(User::class))
;

$blamableEntityUpdatedListener = new BlamableEntityUpdatedListener($security);
$blamableEntityUpdatedListener->preUpdate($entity);
}

public function testDoNotMarkEntityAsUpdated(): void
{
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn(null);

$entity = $this->createMock(AbstractBlamableEntity::class);
$entity
->expects(self::never())
->method('updated')
;

$blamableEntityUpdatedListener = new BlamableEntityUpdatedListener($security);
$blamableEntityUpdatedListener->preUpdate($entity);
}
}
Loading

0 comments on commit 9568cff

Please sign in to comment.