From 82a930c75dbed5ae074a88f3289d379ef50425c2 Mon Sep 17 00:00:00 2001 From: Nathan Utama Date: Thu, 24 Sep 2015 12:01:12 +0200 Subject: [PATCH] Fixed issue no event on initialized proxies --- src/Listener/EntityChangedListener.php | 4 +- test/Listener/EntityChangedListenerTest.php | 46 +++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Listener/EntityChangedListener.php b/src/Listener/EntityChangedListener.php index 358b337..38cb200 100644 --- a/src/Listener/EntityChangedListener.php +++ b/src/Listener/EntityChangedListener.php @@ -81,7 +81,9 @@ public function preFlush(PreFlushEventArgs $event) } foreach ($updates as $entity) { - if (!$this->meta_mutation_provider->isEntityManaged($em, $entity) || $entity instanceof Proxy) { + if (!$this->meta_mutation_provider->isEntityManaged($em, $entity) + || ($entity instanceof Proxy && !$entity->__isInitialized()) + ) { continue; } diff --git a/test/Listener/EntityChangedListenerTest.php b/test/Listener/EntityChangedListenerTest.php index f1383d1..a7da03f 100644 --- a/test/Listener/EntityChangedListenerTest.php +++ b/test/Listener/EntityChangedListenerTest.php @@ -251,6 +251,52 @@ public function testPreFlushWithProxy() $this->listener->preFlush(new PreFlushEventArgs($this->em)); } + public function testPreFlushWithInitializedProxy() + { + $original = new \stdClass(); + $original->id = 0; + + $entity = $this->getMock('Doctrine\ORM\Proxy\Proxy'); + $entity + ->expects($this->once()) + ->method('__isInitialized') + ->willReturn(true); + + $this->meta_mutation_provider + ->expects($this->once()) + ->method('getFullChangeSet') + ->willReturn($this->genericEntityDataProvider($entity)); + + $this->meta_annotation_provider + ->expects($this->once()) + ->method('isTracked') + ->willReturn(true); + + $this->logger->expects($this->once())->method('info'); + + $this->meta_mutation_provider + ->expects($this->once()) + ->method('isEntityManaged') + ->willReturn(true); + + $this->meta_mutation_provider + ->expects($this->once()) + ->method('createOriginalEntity') + ->willReturn($original); + + $this->meta_mutation_provider + ->expects($this->once()) + ->method('getMutatedFields') + ->willReturn(['id']); + + $this->event_manager + ->expects($this->once()) + ->method('dispatchEvent') + ->with('entityChanged', $this->isInstanceof('Hostnet\Component\EntityTracker\Event\EntityChangedEvent')); + + $this->listener->preFlush(new PreFlushEventArgs($this->em)); + } + /** * @param mixed $entity * @return array[]