Skip to content

Collect recorded messages on all managed entities even not dirty ones #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions src/EventListener/CollectsEventsFromEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace SimpleBus\DoctrineORMBridge\EventListener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Proxy\Proxy;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use SimpleBus\Message\Recorder\ContainsRecordedMessages;

class CollectsEventsFromEntities implements EventSubscriber, ContainsRecordedMessages
Expand All @@ -14,25 +16,40 @@ class CollectsEventsFromEntities implements EventSubscriber, ContainsRecordedMes
public function getSubscribedEvents()
{
return array(
Events::postPersist,
Events::postUpdate,
Events::postRemove,
Events::preFlush,
Events::postFlush
);
}

public function postPersist(LifecycleEventArgs $event)
public function preFlush(PreFlushEventArgs $eventArgs)
{
$this->collectEventsFromEntity($event);
}

public function postUpdate(LifecycleEventArgs $event)
{
$this->collectEventsFromEntity($event);
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getIdentityMap() as $entities) {
foreach ($entities as $entity){
$this->collectEventsFromEntity($entity);
}
}
foreach ($uow->getScheduledEntityDeletions() as $entity) {
$this->collectEventsFromEntity($entity);
}
}

public function postRemove(LifecycleEventArgs $event)
/**
* We need to listen on postFlush for Lifecycle Events
* All Lifecycle callback events are triggered after the onFlush event
*
* @param PostFlushEventArgs $eventArgs
*/
public function postFlush(PostFlushEventArgs $eventArgs)
{
$this->collectEventsFromEntity($event);
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getIdentityMap() as $entities) {
foreach ($entities as $entity){
$this->collectEventsFromEntity($entity);
}
}
}

public function recordedMessages()
Expand All @@ -45,15 +62,16 @@ public function eraseMessages()
$this->collectedEvents = array();
}

private function collectEventsFromEntity(LifecycleEventArgs $event)
private function collectEventsFromEntity($entity)
{
$entity = $event->getEntity();

if ($entity instanceof ContainsRecordedMessages) {
if ($entity instanceof ContainsRecordedMessages
&& ( !$entity instanceof Proxy
|| ($entity instanceof Proxy && $entity->__isInitialized__)
)
) {
foreach ($entity->recordedMessages() as $event) {
$this->collectedEvents[] = $event;
}

$entity->eraseMessages();
}
}
Expand Down
55 changes: 52 additions & 3 deletions tests/EventListener/CollectsEventsFromEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Entity\EventRecordingEntity;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityAboutToBeRemoved;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityChanged;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityChangedPreUpdate;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityCreated;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityCreatedPrePersist;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityNotDirty;
use SimpleBus\Message\Recorder\ContainsRecordedMessages;

class CollectsEventsFromEntitiesTest extends AbstractTestCaseWithEntityManager
Expand Down Expand Up @@ -38,8 +41,7 @@ public function it_collects_events_from_persisted_entities_and_erases_them_after
$entity = new EventRecordingEntity();

$this->persistAndFlush($entity);

$this->assertEquals([new EntityCreated()], $this->eventSubscriber->recordedMessages());
$this->assertContains( new EntityCreated(), $this->eventSubscriber->recordedMessages(), '', false, false );

$this->assertEntityHasNoRecordedEvents($entity);
}
Expand All @@ -56,7 +58,7 @@ public function it_collects_events_from_modified_entities_and_erases_them_afterw
$entity->changeSomething();
$this->persistAndFlush($entity);

$this->assertEquals([new EntityChanged()], $this->eventSubscriber->recordedMessages());
$this->assertContains( new EntityChanged(), $this->eventSubscriber->recordedMessages(), '', false, false );

$this->assertEntityHasNoRecordedEvents($entity);
}
Expand All @@ -78,6 +80,53 @@ public function it_collects_events_from_removed_entities_and_erases_them_afterwa
$this->assertEntityHasNoRecordedEvents($entity);
}

/**
* @test
*/
public function it_collects_events_from_not_dirty_entities_and_erases_them_afterwards()
{
$entity = new EventRecordingEntity();
$this->persistAndFlush($entity);
$this->eraseRecordedMessages();

$entity->recordMessageWithoutStateChange();
$this->persistAndFlush($entity);

$this->assertEquals([new EntityNotDirty()], $this->eventSubscriber->recordedMessages());

$this->assertEntityHasNoRecordedEvents($entity);
}

/**
* @test
*/
public function it_collects_events_from_pre_persist_lifecycle_callbacks_of_entities_and_erases_them_afterwards()
{
$entity = new EventRecordingEntity();
$this->persistAndFlush($entity);

$this->assertContains( new EntityCreatedPrePersist(), $this->eventSubscriber->recordedMessages(), '', false, false );

$this->assertEntityHasNoRecordedEvents($entity);
}

/**
* @test
*/
public function it_collects_events_from_pre_update_lifecycle_callbacks_of_dirty_entities_and_erases_them_afterwards()
{
$entity = new EventRecordingEntity();
$this->persistAndFlush($entity);
$this->eraseRecordedMessages();

$entity->changeSomethingWithoutRecording();
$this->persistAndFlush($entity);

$this->assertEquals([new EntityChangedPreUpdate()], $this->eventSubscriber->recordedMessages());

$this->assertEntityHasNoRecordedEvents($entity);
}

private function persistAndFlush($entity)
{
$this->getEntityManager()->persist($entity);
Expand Down
30 changes: 30 additions & 0 deletions tests/EventListener/Fixtures/Entity/EventRecordingEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityAboutToBeRemoved;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityChanged;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityCreated;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityCreatedPrePersist;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityChangedPreUpdate;
use SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event\EntityNotDirty;
use SimpleBus\Message\Recorder\ContainsRecordedMessages;
use SimpleBus\Message\Recorder\PrivateMessageRecorderCapabilities;

/**
* @Entity
* @HasLifecycleCallbacks()
*/
class EventRecordingEntity implements ContainsRecordedMessages
{
Expand Down Expand Up @@ -41,10 +45,36 @@ public function changeSomething()
$this->record(new EntityChanged());
}

public function changeSomethingWithoutRecording()
{
$this->something = 'changed value';
}

public function prepareForRemoval()
{
$this->something = 'changed for the last time';

$this->record(new EntityAboutToBeRemoved());
}

public function recordMessageWithoutStateChange()
{
$this->record(new EntityNotDirty());
}

/**
* @PrePersist
*/
public function recordMessageDuringPrePersistLifecycleCallback()
{
$this->record(new EntityCreatedPrePersist());
}

/**
* @PreUpdate
*/
public function recordMessageDuringPreUpdateLifecycleCallback()
{
$this->record(new EntityChangedPreUpdate());
}
}
7 changes: 7 additions & 0 deletions tests/EventListener/Fixtures/Event/EntityChangedPreUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event;

class EntityChangedPreUpdate
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event;

class EntityCreatedPrePersist
{
}
7 changes: 7 additions & 0 deletions tests/EventListener/Fixtures/Event/EntityNotDirty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SimpleBus\DoctrineORMBridge\Tests\EventListener\Fixtures\Event;

class EntityNotDirty
{
}