Skip to content

Commit

Permalink
Added test cases for both one-to-one and one-to-many cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenard Palko authored and Ocramius committed Jul 15, 2015
1 parent c68edec commit 69ef75f
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php
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;
}
}
26 changes: 26 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php
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 tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php
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 tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php
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 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php
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');
}
}

0 comments on commit 69ef75f

Please sign in to comment.