-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from swagindustries/feature/support-proxy
Tests update + add support for Doctrine proxy
- Loading branch information
Showing
30 changed files
with
309 additions
and
165 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
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
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
37 changes: 37 additions & 0 deletions
37
Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.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,37 @@ | ||
<?php | ||
|
||
namespace Biig\Component\Domain\Tests\Model\Instantiator; | ||
|
||
use Biig\Component\Domain\Event\DomainEventDispatcher; | ||
use Biig\Component\Domain\Tests\fixtures\Entity\FakeModel; | ||
use Biig\Component\Domain\Tests\SetupDatabaseTrait; | ||
use Doctrine\Persistence\Proxy; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PostLoadDispatcherInjectionListenerTest extends TestCase | ||
{ | ||
use SetupDatabaseTrait; | ||
|
||
public function testItInjectDispatcherOnStandardEntity() | ||
{ | ||
$dispatcher = new DomainEventDispatcher(); | ||
$entityManager = $this->setupDatabase($dispatcher, 'testPostLoadDispatcherInjection'); | ||
$entity = $entityManager->getRepository(FakeModel::class)->find(1); | ||
|
||
$this->assertTrue($entity->hasDispatcher()); | ||
$this->dropDatabase(); | ||
} | ||
|
||
public function testItLoadsDispatcherInProxyEntity() | ||
{ | ||
$dispatcher = new DomainEventDispatcher(); | ||
$entityManager = $this->setupDatabase($dispatcher, 'testItLoadsDispatcherInProxyEntity'); | ||
$entity = $entityManager->getRepository(FakeModel::class)->find(1); | ||
|
||
$related = $entity->getRelated(); | ||
$this->assertInstanceOf(Proxy::class, $related); | ||
$this->assertTrue($related->hasDispatcher(), 'The given proxy has no dispatcher yet'); | ||
|
||
$this->dropDatabase(); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
namespace Biig\Component\Domain\Tests; | ||
|
||
use Biig\Component\Domain\Event\DomainEventDispatcherInterface; | ||
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\PostLoadDispatcherInjectionListener; | ||
use Biig\Component\Domain\PostPersistListener\DoctrinePostPersistListener; | ||
use Doctrine\ORM\EntityManager; | ||
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory; | ||
use Doctrine\ORM\ORMSetup; | ||
|
||
trait SetupDatabaseTrait | ||
{ | ||
private $dbPath; | ||
|
||
private function setupDatabase(DomainEventDispatcherInterface $dispatcher, string $name): EntityManager | ||
{ | ||
$this->dbPath = \sys_get_temp_dir() . '/'.$name.'.' . \microtime() . '.sqlite'; | ||
copy(__DIR__ . '/fixtures/dbtest/initial_fake_model.db', $this->dbPath); | ||
|
||
$config = ORMSetup::createAttributeMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true); | ||
$config->setClassMetadataFactoryName(ClassMetadataFactory::class); | ||
$conn = [ | ||
'driver' => 'pdo_sqlite', | ||
'path' => $this->dbPath, | ||
]; | ||
|
||
$entityManager = EntityManager::create($conn, $config); | ||
$entityManager->getEventManager()->addEventSubscriber(new DoctrinePostPersistListener($dispatcher)); | ||
$entityManager->getEventManager()->addEventSubscriber(new PostLoadDispatcherInjectionListener($dispatcher)); | ||
|
||
$entityManager->getMetadataFactory()->setDispatcher($dispatcher); | ||
|
||
return $entityManager; | ||
} | ||
|
||
private function dropDatabase() | ||
{ | ||
if (!$this->dbPath) { | ||
return; | ||
} | ||
|
||
@unlink($this->dbPath); | ||
} | ||
} |
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
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
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,45 @@ | ||
<?php | ||
|
||
namespace Biig\Component\Domain\Tests\fixtures\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
#[ORM\Table(name: 'fake_model_relation')] | ||
class FakeModelRelation implements \Biig\Component\Domain\Model\ModelInterface | ||
{ | ||
use \Biig\Component\Domain\Model\DomainModelTrait; | ||
|
||
#[ORM\Id()] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column(type: 'integer')] | ||
private $id; | ||
|
||
#[ORM\Column] | ||
private $content; | ||
|
||
public function __construct(string $content) | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function setContent(string $content): void | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
public function hasDispatcher() | ||
{ | ||
return null !== $this->dispatcher; | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.