ManyToManyBidirectionalAssociationTest.php 6.62 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
7
use Doctrine\ORM\Mapping\AssociationMapping;
8 9 10 11 12

require_once __DIR__ . '/../../TestInit.php';

/**
 * Tests a bidirectional many-to-many association mapping (without inheritance).
13
 * Owning side is ECommerceProduct, inverse side is ECommerceCategory.
14 15 16
 */
class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
{
17 18 19
    protected $_firstField = 'product_id';
    protected $_secondField = 'category_id';
    protected $_table = 'ecommerce_products_categories';
20 21 22 23 24 25 26 27 28 29
    private $firstProduct;
    private $secondProduct;
    private $firstCategory;
    private $secondCategory;

    protected function setUp()
    {
        $this->useModelSet('ecommerce');
        parent::setUp();
        $this->firstProduct = new ECommerceProduct();
30
        $this->firstProduct->setName("First Product");
31
        $this->secondProduct = new ECommerceProduct();
32
        $this->secondProduct->setName("Second Product");
33 34 35 36 37 38 39 40 41 42
        $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);
romanb's avatar
romanb committed
43
        $this->_em->persist($this->firstProduct);
44 45 46 47 48 49 50 51 52 53 54 55
        $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);
romanb's avatar
romanb committed
56
        $this->_em->persist($this->firstProduct);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        $this->firstProduct->removeCategory($this->firstCategory);

        $this->_em->flush();

        $this->assertForeignKeysNotContain($this->firstProduct->getId(),
                                   $this->firstCategory->getId());
        $this->assertForeignKeysContain($this->firstProduct->getId(),
                                   $this->secondCategory->getId());
    }

    public function testEagerLoadsInverseSide()
    {
        $this->_createLoadingFixture();
        list ($firstProduct, $secondProduct) = $this->_findProducts();
        $categories = $firstProduct->getCategories();
72 73
        $this->assertLoadingOfInverseSide($categories); 
        $this->assertLoadingOfInverseSide($secondProduct->getCategories());
74 75 76 77 78
    }

    public function testEagerLoadsOwningSide()
    {
        $this->_createLoadingFixture();
79 80 81 82 83 84 85
        $products = $this->_findProducts();
        $this->assertLoadingOfOwningSide($products); 
    }
    
    public function testLazyLoadsCollectionOnTheInverseSide()
    {
        $this->_createLoadingFixture();
86

87 88 89 90 91
        $this->_em->getConfiguration()->setAllowPartialObjects(false);
        $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCategory');
        $metadata->getAssociationMapping('products')->fetchMode = AssociationMapping::FETCH_LAZY;

        $query = $this->_em->createQuery('SELECT c FROM Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id');
92
        $categories = $query->getResult();
93 94 95 96 97 98 99 100 101 102 103 104
        $this->assertLoadingOfInverseSide($categories); 
    }

    public function testLazyLoadsCollectionOnTheOwningSide()
    {
        $this->_createLoadingFixture();

        $this->_em->getConfiguration()->setAllowPartialObjects(false);
        $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
        $metadata->getAssociationMapping('categories')->fetchMode = AssociationMapping::FETCH_LAZY;

        $query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p order by p.id');
105
        $products = $query->getResult();
106 107 108 109 110
        
        $this->assertEquals(2, count($products));
        $this->assertEquals(0, count($products[0]->getCategories()->unwrap()));
        $this->assertEquals(0, count($products[1]->getCategories()->unwrap()));
        
111
        $this->assertLoadingOfOwningSide($products); 
112 113
    }

114 115

    private function _createLoadingFixture()
116 117 118 119 120
    {
        $this->firstProduct->addCategory($this->firstCategory);
        $this->firstProduct->addCategory($this->secondCategory);
        $this->secondProduct->addCategory($this->firstCategory);
        $this->secondProduct->addCategory($this->secondCategory);
romanb's avatar
romanb committed
121 122
        $this->_em->persist($this->firstProduct);
        $this->_em->persist($this->secondProduct);
123 124 125 126 127 128 129 130
        
        $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');
131
        return $query->getResult();
132 133
    }
    
134 135 136
    public function assertLoadingOfOwningSide($products)
    {
        list ($firstProduct, $secondProduct) = $products;
137

138 139 140 141 142 143 144 145 146 147 148
        $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();
        
149 150
        $this->assertEquals(2, count($firstCategoryProducts));
        $this->assertEquals(2, count($secondCategoryProducts));
151
        
152 153 154 155 156 157 158 159 160 161 162 163 164
        $this->assertTrue($firstCategoryProducts[0] instanceof ECommerceProduct);
        $this->assertTrue($firstCategoryProducts[1] instanceof ECommerceProduct);
        $this->assertTrue($secondCategoryProducts[0] instanceof ECommerceProduct);
        $this->assertTrue($secondCategoryProducts[1] instanceof ECommerceProduct);
        
        $this->assertCollectionEquals($firstCategoryProducts, $secondCategoryProducts);
    }

    public function assertLoadingOfInverseSide($categories)
    {
        $this->assertTrue($categories[0] instanceof ECommerceCategory);
        $this->assertTrue($categories[1] instanceof ECommerceCategory);
    }
165
}