-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ManyToManyBidirectionalAssociationTest.php
202 lines (163 loc) · 9.17 KB
/
ManyToManyBidirectionalAssociationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Query;
require_once __DIR__ . '/../../TestInit.php';
/**
* Tests a bidirectional many-to-many association mapping (without inheritance).
* Owning side is ECommerceProduct, inverse side is ECommerceCategory.
*/
class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
{
protected $_firstField = 'product_id';
protected $_secondField = 'category_id';
protected $_table = 'ecommerce_products_categories';
private $firstProduct;
private $secondProduct;
private $firstCategory;
private $secondCategory;
protected function setUp()
{
$this->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);
}
}