From 216c4662333780fb7f09b71795ed75478286d0fa Mon Sep 17 00:00:00 2001 From: bilouwan Date: Fri, 27 Nov 2015 17:28:45 +0100 Subject: [PATCH] Unit test & fix for merge versionned entity --- lib/Doctrine/ORM/UnitOfWork.php | 14 +++- .../Models/VersionedOneToMany/Article.php | 47 +++++++++++++ .../Models/VersionedOneToMany/Category.php | 49 ++++++++++++++ .../MergeVersionedOneToManyTest.php | 67 +++++++++++++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php create mode 100644 tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/MergeVersionedOneToManyTest.php diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 194e45e30..1818b86cd 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1870,7 +1870,7 @@ class UnitOfWork implements PropertyChangedListener } } - if ($class->isVersioned) { + if ($class->isVersioned && !($this->isNotInitializedProxy($managedCopy) || $this->isNotInitializedProxy($entity))) { $reflField = $class->reflFields[$class->versionField]; $managedCopyVersion = $reflField->getValue($managedCopy); $entityVersion = $reflField->getValue($entity); @@ -1908,6 +1908,18 @@ class UnitOfWork implements PropertyChangedListener return $managedCopy; } + /** + * Tests if an entity is a non initialized proxy class + * + * @param $entity + * + * @return bool + */ + private function isNotInitializedProxy($entity) + { + return $entity instanceof Proxy && !$entity->__isInitialized(); + } + /** * Sets/adds associated managed copies into the previous entity's association field * diff --git a/tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php b/tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php new file mode 100644 index 000000000..c284fc15f --- /dev/null +++ b/tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php @@ -0,0 +1,47 @@ +tags = new ArrayCollection(); + } +} diff --git a/tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php b/tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php new file mode 100644 index 000000000..5ace04603 --- /dev/null +++ b/tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php @@ -0,0 +1,49 @@ +articles = new ArrayCollection(); + } + + +} diff --git a/tests/Doctrine/Tests/ORM/Functional/MergeVersionedOneToManyTest.php b/tests/Doctrine/Tests/ORM/Functional/MergeVersionedOneToManyTest.php new file mode 100644 index 000000000..c5f022c52 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/MergeVersionedOneToManyTest.php @@ -0,0 +1,67 @@ +_schemaTool->createSchema( + [ + $this->_em->getClassMetadata(Category::class), + $this->_em->getClassMetadata(Article::class), + ] + ); + } catch (ORMException $e) { + } + } + + /** + * This test case tests that a versionable entity, that has a oneToOne relationship as it's id can be created + * without this bug fix (DDC-3318), you could not do this + */ + public function testSetVersionOnCreate() + { + $category = new Category(); + $category->name = 'Category'; + + $article = new Article(); + $article->name = 'Article'; + $article->category = $category; + + $this->_em->persist($article); + $this->_em->flush(); + $this->_em->clear(); + + $mergeSucceed = false; + try { + $articleMerged = $this->_em->merge($article); + $mergeSucceed = true; + } catch (OptimisticLockException $e) { + } + $this->assertTrue($mergeSucceed); + + $articleMerged->name = 'Article Merged'; + + $flushSucceed = false; + try { + $this->_em->flush(); + $flushSucceed = true; + } catch (OptimisticLockException $e) { + } + $this->assertTrue($flushSucceed); + } +}