Skip to content

Commit

Permalink
Merge pull request #52 from athos7933/symfony-4.3
Browse files Browse the repository at this point in the history
Update for new symfony dispatcher
  • Loading branch information
Maxime Veber authored Jul 5, 2019
2 parents 522982c + 29fcf77 commit 6c6ab63
Show file tree
Hide file tree
Showing 26 changed files with 136 additions and 110 deletions.
23 changes: 22 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
language: php

php:
- 7.1
- 7.2
- 7.3

cache:
directories:
- $HOME/.composer/cache

matrix:
include:
- php: 7.1
env: 'COMPOSER_FLAGS="--prefer-lowest --prefer-stable"'
- php: 7.1
env: 'COMPOSER_FLAGS="--prefer-stable"'
- php: 7.2
env: 'COMPOSER_FLAGS="--prefer-lowest --prefer-stable"'
- php: 7.2
env: 'COMPOSER_FLAGS="--prefer-stable"'
- php: 7.3
env: 'COMPOSER_FLAGS="--prefer-lowest --prefer-stable"'
- php: 7.3
env: 'COMPOSER_FLAGS="--prefer-stable"'

branches:
only:
- master
- v1;

install:
- composer install --prefer-source
- composer update --prefer-source $COMPOSER_FLAGS

script:
- ./vendor/bin/phpunit -c phpunit.xml.dist
4 changes: 2 additions & 2 deletions Tests/Event/DelayedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function after()
return [\FakeModel::class => 'action'];
}

public function execute(\Biig\Component\Domain\Event\DomainEvent $event)
public function execute(DomainEvent $event)
{
$model = new \FakeModel();
$model->setFoo('RulePostPersist');
Expand Down Expand Up @@ -173,7 +173,7 @@ public function after()
return [\FakeModel::class => 'action'];
}

public function execute(\Biig\Component\Domain\Event\DomainEvent $event)
public function execute(DomainEvent $event)
{
// Count times of execution
$event->getSubject()->setFoo($event->getSubject()->getFoo() + 1);
Expand Down
6 changes: 3 additions & 3 deletions Tests/Event/DomainEventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function on()
public function setFoo($foo)
{
$this->foo = $foo;
$this->dispatch('foo.changed', new DomainEvent($this));
$this->dispatch(new DomainEvent($this), 'foo.changed');
}

public function getFoo()
Expand Down Expand Up @@ -94,13 +94,13 @@ public function on()
public function setFoo($foo)
{
$this->foo = $foo;
$this->dispatch('foo.changed', new DomainEvent($this));
$this->dispatch(new DomainEvent($this), 'foo.changed');
}

public function setBar($bar)
{
$this->bar = $bar;
$this->dispatch('bar.changed', new DomainEvent($this));
$this->dispatch(new DomainEvent($this), 'bar.changed');
}

public function getFoo()
Expand Down
14 changes: 10 additions & 4 deletions Tests/Event/DomainEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@

use Biig\Component\Domain\Event\DomainEvent;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\GenericEvent;

class DomainEventTest extends TestCase
{
public function testItIsInstanceOfSymfonyEvent()
public function testItIsInstanceOfGenericEvent()
{
$event = new DomainEvent();
$this->assertInstanceOf(Event::class, $event);
$this->assertInstanceOf(GenericEvent::class, $event);
}

public function testItAcceptAnEventAsParameter()
{
$event = new DomainEvent(null, [], new GenericEvent());

$this->assertInstanceOf(GenericEvent::class, $event->getOriginalEvent());
}

Expand All @@ -30,4 +28,12 @@ public function testItReturnIfDelayedOrNot()
$event->setDelayed();
$this->assertTrue($event->isDelayed());
}

/**
* @expectedException \Biig\Component\Domain\Exception\InvalidArgumentException
*/
public function testItThrowsAnErrorIfOrignalEventIsNotAnEvent()
{
$event = new DomainEvent(null, [], 'foo');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once __DIR__ . '/../../../fixtures/FakeModel.php';

use Biig\Component\Domain\Event\DomainEventDispatcher;
use Biig\Component\Domain\Event\DomainEventDispatcherInterface;
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadata;
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory;
use Doctrine\ORM\EntityManager;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function testItAllowToRetrieveDomainModel()
$config = Setup::createYAMLMetadataConfiguration(array(__DIR__ . '/../../../fixtures/config'), true);
$config->setClassMetadataFactoryName(ClassMetadataFactory::class);

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

$conn = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once __DIR__ . '/../fixtures/FakeModel.php';

use Biig\Component\Domain\Event\DomainEventDispatcher;
use Biig\Component\Domain\Event\DomainEventDispatcherInterface;
use Biig\Component\Domain\PostPersistListener\DoctrinePostPersistListener;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\OnFlushEventArgs;
Expand All @@ -18,7 +19,7 @@ public function testItCallPersistForEachFlushedModel()
{
$model = new \FakeModel();

$dispatcher = $this->prophesize(DomainEventDispatcher::class);
$dispatcher = $this->prophesize(DomainEventDispatcherInterface::class);
$dispatcher->persistModel($model)->shouldBeCalled();

$unitOfWork = $this->prophesize(UnitOfWork::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ class EntityManagerConfiguratorTest extends TestCase
public function testItInsertsTheDispatcher()
{
$entityManager = $this->prophesize(EntityManager::class);
$dispatcher = $this->prophesize(DomainEventDispatcher::class)->reveal();
$originalConfigurator = $this->prophesize(ManagerConfigurator::class)->reveal();
$factory = new ClassMetadataFactory();

$entityManager->getMetadataFactory()->willReturn($factory);

$configurator = new EntityManagerConfigurator($originalConfigurator, $dispatcher);
$configurator = new EntityManagerConfigurator($originalConfigurator, new DomainEventDispatcher());
$configurator->configure($entityManager->reveal());

$ref = new \ReflectionObject($factory);
Expand Down
12 changes: 6 additions & 6 deletions Tests/Symfony/Serializer/DomainDenormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ class DomainDenormalizerTest extends TestCase
public function setUp()
{
$this->decorated = $this->prophesize(ObjectNormalizer::class);
$this->dispatcher = $this->prophesize(DomainEventDispatcher::class);
$this->dispatcher = new DomainEventDispatcher();
}

public function testItIsAnInstanceOfDenormalize()
{
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher->reveal());
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher);
$this->assertInstanceOf(DenormalizerInterface::class, $denormalizer);
}

public function testItDoesntSupportsDenormalization()
{
$this->decorated->supportsDenormalization(Argument::cetera())->willReturn(false);
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher->reveal());
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher);

$this->assertFalse($denormalizer->supportsDenormalization([], \stdClass::class, []));
$this->assertFalse($denormalizer->supportsDenormalization([], \FakeModel::class, []));
Expand All @@ -47,7 +47,7 @@ public function testItDoesntSupportsDenormalization()
public function testItSupportsDenormalization()
{
$this->decorated->supportsDenormalization(Argument::cetera())->willReturn(true);
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher->reveal());
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher);

$this->assertTrue($denormalizer->supportsDenormalization([], \stdClass::class, []));
$this->assertTrue($denormalizer->supportsDenormalization([], \FakeModel::class, []));
Expand All @@ -58,9 +58,9 @@ public function testDenormalize()
$fake = $this->prophesize(\FakeModel::class);

$this->decorated->denormalize([], \FakeModel::class, null, [])->willReturn($fake)->shouldBeCalled();
$fake->setDispatcher($this->dispatcher->reveal())->shouldBeCalled();
$fake->setDispatcher($this->dispatcher)->shouldBeCalled();

$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher->reveal());
$denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher);
$denormalizer->denormalize([], \FakeModel::class, null, []);
}
}
4 changes: 2 additions & 2 deletions Tests/fixtures/FakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function setFoo($foo)
*/
public function doAction()
{
$this->dispatch('previous_action', new \Biig\Component\Domain\Event\DomainEvent($this));
$this->dispatch('action', new \Biig\Component\Domain\Event\DomainEvent($this));
$this->dispatch(new \Biig\Component\Domain\Event\DomainEvent($this), 'previous_action');
$this->dispatch(new \Biig\Component\Domain\Event\DomainEvent($this), 'action');
}
}
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "MIT",
"require": {
"php": ">=7.1",
"symfony/event-dispatcher": "~3.3|~4.0"
"symfony/event-dispatcher": "^4.3|^5.0",
"doctrine/orm": "^2.6.3"
},
"authors": [
{
Expand All @@ -14,10 +15,13 @@
],
"require-dev": {
"phpunit/phpunit": "^6.4",
"doctrine/orm": "^2.5",
"symfony/symfony": "~3.3|~4.0",
"doctrine/orm": "^2.6.3",
"friendsofphp/php-cs-fixer": "^2.14",
"doctrine/doctrine-bundle": "^1.8"
"doctrine/doctrine-bundle": "^1.8",
"symfony/symfony": "^4.3|^5.0"
},
"conflict": {
"doctrine/orm": "<2.6.3"
},
"autoload": {
"psr-4": {
Expand Down
20 changes: 15 additions & 5 deletions src/Event/DomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Biig\Component\Domain\Event;

use Symfony\Component\EventDispatcher\Event;
use Biig\Component\Domain\Exception\InvalidArgumentException;
use Symfony\Component\EventDispatcher\GenericEvent;


class DomainEvent extends GenericEvent
{
/**
* @var Event
* @var \Symfony\Component\EventDispatcher\Event|\Symfony\Contracts\EventDispatcher\Event|null
*/
private $originalEvent;

Expand All @@ -19,17 +20,26 @@ class DomainEvent extends GenericEvent
*/
private $delayed;

public function __construct($subject = null, $arguments = [], Event $originalEvent = null)
public function __construct($subject = null, $arguments = [], /* Event */ $originalEvent = null)
{
// BC layer for Symfony 4.3
if (!\is_null($originalEvent) &&
!((class_exists(\Symfony\Component\EventDispatcher\Event::class) && $originalEvent instanceof \Symfony\Component\EventDispatcher\Event)
|| (class_exists(\Symfony\Contracts\EventDispatcher\Event::class) && $originalEvent instanceof \Symfony\Contracts\EventDispatcher\Event)
)
) {
throw new InvalidArgumentException('The orignal event must be an instance of Symfony Events');
}

parent::__construct($subject, $arguments);
$this->originalEvent = $originalEvent;
$this->delayed = false;
}

/**
* @return Event
* @return \Symfony\Component\EventDispatcher\Event|\Symfony\Contracts\EventDispatcher\Event|null
*/
public function getOriginalEvent(): ?Event
public function getOriginalEvent()
{
return $this->originalEvent;
}
Expand Down
12 changes: 7 additions & 5 deletions src/Event/DomainEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Biig\Component\Domain\Rule\DomainRuleInterface;
use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface;
use Biig\Component\Domain\Rule\RuleInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;

class DomainEventDispatcher extends EventDispatcher
final class DomainEventDispatcher extends EventDispatcher implements DomainEventDispatcherInterface
{
/**
* @var DelayedListener[]
Expand All @@ -19,6 +19,7 @@ class DomainEventDispatcher extends EventDispatcher

public function __construct()
{
parent::__construct();
$this->delayedListeners = [];
}

Expand Down Expand Up @@ -79,14 +80,14 @@ public function addPostPersistDomainRuleInterface(PostPersistDomainRuleInterface
}

/**
* @param string $eventName
* @param Event|null $event
*
* @return Event
*/
public function dispatch($eventName, Event $event = null)
public function dispatch($event/*, string $eventName = null*/)
{
$event = parent::dispatch($eventName, $event);
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
$event = parent::dispatch($event, $eventName);

if ($event instanceof DomainEvent) {
foreach ($this->delayedListeners as $listener) {
Expand All @@ -104,6 +105,7 @@ public function dispatch($eventName, Event $event = null)
*/
public function persistModel(ModelInterface $model)
{

foreach ($this->delayedListeners as $listener) {
if ($listener->shouldOccur($model)) {
$listener->process($model);
Expand Down
15 changes: 15 additions & 0 deletions src/Event/DomainEventDispatcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Biig\Component\Domain\Event;

use Biig\Component\Domain\Model\ModelInterface;
use Biig\Component\Domain\Rule\DomainRuleInterface;
use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

interface DomainEventDispatcherInterface extends EventDispatcherInterface
{
public function addDomainRule(DomainRuleInterface $rule);
public function addPostPersistDomainRuleInterface(PostPersistDomainRuleInterface $rule);
public function persistModel(ModelInterface $model);
}
10 changes: 2 additions & 8 deletions src/Integration/Symfony/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('biig_domain');

if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC for symfony/config < 4.2
$rootNode = $treeBuilder->root('biig_domain');
}

$rootNode
$treeBuilder
->getRootNode()
->children()
->booleanNode('override_doctrine_instantiator')
->defaultTrue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Biig\Component\Domain\Integration\Symfony\DependencyInjection;

use Biig\Component\Domain\Event\DomainEventDispatcher;
use Biig\Component\Domain\Event\DomainEventDispatcherInterface;
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory;
use Doctrine\Bundle\DoctrineBundle\ManagerConfigurator;
use Doctrine\ORM\EntityManager;
Expand All @@ -18,11 +18,11 @@ class EntityManagerConfigurator
private $originalConfigurator;

/**
* @var DomainEventDispatcher
* @var DomainEventDispatcherInterface
*/
private $dispatcher;

public function __construct(ManagerConfigurator $configurator, DomainEventDispatcher $dispatcher)
public function __construct(ManagerConfigurator $configurator, DomainEventDispatcherInterface $dispatcher)
{
$this->originalConfigurator = $configurator;
$this->dispatcher = $dispatcher;
Expand Down
Loading

0 comments on commit 6c6ab63

Please sign in to comment.