-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test cases for both one-to-one and one-to-many cases.
- Loading branch information
Showing
5 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\DDC3699; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
|
||
/** | ||
* @Entity | ||
* @Table(name="ddc3699_child") | ||
*/ | ||
class DDC3699Child extends DDC3699Parent | ||
{ | ||
const CLASSNAME = __CLASS__; | ||
|
||
/** | ||
* @Id | ||
* @Column(type="integer") | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @Column(type="string") | ||
*/ | ||
protected $childField; | ||
|
||
/** | ||
* @OneToOne(targetEntity="DDC3699RelationOne", inversedBy="child") | ||
*/ | ||
protected $oneRelation; | ||
|
||
/** | ||
* @OneToMany(targetEntity="DDC3699RelationMany", mappedBy="child") | ||
*/ | ||
protected $relations; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId($id) | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getChildField() | ||
{ | ||
return $this->childField; | ||
} | ||
|
||
public function setChildField($childField) | ||
{ | ||
$this->childField = $childField; | ||
} | ||
|
||
public function getOneRelation() | ||
{ | ||
return $this->oneRelation; | ||
} | ||
|
||
public function setOneRelation($oneRelation) | ||
{ | ||
$this->oneRelation = $oneRelation; | ||
} | ||
|
||
public function hasRelation($relation) | ||
{ | ||
return $this->relations && $this->relations->contains($relation); | ||
} | ||
|
||
public function addRelation($relation) | ||
{ | ||
if (!$this->hasRelation($relation)) { | ||
$this->relations[] = $relation; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function removeRelation($relation) | ||
{ | ||
$this->relations->removeElement($relation); | ||
|
||
return $this; | ||
} | ||
|
||
public function getRelations() | ||
{ | ||
return $this->relations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\DDC3699; | ||
|
||
/** | ||
* @MappedSuperclass | ||
*/ | ||
abstract class DDC3699Parent | ||
{ | ||
const CLASSNAME = __CLASS__; | ||
|
||
/** | ||
* @Column(type="string") | ||
*/ | ||
protected $parentField; | ||
|
||
public function getParentField() | ||
{ | ||
return $this->parentField; | ||
} | ||
|
||
public function setParentField($parentField) | ||
{ | ||
$this->parentField = $parentField; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\DDC3699; | ||
|
||
/** | ||
* @Entity | ||
* @Table(name="ddc3699_relation_many") | ||
*/ | ||
class DDC3699RelationMany | ||
{ | ||
const CLASSNAME = __CLASS__; | ||
|
||
/** | ||
* @Id | ||
* @Column(type="integer") | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @ManyToOne(targetEntity="DDC3699Child", inversedBy="relations") | ||
* @JoinColumn(name="child", referencedColumnName="id") | ||
*/ | ||
protected $child; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId($id) | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getChild() | ||
{ | ||
return $this->child; | ||
} | ||
|
||
public function setChild($child) | ||
{ | ||
$this->child = $child; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\DDC3699; | ||
|
||
/** | ||
* @Entity | ||
* @Table(name="ddc3699_relation_one") | ||
*/ | ||
class DDC3699RelationOne | ||
{ | ||
const CLASSNAME = __CLASS__; | ||
|
||
/** | ||
* @Id | ||
* @Column(type="integer") | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @OneToOne(targetEntity="DDC3699Child", mappedBy="oneRelation") | ||
*/ | ||
protected $child; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId($id) | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getChild() | ||
{ | ||
return $this->child; | ||
} | ||
|
||
public function setChild($child) | ||
{ | ||
$this->child = $child; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
use Doctrine\Tests\Models\DDC3699\DDC3699Parent; | ||
use Doctrine\Tests\Models\DDC3699\DDC3699RelationOne; | ||
use Doctrine\Tests\Models\DDC3699\DDC3699RelationMany; | ||
use Doctrine\Tests\Models\DDC3699\DDC3699Child; | ||
|
||
/** | ||
* @group DDC-3699 | ||
*/ | ||
class DDC3597Test extends \Doctrine\Tests\OrmFunctionalTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
try { | ||
$this->_schemaTool->createSchema(array( | ||
$this->_em->getClassMetadata(DDC3699Parent::CLASSNAME), | ||
$this->_em->getClassMetadata(DDC3699RelationOne::CLASSNAME), | ||
$this->_em->getClassMetadata(DDC3699RelationMany::CLASSNAME), | ||
$this->_em->getClassMetadata(DDC3699Child::CLASSNAME), | ||
)); | ||
} catch (\Exception $e) { | ||
// should throw error on second because schema is already created | ||
} | ||
} | ||
|
||
private function createChild($id, $relationClass, $relationMethod) | ||
{ | ||
// element in DB | ||
$child = new DDC3699Child(); | ||
$child->setId($id); | ||
$child->setChildField('childValue'); | ||
$child->setParentField('parentValue'); | ||
|
||
$relation = new $relationClass(); | ||
$relation->setId($id); | ||
$relation->setChild($child); | ||
$child->$relationMethod($relation); | ||
|
||
$this->_em->persist($relation); | ||
$this->_em->persist($child); | ||
$this->_em->flush(); | ||
|
||
// detach | ||
$this->_em->detach($relation); | ||
$this->_em->detach($child); | ||
} | ||
|
||
/** | ||
* @group DDC-3699 | ||
*/ | ||
public function testMergeParentEntityFieldsOne() | ||
{ | ||
$id = 1; | ||
$this->createChild($id, DDC3699RelationOne::CLASSNAME, 'setOneRelation'); | ||
|
||
$unmanagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id); | ||
$this->_em->detach($unmanagedChild); | ||
|
||
// make it managed again | ||
$this->_em->find(DDC3699Child::CLASSNAME, $id); | ||
|
||
$unmanagedChild->setChildField('modifiedChildValue'); | ||
$unmanagedChild->setParentField('modifiedParentValue'); | ||
|
||
$mergedChild = $this->_em->merge($unmanagedChild); | ||
|
||
$this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue'); | ||
$this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue'); | ||
} | ||
|
||
/** | ||
* @group DDC-3699 | ||
*/ | ||
public function testMergeParentEntityFieldsMany() | ||
{ | ||
$id = 2; | ||
$this->createChild($id, DDC3699RelationMany::CLASSNAME, 'addRelation'); | ||
|
||
$unmanagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id); | ||
$this->_em->detach($unmanagedChild); | ||
|
||
// make it managed again | ||
$this->_em->find(DDC3699Child::CLASSNAME, $id); | ||
|
||
$unmanagedChild->setChildField('modifiedChildValue'); | ||
$unmanagedChild->setParentField('modifiedParentValue'); | ||
|
||
$mergedChild = $this->_em->merge($unmanagedChild); | ||
|
||
$this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue'); | ||
$this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue'); | ||
} | ||
} |