Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / ManyToManyBidirectionalAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
6 use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
7 use Doctrine\ORM\Mapping\AssociationMapping;
8 use Doctrine\ORM\Query;
9
10 require_once __DIR__ . '/../../TestInit.php';
11
12 /**
13  * Tests a bidirectional many-to-many association mapping (without inheritance).
14  * Owning side is ECommerceProduct, inverse side is ECommerceCategory.
15  */
16 class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
17 {
18     protected $_firstField = 'product_id';
19     protected $_secondField = 'category_id';
20     protected $_table = 'ecommerce_products_categories';
21     private $firstProduct;
22     private $secondProduct;
23     private $firstCategory;
24     private $secondCategory;
25
26     protected function setUp()
27     {
28         $this->useModelSet('ecommerce');
29         parent::setUp();
30         $this->firstProduct = new ECommerceProduct();
31         $this->firstProduct->setName("First Product");
32         $this->secondProduct = new ECommerceProduct();
33         $this->secondProduct->setName("Second Product");
34         $this->firstCategory = new ECommerceCategory();
35         $this->firstCategory->setName("Business");
36         $this->secondCategory = new ECommerceCategory();
37         $this->secondCategory->setName("Home");
38     }
39
40     public function testSavesAManyToManyAssociationWithCascadeSaveSet()
41     {
42         $this->firstProduct->addCategory($this->firstCategory);
43         $this->firstProduct->addCategory($this->secondCategory);
44         $this->_em->persist($this->firstProduct);
45         $this->_em->flush();
46
47         $this->assertForeignKeysContain($this->firstProduct->getId(), $this->firstCategory->getId());
48         $this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId());
49     }
50
51     public function testRemovesAManyToManyAssociation()
52     {
53         $this->firstProduct->addCategory($this->firstCategory);
54         $this->firstProduct->addCategory($this->secondCategory);
55         $this->_em->persist($this->firstProduct);
56         $this->firstProduct->removeCategory($this->firstCategory);
57
58         $this->_em->flush();
59
60         $this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->firstCategory->getId());
61         $this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId());
62
63         $this->firstProduct->getCategories()->remove(1);
64         $this->_em->flush();
65
66         $this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->secondCategory->getId());
67     }
68
69     public function testEagerLoadFromInverseSideAndLazyLoadFromOwningSide()
70     {
71         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
72         $this->_createLoadingFixture();
73         $categories = $this->_findCategories();
74         $this->assertLazyLoadFromOwningSide($categories);
75     }
76
77     public function testEagerLoadFromOwningSideAndLazyLoadFromInverseSide()
78     {
79         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
80         $this->_createLoadingFixture();
81         $products = $this->_findProducts();
82         $this->assertLazyLoadFromInverseSide($products);
83     }
84
85     private function _createLoadingFixture()
86     {
87         $this->firstProduct->addCategory($this->firstCategory);
88         $this->firstProduct->addCategory($this->secondCategory);
89         $this->secondProduct->addCategory($this->firstCategory);
90         $this->secondProduct->addCategory($this->secondCategory);
91         $this->_em->persist($this->firstProduct);
92         $this->_em->persist($this->secondProduct);
93
94         $this->_em->flush();
95         $this->_em->clear();
96     }
97
98     protected function _findProducts()
99     {
100         $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');
101         //$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
102         $result = $query->getResult();
103         $this->assertEquals(2, count($result));
104         $cats1 = $result[0]->getCategories();
105         $cats2 = $result[1]->getCategories();
106         $this->assertTrue($cats1->isInitialized());
107         $this->assertTrue($cats2->isInitialized());
108         $this->assertFalse($cats1[0]->getProducts()->isInitialized());
109         $this->assertFalse($cats2[0]->getProducts()->isInitialized());
110
111         return $result;
112     }
113
114     protected function _findCategories()
115     {
116         $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');
117         //$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
118         $result = $query->getResult();
119         $this->assertEquals(2, count($result));
120         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $result[0]);
121         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $result[1]);
122         $prods1 = $result[0]->getProducts();
123         $prods2 = $result[1]->getProducts();
124         $this->assertTrue($prods1->isInitialized());
125         $this->assertTrue($prods2->isInitialized());
126
127         $this->assertFalse($prods1[0]->getCategories()->isInitialized());
128         $this->assertFalse($prods2[0]->getCategories()->isInitialized());
129
130         return $result;
131     }
132
133     public function assertLazyLoadFromInverseSide($products)
134     {
135         list ($firstProduct, $secondProduct) = $products;
136
137         $firstProductCategories = $firstProduct->getCategories();
138         $secondProductCategories = $secondProduct->getCategories();
139
140         $this->assertEquals(2, count($firstProductCategories));
141         $this->assertEquals(2, count($secondProductCategories));
142
143         $this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]);
144         $this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]);
145
146         $firstCategoryProducts = $firstProductCategories[0]->getProducts();
147         $secondCategoryProducts = $firstProductCategories[1]->getProducts();
148
149         $this->assertFalse($firstCategoryProducts->isInitialized());
150         $this->assertFalse($secondCategoryProducts->isInitialized());
151         $this->assertEquals(0, $firstCategoryProducts->unwrap()->count());
152         $this->assertEquals(0, $secondCategoryProducts->unwrap()->count());
153
154         $this->assertEquals(2, count($firstCategoryProducts)); // lazy-load
155         $this->assertTrue($firstCategoryProducts->isInitialized());
156         $this->assertFalse($secondCategoryProducts->isInitialized());
157         $this->assertEquals(2, count($secondCategoryProducts)); // lazy-load
158         $this->assertTrue($secondCategoryProducts->isInitialized());
159
160         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstCategoryProducts[0]);
161         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstCategoryProducts[1]);
162         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondCategoryProducts[0]);
163         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondCategoryProducts[1]);
164
165         $this->assertCollectionEquals($firstCategoryProducts, $secondCategoryProducts);
166     }
167
168     public function assertLazyLoadFromOwningSide($categories)
169     {
170         list ($firstCategory, $secondCategory) = $categories;
171
172         $firstCategoryProducts = $firstCategory->getProducts();
173         $secondCategoryProducts = $secondCategory->getProducts();
174
175         $this->assertEquals(2, count($firstCategoryProducts));
176         $this->assertEquals(2, count($secondCategoryProducts));
177
178         $this->assertTrue($firstCategoryProducts[0] === $secondCategoryProducts[0]);
179         $this->assertTrue($firstCategoryProducts[1] === $secondCategoryProducts[1]);
180
181         $firstProductCategories = $firstCategoryProducts[0]->getCategories();
182         $secondProductCategories = $firstCategoryProducts[1]->getCategories();
183
184         $this->assertFalse($firstProductCategories->isInitialized());
185         $this->assertFalse($secondProductCategories->isInitialized());
186         $this->assertEquals(0, $firstProductCategories->unwrap()->count());
187         $this->assertEquals(0, $secondProductCategories->unwrap()->count());
188
189         $this->assertEquals(2, count($firstProductCategories)); // lazy-load
190         $this->assertTrue($firstProductCategories->isInitialized());
191         $this->assertFalse($secondProductCategories->isInitialized());
192         $this->assertEquals(2, count($secondProductCategories)); // lazy-load
193         $this->assertTrue($secondProductCategories->isInitialized());
194
195         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $firstProductCategories[0]);
196         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $firstProductCategories[1]);
197         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $secondProductCategories[0]);
198         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $secondProductCategories[1]);
199
200         $this->assertCollectionEquals($firstProductCategories, $secondProductCategories);
201     }
202 }