-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace doctrine subscribers with listeners
- Loading branch information
1 parent
b86dea6
commit 78587bb
Showing
10 changed files
with
246 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Mods/EventListener/Doctrine/ModGroupUpdatedListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/Mods/EventSubscriber/Doctrine/ModGroupUpdatedSubscriber.php
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/Shared/EventListener/Doctrine/BlamableEntityCreatedListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Shared/EventListener/Doctrine/BlamableEntityUpdatedListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
src/Shared/EventSubscriber/Doctrine/EntityBlamableSubscriber.php
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
tests/unit/Mods/EventListener/Doctrine/ModGroupUpdatedListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
tests/unit/Shared/EventListener/Doctrine/BlamableEntityCreatedListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
tests/unit/Shared/EventListener/Doctrine/BlamableEntityUpdatedListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.