Skip to content

Commit

Permalink
Merge pull request #21 from swagindustries/feature/support-proxy
Browse files Browse the repository at this point in the history
Tests update + add support for Doctrine proxy
  • Loading branch information
Nek- authored Jul 21, 2024
2 parents a70ffe8 + 3649813 commit 6275a32
Show file tree
Hide file tree
Showing 30 changed files with 309 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4', '8.0', '8.1']
php: ['8.1', '8.2', '8.3']
steps:
- uses: actions/checkout@v2

Expand Down
38 changes: 4 additions & 34 deletions Tests/Event/DelayedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
use Biig\Component\Domain\Event\DomainEventDispatcher;
use Biig\Component\Domain\Exception\InvalidDomainEvent;
use Biig\Component\Domain\Model\DomainModel;
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory;
use Biig\Component\Domain\PostPersistListener\DoctrinePostPersistListener;
use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface;
use Biig\Component\Domain\Tests\fixtures\Entity\FakeModel;
use Biig\Component\Domain\Tests\SetupDatabaseTrait;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
use PHPUnit\Framework\TestCase;

class DelayedListenerTest extends TestCase
{
private $dbPath;
use SetupDatabaseTrait;

public function testICanInstantiateDelayedListener()
{
Expand Down Expand Up @@ -62,7 +61,7 @@ public function testItFailsToRegisterOtherThanCurrentModel()
public function testItInsertInBddAfterFlushing()
{
$dispatcher = new DomainEventDispatcher();
$entityManager = $this->setupDatabase($dispatcher);
$entityManager = $this->setupDatabase($dispatcher, 'testItInsertInBddAfterFlushing');

$model = new FakeModel();
$model->setFoo('Model1');
Expand Down Expand Up @@ -104,7 +103,7 @@ public function testItDoesNotExecuteManyTimesSameEvent()
{
// Test setup
$dispatcher = new DomainEventDispatcher();
$entityManager = $this->setupDatabase($dispatcher);
$entityManager = $this->setupDatabase($dispatcher, 'testItInsertInBddAfterFlushing');

$model = new FakeModel();
$model->setFoo(0);
Expand All @@ -122,35 +121,6 @@ public function testItDoesNotExecuteManyTimesSameEvent()
$this->assertEquals(2, $model->getFoo());
$this->dropDatabase();
}

private function setupDatabase(DomainEventDispatcher $dispatcher)
{
$this->dbPath = \sys_get_temp_dir() . '/testItInsertInBddAfterFlushing.' . \microtime() . '.sqlite';
copy(__DIR__ . '/../fixtures/dbtest/initial_fake_model.db', $this->dbPath);

$config = ORMSetup::createAnnotationMetadataConfiguration(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->getMetadataFactory()->setDispatcher($dispatcher);

return $entityManager;
}

private function dropDatabase()
{
if (!$this->dbPath) {
return;
}

@unlink($this->dbPath);
}
}

class FakeDomainModel extends DomainModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Biig\Component\Domain\Event\DomainEventDispatcherInterface;
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadata;
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory;
use Biig\Component\Domain\Tests\SetupDatabaseTrait;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
use PHPUnit\Framework\TestCase;
Expand All @@ -17,6 +18,7 @@
class ClassMetadataFactoryTest extends TestCase
{
use ProphecyTrait;
use SetupDatabaseTrait;

public function testItIsAnInstanceOfDoctrineClassMetadataFactory()
{
Expand All @@ -26,42 +28,27 @@ public function testItIsAnInstanceOfDoctrineClassMetadataFactory()

public function testItReturnAnInstanceOfClassMetadata()
{
$dbpath = \sys_get_temp_dir() . '/testItReturnAnInstanceOfClassMetadata.' . \microtime() . '.sqlite';

$config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true);
$config->setClassMetadataFactoryName(ClassMetadataFactory::class);

$conn = [
'driver' => 'pdo_sqlite',
'path' => $dbpath,
];
$entityManager = EntityManager::create($conn, $config);
$entityManager->getMetadataFactory()->setDispatcher(new DomainEventDispatcher());
$entityManager = $this->setupDatabase(new DomainEventDispatcher(), 'testItReturnAnInstanceOfClassMetadata');

$metadata = $entityManager->getMetadataFactory()->getMetadataFor(FakeModel::class);

$this->assertInstanceOf(ClassMetadata::class, $metadata);

@unlink($dbpath);
$this->dropDatabase();
}

public function testItAllowToRetrieveDomainModel()
{
$config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true);
$config->setClassMetadataFactoryName(ClassMetadataFactory::class);

$dispatcher = $this->prophesize(DomainEventDispatcherInterface::class);
$dispatcher->dispatch(Argument::cetera())->shouldBeCalled();

$conn = [
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/../../../fixtures/dbtest/initial_fake_model.db',
];
$entityManager = EntityManager::create($conn, $config);
$entityManager->getMetadataFactory()->setDispatcher($dispatcher->reveal());
$entityManager = $this->setupDatabase($dispatcher->reveal(), 'testItAllowToRetrieveDomainModel');

$res = $entityManager->getRepository(FakeModel::class)->findAll();

reset($res)->doAction();
/** @var FakeModel $item */
$item = reset($res);
$item->doAction();
$this->dropDatabase();
}
}
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();
}
}
45 changes: 45 additions & 0 deletions Tests/SetupDatabaseTrait.php
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);
}
}
4 changes: 2 additions & 2 deletions Tests/config/symfony_test_kernel_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ framework:
serializer: ~
form: ~
test: ~
annotations: ~
assets: ~
property_access: ~

doctrine:
dbal:
Expand All @@ -25,7 +25,7 @@ doctrine:
mappings:
TestApplication:
is_bundle: false
type: annotation
type: attribute
dir: '%kernel.project_dir%/Tests/fixtures/Entity'
prefix: 'Biig\Component\Domain\Tests\fixtures\Entity'
alias: App
55 changes: 25 additions & 30 deletions Tests/fixtures/Entity/FakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,65 @@

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\Table(name="fake_model")
*/
#[ORM\Entity]
#[ORM\Table(name: 'fake_model')]
class FakeModel implements \Biig\Component\Domain\Model\ModelInterface
{
use \Biig\Component\Domain\Model\DomainModelTrait;

/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[ORM\Id()]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

/** @ORM\Column() */
#[ORM\Column(nullable: true)]
private $foo;

#[ORM\OneToOne(targetEntity: FakeModelRelation::class)]
private FakeModelRelation|null $related = null;

private $something;

public function hasDispatcher()
{
return null !== $this->dispatcher;
}

/**
* @return int
*/
public function getId()
public function getId(): int
{
return $this->id;
}

/**
* @return string
*/
public function getFoo()
public function getFoo(): string
{
return $this->foo;
}

/**
* @return mixed
*/
public function getSomething()
public function getSomething(): mixed
{
return $this->something;
}

/**
* @param mixed $something
*/
public function setSomething($something)
public function setSomething(string $something)
{
$this->something = $something;
}

/**
* @param string $foo
*/
public function setFoo($foo)
public function setFoo(string $foo)
{
$this->foo = $foo;
}

public function getRelated(): ?FakeModelRelation
{
return $this->related;
}

public function setRelated(?FakeModelRelation $related): void
{
$this->related = $related;
}

/**
* Raise a domain event.
*/
Expand Down
45 changes: 45 additions & 0 deletions Tests/fixtures/Entity/FakeModelRelation.php
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 modified Tests/fixtures/dbtest/fake_model.db
Binary file not shown.
Binary file modified Tests/fixtures/dbtest/initial_fake_model.db
Binary file not shown.
Loading

0 comments on commit 6275a32

Please sign in to comment.