From 48c59440d8fd0628ecbfaee6ec7c1021aa75ca13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20B=C3=A1n?= Date: Thu, 29 Oct 2015 13:47:05 +0100 Subject: [PATCH 1/3] Use NOTIFY change tracking policy on NotifyChangedDocument class --- tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php index c887c86080..13731a115e 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php @@ -686,7 +686,10 @@ public function __construct($name) } } -/** @ODM\Document */ +/** + * @ODM\Document + * @ODM\ChangeTrackingPolicy("NOTIFY") + */ class NotifyChangedDocument implements \Doctrine\Common\NotifyPropertyChanged { private $_listeners = array(); From cb01a385e70d734f424e98142772af88ed76dafa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20B=C3=A1n?= Date: Thu, 29 Oct 2015 14:01:05 +0100 Subject: [PATCH 2/3] Committing multiple times with NOTIFY change tracking policy doesn't raise a notice error --- lib/Doctrine/ODM/MongoDB/UnitOfWork.php | 2 +- .../ODM/MongoDB/Tests/UnitOfWorkTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php index d732dbc9d1..5641b6ee27 100644 --- a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php +++ b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php @@ -690,7 +690,7 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r // and we have a copy of the original data $originalData = $this->originalDocumentData[$oid]; $isChangeTrackingNotify = $class->isChangeTrackingNotify(); - if ($isChangeTrackingNotify && ! $recompute) { + if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { $changeSet = $this->documentChangeSets[$oid]; } else { $changeSet = array(); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php index 13731a115e..587448337c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php @@ -240,6 +240,24 @@ public function testChangeTrackingNotify() $this->assertTrue($updates[0] === $item); } + public function testDoubleCommitWithChangeTrackingNotify() + { + $pb = $this->getMockPersistenceBuilder(); + + $class = $this->dm->getClassMetadata('Doctrine\ODM\MongoDB\Tests\NotifyChangedDocument'); + $persister = $this->getMockDocumentPersister($pb, $class); + $this->uow->setDocumentPersister($class->name, $persister); + + $entity = new NotifyChangedDocument(); + $entity->setId(2); + $this->uow->persist($entity); + + $this->uow->commit($entity); + @$this->uow->commit($entity); + + $this->assertNull(error_get_last(), 'Expected not to get an error after committing an entity multiple times using the NOTIFY change tracking policy.'); + } + public function testGetDocumentStateWithAssignedIdentity() { $pb = $this->getMockPersistenceBuilder(); From aeec731031bf70dd9ee6e2b8e1eadcf46dbde7fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20B=C3=A1n?= Date: Thu, 29 Oct 2015 15:45:28 +0100 Subject: [PATCH 3/3] Updated test to use an error handler instead of the @ operator and error_get_last() for catching an E_NOTICE error --- tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php index 587448337c..35920aa6c5 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php @@ -253,9 +253,18 @@ public function testDoubleCommitWithChangeTrackingNotify() $this->uow->persist($entity); $this->uow->commit($entity); - @$this->uow->commit($entity); - $this->assertNull(error_get_last(), 'Expected not to get an error after committing an entity multiple times using the NOTIFY change tracking policy.'); + // Use a custom error handler that will fail the test if the next commit() call raises a notice error + set_error_handler(function() { + restore_error_handler(); + + $this->fail('Expected not to get a notice error after committing an entity multiple times using the NOTIFY change tracking policy.'); + }, E_NOTICE); + + $this->uow->commit($entity); + + // Restore previous error handler if no errors have been raised + restore_error_handler(); } public function testGetDocumentStateWithAssignedIdentity()