X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=vendor%2Fdoctrine%2Form%2Ftests%2FDoctrine%2FTests%2FORM%2FFunctional%2FManyToManyBidirectionalAssociationTest.php;fp=vendor%2Fdoctrine%2Form%2Ftests%2FDoctrine%2FTests%2FORM%2FFunctional%2FManyToManyBidirectionalAssociationTest.php;h=cfe9f1e83233bbebed2524b271604a89c8f96575;hb=8b04b2d11798dee4f3e1358e4f43e97a6df851f6;hp=0000000000000000000000000000000000000000;hpb=73568cf05a785a45f94ca3f2351d9e07bf917958;p=zf2.biz%2Fapplication_blanche.git diff --git a/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/ManyToManyBidirectionalAssociationTest.php b/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/ManyToManyBidirectionalAssociationTest.php new file mode 100644 index 0000000..cfe9f1e --- /dev/null +++ b/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/ManyToManyBidirectionalAssociationTest.php @@ -0,0 +1,202 @@ +useModelSet('ecommerce'); + parent::setUp(); + $this->firstProduct = new ECommerceProduct(); + $this->firstProduct->setName("First Product"); + $this->secondProduct = new ECommerceProduct(); + $this->secondProduct->setName("Second Product"); + $this->firstCategory = new ECommerceCategory(); + $this->firstCategory->setName("Business"); + $this->secondCategory = new ECommerceCategory(); + $this->secondCategory->setName("Home"); + } + + public function testSavesAManyToManyAssociationWithCascadeSaveSet() + { + $this->firstProduct->addCategory($this->firstCategory); + $this->firstProduct->addCategory($this->secondCategory); + $this->_em->persist($this->firstProduct); + $this->_em->flush(); + + $this->assertForeignKeysContain($this->firstProduct->getId(), $this->firstCategory->getId()); + $this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId()); + } + + public function testRemovesAManyToManyAssociation() + { + $this->firstProduct->addCategory($this->firstCategory); + $this->firstProduct->addCategory($this->secondCategory); + $this->_em->persist($this->firstProduct); + $this->firstProduct->removeCategory($this->firstCategory); + + $this->_em->flush(); + + $this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->firstCategory->getId()); + $this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId()); + + $this->firstProduct->getCategories()->remove(1); + $this->_em->flush(); + + $this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->secondCategory->getId()); + } + + public function testEagerLoadFromInverseSideAndLazyLoadFromOwningSide() + { + //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); + $this->_createLoadingFixture(); + $categories = $this->_findCategories(); + $this->assertLazyLoadFromOwningSide($categories); + } + + public function testEagerLoadFromOwningSideAndLazyLoadFromInverseSide() + { + //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); + $this->_createLoadingFixture(); + $products = $this->_findProducts(); + $this->assertLazyLoadFromInverseSide($products); + } + + private function _createLoadingFixture() + { + $this->firstProduct->addCategory($this->firstCategory); + $this->firstProduct->addCategory($this->secondCategory); + $this->secondProduct->addCategory($this->firstCategory); + $this->secondProduct->addCategory($this->secondCategory); + $this->_em->persist($this->firstProduct); + $this->_em->persist($this->secondProduct); + + $this->_em->flush(); + $this->_em->clear(); + } + + protected function _findProducts() + { + $query = $this->_em->createQuery('SELECT p, c FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p LEFT JOIN p.categories c ORDER BY p.id, c.id'); + //$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true); + $result = $query->getResult(); + $this->assertEquals(2, count($result)); + $cats1 = $result[0]->getCategories(); + $cats2 = $result[1]->getCategories(); + $this->assertTrue($cats1->isInitialized()); + $this->assertTrue($cats2->isInitialized()); + $this->assertFalse($cats1[0]->getProducts()->isInitialized()); + $this->assertFalse($cats2[0]->getProducts()->isInitialized()); + + return $result; + } + + protected function _findCategories() + { + $query = $this->_em->createQuery('SELECT c, p FROM Doctrine\Tests\Models\ECommerce\ECommerceCategory c LEFT JOIN c.products p ORDER BY c.id, p.id'); + //$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true); + $result = $query->getResult(); + $this->assertEquals(2, count($result)); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $result[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $result[1]); + $prods1 = $result[0]->getProducts(); + $prods2 = $result[1]->getProducts(); + $this->assertTrue($prods1->isInitialized()); + $this->assertTrue($prods2->isInitialized()); + + $this->assertFalse($prods1[0]->getCategories()->isInitialized()); + $this->assertFalse($prods2[0]->getCategories()->isInitialized()); + + return $result; + } + + public function assertLazyLoadFromInverseSide($products) + { + list ($firstProduct, $secondProduct) = $products; + + $firstProductCategories = $firstProduct->getCategories(); + $secondProductCategories = $secondProduct->getCategories(); + + $this->assertEquals(2, count($firstProductCategories)); + $this->assertEquals(2, count($secondProductCategories)); + + $this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]); + $this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]); + + $firstCategoryProducts = $firstProductCategories[0]->getProducts(); + $secondCategoryProducts = $firstProductCategories[1]->getProducts(); + + $this->assertFalse($firstCategoryProducts->isInitialized()); + $this->assertFalse($secondCategoryProducts->isInitialized()); + $this->assertEquals(0, $firstCategoryProducts->unwrap()->count()); + $this->assertEquals(0, $secondCategoryProducts->unwrap()->count()); + + $this->assertEquals(2, count($firstCategoryProducts)); // lazy-load + $this->assertTrue($firstCategoryProducts->isInitialized()); + $this->assertFalse($secondCategoryProducts->isInitialized()); + $this->assertEquals(2, count($secondCategoryProducts)); // lazy-load + $this->assertTrue($secondCategoryProducts->isInitialized()); + + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstCategoryProducts[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstCategoryProducts[1]); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondCategoryProducts[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondCategoryProducts[1]); + + $this->assertCollectionEquals($firstCategoryProducts, $secondCategoryProducts); + } + + public function assertLazyLoadFromOwningSide($categories) + { + list ($firstCategory, $secondCategory) = $categories; + + $firstCategoryProducts = $firstCategory->getProducts(); + $secondCategoryProducts = $secondCategory->getProducts(); + + $this->assertEquals(2, count($firstCategoryProducts)); + $this->assertEquals(2, count($secondCategoryProducts)); + + $this->assertTrue($firstCategoryProducts[0] === $secondCategoryProducts[0]); + $this->assertTrue($firstCategoryProducts[1] === $secondCategoryProducts[1]); + + $firstProductCategories = $firstCategoryProducts[0]->getCategories(); + $secondProductCategories = $firstCategoryProducts[1]->getCategories(); + + $this->assertFalse($firstProductCategories->isInitialized()); + $this->assertFalse($secondProductCategories->isInitialized()); + $this->assertEquals(0, $firstProductCategories->unwrap()->count()); + $this->assertEquals(0, $secondProductCategories->unwrap()->count()); + + $this->assertEquals(2, count($firstProductCategories)); // lazy-load + $this->assertTrue($firstProductCategories->isInitialized()); + $this->assertFalse($secondProductCategories->isInitialized()); + $this->assertEquals(2, count($secondProductCategories)); // lazy-load + $this->assertTrue($secondProductCategories->isInitialized()); + + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $firstProductCategories[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $firstProductCategories[1]); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $secondProductCategories[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $secondProductCategories[1]); + + $this->assertCollectionEquals($firstProductCategories, $secondProductCategories); + } +}