1
0
mirror of synced 2025-02-09 00:39:25 +03:00

Unit test & fix for merge versionned entity

This commit is contained in:
bilouwan 2015-11-27 17:28:45 +01:00 committed by Marco Pivetta
parent 65f5777e60
commit 216c466233
4 changed files with 176 additions and 1 deletions

View File

@ -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
*

View File

@ -0,0 +1,47 @@
<?php
namespace Doctrine\Tests\Models\VersionedOneToMany;
use Doctrine\Common\Collections\ArrayCollection;
/**
*
* @Entity
* @Table(name="article")
*/
class Article
{
/**
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(name="name")
*/
public $name;
/**
* @ManyToOne(targetEntity="Category", inversedBy="category", cascade={"merge", "persist"})
*/
public $category;
/**
* Version column
*
* @Column(type="integer", name="version")
* @Version
*/
public $version;
/**
* Category constructor.
*
*/
public function __construct()
{
$this->tags = new ArrayCollection();
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace Doctrine\Tests\Models\VersionedOneToMany;
use Doctrine\Common\Collections\ArrayCollection;
/**
*
* @Entity
* @Table(name="category")
*/
class Category
{
/**
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @OneToMany(targetEntity="Article", mappedBy="category", cascade={"merge", "persist"})
*/
public $articles;
/**
* @Column(name="name")
*/
public $name;
/**
* Version column
*
* @Column(type="integer", name="version")
* @Version
*/
public $version;
/**
* Category constructor.
*
*/
public function __construct()
{
$this->articles = new ArrayCollection();
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Tests\Models\VersionedOneToMany\Article;
use Doctrine\Tests\Models\VersionedOneToMany\Category;
use Doctrine\Tests\Models\VersionedOneToMany\Tag;
/**
*
* @group MergeVersionedOneToMany
*/
class MergeVersionedOneToManyTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_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);
}
}