ManyToManyBidirectionalAssociationTest.php 7.09 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
use Doctrine\ORM\Query;
9 10 11 12 13

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

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

    protected function setUp()
    {
        $this->useModelSet('ecommerce');
        parent::setUp();
        $this->firstProduct = new ECommerceProduct();
31
        $this->firstProduct->setName("First Product");
32
        $this->secondProduct = new ECommerceProduct();
33
        $this->secondProduct->setName("Second Product");
34 35 36 37 38 39 40 41 42 43
        $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
44
        $this->_em->persist($this->firstProduct);
45 46 47 48 49 50 51 52 53 54 55 56
        $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
57
        $this->_em->persist($this->firstProduct);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        $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();
73 74
        $this->assertLoadingOfInverseSide($categories); 
        $this->assertLoadingOfInverseSide($secondProduct->getCategories());
75 76 77 78 79
    }

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

88 89 90 91
        $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
        $this->assertLoadingOfInverseSide($categories);
94 95 96 97 98 99 100 101 102 103
    }

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

        $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');
104
        $products = $query->getResult();
105 106 107 108 109
        
        $this->assertEquals(2, count($products));
        $this->assertEquals(0, count($products[0]->getCategories()->unwrap()));
        $this->assertEquals(0, count($products[1]->getCategories()->unwrap()));
        
110
        $this->assertLoadingOfOwningSide($products);
111 112
    }

113
    private function _createLoadingFixture()
114 115 116 117 118
    {
        $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
119 120
        $this->_em->persist($this->firstProduct);
        $this->_em->persist($this->secondProduct);
121 122 123 124 125 126 127 128
        
        $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');
129
        //$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
130
        return $query->getResult();
131 132
    }
    
133 134 135
    public function assertLoadingOfOwningSide($products)
    {
        list ($firstProduct, $secondProduct) = $products;
136

137 138 139 140 141
        $firstProductCategories = $firstProduct->getCategories();
        $secondProductCategories = $secondProduct->getCategories();
        
        $this->assertEquals(2, count($firstProductCategories));
        $this->assertEquals(2, count($secondProductCategories));
142
        
143 144 145 146 147 148
        $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
        $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);
164 165 166 167 168 169 170 171
        
        $this->assertEquals(2, count($categories[0]->getProducts()));
        $this->assertTrue($categories[0]->getProducts()->get(0) instanceof ECommerceProduct);
        $this->assertTrue($categories[0]->getProducts()->get(1) instanceof ECommerceProduct);
        
        $this->assertEquals(2, count($categories[1]->getProducts()));
        $this->assertTrue($categories[1]->getProducts()->get(0) instanceof ECommerceProduct);
        $this->assertTrue($categories[1]->getProducts()->get(1) instanceof ECommerceProduct);
172
    }
173
}