Skip to content

Commit

Permalink
DDC-3699 - #1387 - simpifying tests, clarifying on test method names
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jul 15, 2015
1 parent 69ef75f commit 86abbb0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 189 deletions.
88 changes: 9 additions & 79 deletions tests/Doctrine/Tests/Models/DDC3699/DDC3699Child.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,20 @@

namespace Doctrine\Tests\Models\DDC3699;

use Doctrine\Common\Collections\ArrayCollection;

/**
* @Entity
* @Table(name="ddc3699_child")
*/
/** @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;
}
/** @Id @Column(type="integer") */
public $id;

public function removeRelation($relation)
{
$this->relations->removeElement($relation);
/** @Column(type="string") */
public $childField;

return $this;
}
/** @OneToOne(targetEntity="DDC3699RelationOne", inversedBy="child") */
public $oneRelation;

public function getRelations()
{
return $this->relations;
}
/** @OneToMany(targetEntity="DDC3699RelationMany", mappedBy="child") */
public $relations;
}
20 changes: 3 additions & 17 deletions tests/Doctrine/Tests/Models/DDC3699/DDC3699Parent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,11 @@

namespace Doctrine\Tests\Models\DDC3699;

/**
* @MappedSuperclass
*/
/** @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;
}
/** @Column(type="string") */
public $parentField;
}
34 changes: 4 additions & 30 deletions tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,9 @@ class DDC3699RelationMany
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @Column(type="integer")
*/
protected $id;
/** @Id @Column(type="integer") */
public $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;
}
/** @ManyToOne(targetEntity="DDC3699Child", inversedBy="relations") */
public $child;
}
33 changes: 4 additions & 29 deletions tests/Doctrine/Tests/Models/DDC3699/DDC3699RelationOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,9 @@ class DDC3699RelationOne
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @Column(type="integer")
*/
protected $id;
/** @Id @Column(type="integer") */
public $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;
}
/** @OneToOne(targetEntity="DDC3699Child", mappedBy="oneRelation") */
public $child;
}
84 changes: 50 additions & 34 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3699Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,87 @@ protected function setUp()
}
}

private function createChild($id, $relationClass, $relationMethod)
/**
* @group DDC-3699
*/
public function testMergingParentClassFieldsDoesNotStopMergingScalarFieldsForToOneUninitializedAssociations()
{
// element in DB
$id = 1;

$child = new DDC3699Child();
$child->setId($id);
$child->setChildField('childValue');
$child->setParentField('parentValue');

$relation = new $relationClass();
$relation->setId($id);
$relation->setChild($child);
$child->$relationMethod($relation);
$child->id = $id;
$child->childField = 'childValue';
$child->parentField = 'parentValue';

$relation = new DDC3699RelationOne();

$relation->id = $id;
$relation->child = $child ;
$child->oneRelation = $relation;

$this->_em->persist($relation);
$this->_em->persist($child);
$this->_em->flush();
$this->_em->clear();

// detach
$this->_em->detach($relation);
$this->_em->detach($child);
}

/**
* @group DDC-3699
*/
public function testMergeParentEntityFieldsOne()
{
$id = 1;
$this->createChild($id, DDC3699RelationOne::CLASSNAME, 'setOneRelation');
// fixtures loaded
/* @var $unManagedChild DDC3699Child */
$unManagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id);

$unmanagedChild = $this->_em->find(DDC3699Child::CLASSNAME, $id);
$this->_em->detach($unmanagedChild);
$this->_em->detach($unManagedChild);

// make it managed again
$this->_em->find(DDC3699Child::CLASSNAME, $id);

$unmanagedChild->setChildField('modifiedChildValue');
$unmanagedChild->setParentField('modifiedParentValue');
$unManagedChild->childField = 'modifiedChildValue';
$unManagedChild->parentField = 'modifiedParentValue';

$mergedChild = $this->_em->merge($unmanagedChild);
/* @var $mergedChild DDC3699Child */
$mergedChild = $this->_em->merge($unManagedChild);

$this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue');
$this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue');
$this->assertSame($mergedChild->childField, 'modifiedChildValue');
$this->assertSame($mergedChild->parentField, 'modifiedParentValue');
}

/**
* @group DDC-3699
*/
public function testMergeParentEntityFieldsMany()
public function testMergingParentClassFieldsDoesNotStopMergingScalarFieldsForToManyUninitializedAssociations()
{
$id = 2;
$this->createChild($id, DDC3699RelationMany::CLASSNAME, 'addRelation');

$child = new DDC3699Child();

$child->id = $id;
$child->childField = 'childValue';
$child->parentField = 'parentValue';

$relation = new DDC3699RelationMany();

$relation->id = $id;
$relation->child = $child ;
$child->relations[] = $relation;

$this->_em->persist($relation);
$this->_em->persist($child);
$this->_em->flush();
$this->_em->clear();

/* @var $unmanagedChild DDC3699Child */
$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');
$unmanagedChild->childField = 'modifiedChildValue';
$unmanagedChild->parentField = 'modifiedParentValue';

/* @var $mergedChild DDC3699Child */
$mergedChild = $this->_em->merge($unmanagedChild);

$this->assertEquals($mergedChild->getChildField(), 'modifiedChildValue');
$this->assertEquals($mergedChild->getParentField(), 'modifiedParentValue');
$this->assertSame($mergedChild->childField, 'modifiedChildValue');
$this->assertSame($mergedChild->parentField, 'modifiedParentValue');
}
}

0 comments on commit 86abbb0

Please sign in to comment.