Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace doctrine subscribers with listeners #329

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}

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
Loading