Skip to content

Commit

Permalink
use UnitOfWork in EventPuller
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Aug 17, 2017
1 parent 1c47c35 commit 7abbc67
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Event/Listener/DomainEventPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getSubscribedEvents()
public function onFlush(OnFlushEventArgs $args)
{
// aggregate events from deleted entities
$this->events = $this->puller->pull($args->getEntityManager());
$this->events = $this->puller->pull($args->getEntityManager()->getUnitOfWork());
}

/**
Expand All @@ -82,7 +82,7 @@ public function onFlush(OnFlushEventArgs $args)
public function postFlush(PostFlushEventArgs $args)
{
// aggregate PreRemove/PostRemove events
$events = array_merge($this->events, $this->puller->pull($args->getEntityManager()));
$events = array_merge($this->events, $this->puller->pull($args->getEntityManager()->getUnitOfWork()));

// clear aggregate events before publish it
// it necessary for fix recursive publish of events
Expand Down
7 changes: 3 additions & 4 deletions src/Service/EventPuller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@
namespace GpsLab\Bundle\DomainEvent\Service;

use Doctrine\Common\Persistence\Proxy;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use GpsLab\Domain\Event\Aggregator\AggregateEvents;
use GpsLab\Domain\Event\Event;

class EventPuller
{
/**
* @param EntityManagerInterface $em
* @param UnitOfWork $uow
*
* @return Event[]
*/
public function pull(EntityManagerInterface $em)
public function pull(UnitOfWork $uow)
{
$uow = $em->getUnitOfWork();
$events = [];

$events = array_merge($events, $this->pullFromEntities($uow->getScheduledEntityDeletions()));
Expand Down
51 changes: 39 additions & 12 deletions tests/Event/Listener/DomainEventPublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\UnitOfWork;
use GpsLab\Bundle\DomainEvent\Event\Listener\DomainEventPublisher;
use GpsLab\Bundle\DomainEvent\Service\EventPuller;
use GpsLab\Domain\Event\Bus\EventBus;
Expand All @@ -36,6 +37,11 @@ class DomainEventPublisherTest extends \PHPUnit_Framework_TestCase
*/
private $em;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
*/
private $uow;

/**
* @var OnFlushEventArgs
*/
Expand All @@ -58,6 +64,11 @@ protected function setUp()
$this->em = $this->getMock(EntityManagerInterface::class);
$this->on_flush = new OnFlushEventArgs($this->em);
$this->post_flush = new PostFlushEventArgs($this->em);
$this->uow = $this
->getMockBuilder(UnitOfWork::class)
->disableOriginalConstructor()
->getMock()
;

$this->publisher = new DomainEventPublisher($this->puller, $this->bus, true);
}
Expand All @@ -76,10 +87,16 @@ public function testEnabled()

public function testPreFlush()
{
$this->em
->expects($this->once())
->method('getUnitOfWork')
->will($this->returnValue($this->uow))
;

$this->puller
->expects($this->once())
->method('pull')
->with($this->em)
->with($this->uow)
;

$this->publisher->onFlush($this->on_flush);
Expand Down Expand Up @@ -121,13 +138,13 @@ public function testPublishEvents(array $remove_events, array $exist_events)
$this->puller
->expects($this->at(0))
->method('pull')
->with($this->em)
->with($this->uow)
->will($this->returnValue($remove_events))
;
$this->puller
->expects($this->at(1))
->method('pull')
->with($this->em)
->with($this->uow)
->will($this->returnValue($exist_events))
;

Expand Down Expand Up @@ -155,6 +172,11 @@ public function testPublishEvents(array $remove_events, array $exist_events)
->method('flush')
;
}
$this->em
->expects($this->atLeastOnce())
->method('getUnitOfWork')
->will($this->returnValue($this->uow))
;

$this->publisher->onFlush($this->on_flush);
$this->publisher->postFlush($this->post_flush);
Expand All @@ -181,28 +203,38 @@ public function testRecursivePublish()
$this->getMock(Event::class),
];

$this->em
->expects($this->atLeastOnce())
->method('getUnitOfWork')
->will($this->returnValue($this->uow))
;
$this->em
->expects($this->exactly(2))
->method('flush')
;

$this->puller
->expects($this->at(0))
->method('pull')
->with($this->em)
->with($this->uow)
->will($this->returnValue($remove_events1))
;
$this->puller
->expects($this->at(1))
->method('pull')
->with($this->em)
->with($this->uow)
->will($this->returnValue($exist_events1))
;
$this->puller
->expects($this->at(2))
->method('pull')
->with($this->em)
->with($this->uow)
->will($this->returnValue($remove_events2))
;
$this->puller
->expects($this->at(3))
->method('pull')
->with($this->em)
->with($this->uow)
->will($this->returnValue($exist_events2))
;

Expand All @@ -215,11 +247,6 @@ public function testRecursivePublish()
;
}

$this->em
->expects($this->exactly(2))
->method('flush')
;

$this->publisher->onFlush($this->on_flush);
$this->publisher->postFlush($this->post_flush);
// recursive call
Expand Down
15 changes: 1 addition & 14 deletions tests/Service/EventPullerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@
namespace GpsLab\Bundle\DomainEvent\Tests\Service;

use Doctrine\Common\Persistence\Proxy;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use GpsLab\Bundle\DomainEvent\Service\EventPuller;
use GpsLab\Domain\Event\Aggregator\AggregateEvents;
use GpsLab\Domain\Event\Event;

class EventPullerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
*/
private $em;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
*/
Expand All @@ -42,13 +36,6 @@ protected function setUp()
->getMock()
;

$this->em = $this->getMock(EntityManagerInterface::class);
$this->em
->expects($this->once())
->method('getUnitOfWork')
->will($this->returnValue($this->uow))
;

$this->puller = new EventPuller();
}

Expand Down Expand Up @@ -172,7 +159,7 @@ public function testPull(
$map_events
);

$this->assertEquals($expected_events, $this->puller->pull($this->em));
$this->assertEquals($expected_events, $this->puller->pull($this->uow));
}

/**
Expand Down

0 comments on commit 7abbc67

Please sign in to comment.