From a812dab4d403f239e50391ee32828883a973da8a Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 9 Aug 2010 23:02:44 +0200 Subject: [PATCH] DDC-729 Add more tests for merging with many to many relations --- .../ORM/Functional/Ticket/DDC729Test.php | 104 +++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC729Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC729Test.php index b63dfdbce..c2be27b11 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC729Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC729Test.php @@ -51,6 +51,100 @@ class DDC729Test extends \Doctrine\Tests\OrmFunctionalTestCase $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId); $this->assertEquals(1, count($a->related)); } + + public function testUnidirectionalMergeManyToMany() + { + $a = new DDC729A(); + $b1 = new DDC729B(); + $b2 = new DDC729B(); + $a->related[] = $b1; + + $this->_em->persist($a); + $this->_em->persist($b1); + $this->_em->persist($b2); + $this->_em->flush(); + $this->_em->clear(); + $aId = $a->id; + + $a = new DDC729A(); + $a->id = $aId; + + $a = $this->_em->merge($a); + + $a->related->set(0, $this->_em->merge($b1)); + + $a->related->set(1, $this->_em->merge($b2)); + + $this->_em->flush(); + $this->_em->clear(); + + $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId); + $this->assertEquals(2, count($a->related)); + } + + public function testBidirectionalMergeManyToMany() + { + $a = new DDC729A(); + $b1 = new DDC729B(); + $b2 = new DDC729B(); + $a->related[] = $b1; + + $this->_em->persist($a); + $this->_em->persist($b1); + $this->_em->persist($b2); + $this->_em->flush(); + $this->_em->clear(); + $aId = $a->id; + + $a = new DDC729A(); + $a->id = $aId; + + $a = $this->_em->merge($a); + + $a->related->set(0, $this->_em->merge($b1)); + $b1->related->set(0, $a); + + $a->related->set(1, $this->_em->merge($b2)); + $b2->related->set(0, $a); + + $this->_em->flush(); + $this->_em->clear(); + + $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId); + $this->assertEquals(2, count($a->related)); + } + + public function testBidirectionalMultiMergeManyToMany() + { + $a = new DDC729A(); + $b1 = new DDC729B(); + $b2 = new DDC729B(); + $a->related[] = $b1; + + $this->_em->persist($a); + $this->_em->persist($b1); + $this->_em->persist($b2); + $this->_em->flush(); + $this->_em->clear(); + $aId = $a->id; + + $a = new DDC729A(); + $a->id = $aId; + + $a = $this->_em->merge($a); + + $a->related->set(0, $this->_em->merge($b1)); + $b1->related->set(0, $this->_em->merge($a)); + + $a->related->set(1, $this->_em->merge($b2)); + $b2->related->set(0, $this->_em->merge($a)); + + $this->_em->flush(); + $this->_em->clear(); + + $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId); + $this->assertEquals(2, count($a->related)); + } } /** @@ -61,7 +155,7 @@ class DDC729A /** @Id @GeneratedValue @Column(type="integer") */ public $id; - /** @ManyToMany(targetEntity="DDC729B") */ + /** @ManyToMany(targetEntity="DDC729B", inversedBy="related") */ public $related; public function __construct() @@ -77,4 +171,12 @@ class DDC729B { /** @Id @GeneratedValue @Column(type="integer") */ public $id; + + /** @ManyToMany(targetEntity="DDC729B", mappedBy="related") */ + public $related; + + public function __construct() + { + $this->related = new \Doctrine\Common\Collections\ArrayCollection(); + } }