Skip to content

Commit

Permalink
Support proxy
Browse files Browse the repository at this point in the history
This commit also comes with a dependencies upgrade:
- PHP 7.4 & 8.0 are no more supported
- Symfony 4.x is not supported anymore
  • Loading branch information
Nek- committed Feb 1, 2024
1 parent a70ffe8 commit 96fb9fd
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 92 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
@@ -0,0 +1,27 @@
<?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 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());
}

public function testItLoadsDispatcherInProxyEntity()
{

}
}
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\DomainEventDispatcher;
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(DomainEventDispatcher $dispatcher, string $name)
{
$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);
}
}
41 changes: 11 additions & 30 deletions Tests/fixtures/Entity/FakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@

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]
private $foo;
private $something;

Expand All @@ -27,42 +23,27 @@ 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;
}
Expand Down
Binary file modified Tests/fixtures/dbtest/fake_model.db
Binary file not shown.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "symfony-bundle",
"license": "MIT",
"require": {
"php": ">=7.4",
"php": ">=8.1",
"symfony/event-dispatcher": "^4.3|^5.0|^6.0",
"doctrine/doctrine-bundle": "^1.8|^2.0",
"doctrine/orm": "^2.6.3"
Expand All @@ -15,10 +15,10 @@
}
],
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5 || ^10",
"doctrine/orm": "^2.6.3",
"friendsofphp/php-cs-fixer": "^3.11.0",
"symfony/symfony": "^4.3 || ^5.0 || ^6.0",
"symfony/symfony": "^5.0 || ^6.0 || ^7.0",
"phpspec/prophecy-phpunit": "^2.0.1",
"monolog/monolog": "^2.8.0 || ^3.2.0"
},
Expand Down
43 changes: 20 additions & 23 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="Tests/bootstrap.php" colors="true">

<php>
<server name="APP_ENV" value="test" force="true" />
<server name="KERNEL_CLASS" value="Biig\Component\Domain\Tests\TestKernel" force="true" />
</php>
<testsuites>
<testsuite name="BiiG Domain">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./docs</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="Tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./docs</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<php>
<server name="APP_ENV" value="test" force="true"/>
<server name="KERNEL_CLASS" value="Biig\Component\Domain\Tests\TestKernel" force="true"/>
</php>
<testsuites>
<testsuite name="BiiG Domain">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>
</phpunit>
25 changes: 25 additions & 0 deletions phpunit.xml.dist.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="Tests/bootstrap.php" colors="true">

<php>
<server name="APP_ENV" value="test" force="true" />
<server name="KERNEL_CLASS" value="Biig\Component\Domain\Tests\TestKernel" force="true" />
</php>
<testsuites>
<testsuite name="BiiG Domain">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./docs</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Biig\Component\Domain\Model\Instantiator\DoctrineConfig;

use Biig\Component\Domain\Event\DomainEventDispatcherInterface;
use Biig\Component\Domain\Model\DomainModel;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;

class PostLoadDispatcherInjectionListener implements EventSubscriber
{
public function __construct(private DomainEventDispatcherInterface $dispatcher)
{
}

public function getSubscribedEvents()
{
return ['postLoad'];
}

/**
* BC Layer: typing LifecycleEventArgs for previous Doctrine versions.
* New versions use \Doctrine\ORM\Event\PostLoadEventArgs.
*/
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getObject();

if ($entity instanceof DomainModel) {
$entity->setDispatcher($this->dispatcher);
}
}
}
2 changes: 1 addition & 1 deletion src/PostFlushListener/EntitiesHasDispatcherChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function postFlush(PostFlushEventArgs $args): void

private function checkModelEntityHasDispatcher(object $entity, EntityManagerInterface $entityManager): void
{
if (!$entity instanceof ModelInterface || $entity instanceof Proxy) {
if (!$entity instanceof ModelInterface) {
return;
}

Expand Down

0 comments on commit 96fb9fd

Please sign in to comment.