postLoad eventListener adds Game object to GameAware entities.

This commit is contained in:
Vassyli
2018-01-05 16:30:59 +01:00
parent 9ddd16b4e8
commit aba0d53a68
7 changed files with 154 additions and 11 deletions
+8 -4
View File
@@ -3,6 +3,8 @@ declare(strict_types=1);
namespace LotGD\Core;
use Doctrine\Common\EventManager as DoctrineEventManager;
use Doctrine\ORM\Events as DoctrineEvents;
use Doctrine\ORM\ {
EntityManager,
EntityManagerInterface,
@@ -17,10 +19,8 @@ use Monolog\ {
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Application;
use LotGD\Core\ {
ComposerManager,
BootstrapInterface,
Exceptions\InvalidConfigurationException
use LotGD\Core\{
ComposerManager, BootstrapInterface, Doctrine\EntityPostLoadEventListener, Exceptions\InvalidConfigurationException
};
/**
@@ -71,6 +71,10 @@ class Bootstrap
->withCwd($cwd)
->create();
// Add Event listener to entity manager
$dem = $entityManager->getEventManager();
$dem->addEventListener([DoctrineEvents::postLoad], new EntityPostLoadEventListener($this->game));
return $this->game;
}
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Doctrine;
use Doctrine\Common\Util\Debug;
use Doctrine\ORM\Event\LifecycleEventArgs;
use LotGD\Core\Game;
use LotGD\Core\GameAwareInterface;
/**
* Class EntityPostLoadEventListener
* @package LotGD\Core\Doctrine
*/
class EntityPostLoadEventListener
{
/** @var Game $game */
private $game;
public function __construct(Game $g)
{
$this->game = $g;
}
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof GameAwareInterface) {
$entity->setGame($this->game);
}
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace LotGD\Core;
/**
* Interface for classes that are aware of the game
* @package LotGD\Core
*/
interface GameAwareInterface
{
public function setGame(Game $g);
}
+4 -6
View File
@@ -11,14 +11,11 @@ use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use LotGD\Core\{
BuffList,
Game
BuffList, Game, GameAwareInterface
};
use LotGD\Core\Tools\Exceptions\BuffSlotOccupiedException;
use LotGD\Core\Tools\Model\{
Creator,
PropertyManager,
SoftDeletable
Creator, GameAware, PropertyManager, SoftDeletable
};
/**
@@ -27,11 +24,12 @@ use LotGD\Core\Tools\Model\{
* @Entity(repositoryClass="LotGD\Core\Models\Repositories\CharacterRepository")
* @Table(name="characters")
*/
class Character implements CharacterInterface, CreateableInterface
class Character implements CharacterInterface, CreateableInterface, GameAwareInterface
{
use Creator;
use SoftDeletable;
use PropertyManager;
use GameAware;
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
+24
View File
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tools\Model;
use LotGD\Core\Game;
/**
* Helper trait to implement public setGame from GameAwareInterface and private getGame for internal use.
* @package LotGD\Core\Tools\Model
*/
trait GameAware
{
private $game;
public function setGame(Game $game) {
$this->game = $game;
}
private function getGame(): Game {
return $this->game;
}
}
+62
View File
@@ -89,4 +89,66 @@ class BootstrapTest extends \PHPUnit_Framework_TestCase
$this->assertInternalType("string", $user->getName());
$this->assertSame("Monthy", $user->getName());
}
public function testIfGameAwareEntitiesHaveAGAmeInstanceAssociatedAfterLoading()
{
$installationManager = $this->getMockBuilder(InstallationManager::class)
->disableOriginalConstructor()
->setMethods(["getInstallPath"])
->getMock();
$installationManager->method("getInstallPath")->willReturn(__DIR__ . "/FakeModule");
$composer = $this->getMockBuilder(\Composer\Composer::class)
->disableOriginalConstructor()
->setMethods(["getInstallationManager"])
->getMock();
$composer->method("getInstallationManager")->willReturn($installationManager);
$fakeModulePackage = $this->getMockBuilder(AliasPackage::class)
->disableOriginalConstructor()
->setMethods(["getType", "getAutoload"])
->getMock();
$fakeModulePackage->method("getType")->willReturn("lotgd-module");
$fakeModulePackage->method("getAutoload")->willReturn([
"psr-4" => [
"LotGD\\Core\\Tests\\FakeModule\\" => "FakeModule/"
]
]);
$composerManager = $this->getMockBuilder(ComposerManager::class)
->disableOriginalConstructor()
->setMethods(["getPackages", "getComposer", "translateNamespaceToPath"])
->getMock();
$composerManager->method("getPackages")->willReturn([$fakeModulePackage]);
$composerManager->method("getComposer")->willReturn($composer);
$composerManager
->expects($this->exactly(1))
->method("translateNamespaceToPath")
->with("LotGD\\Core\\Tests\\FakeModule\\Models\\")
->willReturn(__DIR__ . "/FakeModule/Models");
$bootstrap = $this->getMockBuilder(Bootstrap::class)
->setMethods(["createComposerManager"])
->getMock();
$bootstrap->method("createComposerManager")->willReturn($composerManager);
// run tests
$game = $bootstrap->getGame(implode(DIRECTORY_SEPARATOR, [__DIR__, '..']));
// A freshly created user entity should not rely
$user = new UserEntity();
$user->setName("Testus");
$user->setGame($game);
$this->assertSame($game, $user->returnGame());
$game->getEntityManager()->persist($user);
$game->getEntityManager()->flush();
$id = $user->getId();
$this->assertInternalType("int", $id);
$game->getEntityManager()->clear();
$user = $game->getEntityManager()->getRepository(UserEntity::class)->find($id);
$this->assertSame($game, $user->returnGame());
}
}
+11 -1
View File
@@ -5,13 +5,18 @@ namespace LotGD\Core\Tests\FakeModule\Models;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use LotGD\Core\Game;
use LotGD\Core\GameAwareInterface;
use LotGD\Core\Tools\Model\GameAware;
/**
* @Entity
* @Table(name="Users")
*/
class UserEntity
class UserEntity implements GameAwareInterface
{
use GameAware;
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/** @Column(type="string", length=50); */
@@ -31,4 +36,9 @@ class UserEntity
{
$this->name = $name;
}
public function returnGame(): Game
{
return $this->getGame();
}
}