1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\Tests\ORM\Functional; |
||
6 | |||
7 | use Doctrine\Common\Collections\Collection; |
||
8 | use Doctrine\Common\Collections\Criteria; |
||
9 | use Doctrine\Tests\Models\ECommerce\ECommerceFeature; |
||
10 | use Doctrine\Tests\Models\ECommerce\ECommerceProduct; |
||
11 | use Doctrine\Tests\OrmFunctionalTestCase; |
||
12 | use ProxyManager\Proxy\GhostObjectInterface; |
||
13 | |||
14 | /** |
||
15 | * Tests a bidirectional one-to-one association mapping (without inheritance). |
||
16 | */ |
||
17 | class OneToManyBidirectionalAssociationTest extends OrmFunctionalTestCase |
||
18 | { |
||
19 | private $product; |
||
20 | private $firstFeature; |
||
21 | private $secondFeature; |
||
22 | |||
23 | protected function setUp() |
||
24 | { |
||
25 | $this->useModelSet('ecommerce'); |
||
26 | parent::setUp(); |
||
27 | $this->product = new ECommerceProduct(); |
||
28 | $this->product->setName('Doctrine Cookbook'); |
||
29 | $this->firstFeature = new ECommerceFeature(); |
||
30 | $this->firstFeature->setDescription('Model writing tutorial'); |
||
31 | $this->secondFeature = new ECommerceFeature(); |
||
32 | $this->secondFeature->setDescription('Annotations examples'); |
||
33 | } |
||
34 | |||
35 | public function testSavesAOneToManyAssociationWithCascadeSaveSet() |
||
36 | { |
||
37 | $this->product->addFeature($this->firstFeature); |
||
38 | $this->product->addFeature($this->secondFeature); |
||
39 | $this->em->persist($this->product); |
||
40 | $this->em->flush(); |
||
41 | |||
42 | self::assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature); |
||
43 | self::assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature); |
||
44 | } |
||
45 | |||
46 | public function testSavesAnEmptyCollection() |
||
47 | { |
||
48 | $this->em->persist($this->product); |
||
49 | $this->em->flush(); |
||
50 | |||
51 | self::assertCount(0, $this->product->getFeatures()); |
||
52 | } |
||
53 | |||
54 | public function testDoesNotSaveAnInverseSideSet() |
||
55 | { |
||
56 | $this->product->brokenAddFeature($this->firstFeature); |
||
57 | $this->em->persist($this->product); |
||
58 | $this->em->flush(); |
||
59 | |||
60 | self::assertFeatureForeignKeyIs(null, $this->firstFeature); |
||
61 | } |
||
62 | |||
63 | public function testRemovesOneToOneAssociation() |
||
64 | { |
||
65 | $this->product->addFeature($this->firstFeature); |
||
66 | $this->product->addFeature($this->secondFeature); |
||
67 | $this->em->persist($this->product); |
||
68 | |||
69 | $this->product->removeFeature($this->firstFeature); |
||
70 | $this->em->flush(); |
||
71 | |||
72 | self::assertFeatureForeignKeyIs(null, $this->firstFeature); |
||
73 | self::assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature); |
||
74 | } |
||
75 | |||
76 | public function testEagerLoadsOneToManyAssociation() |
||
77 | { |
||
78 | $this->createFixture(); |
||
79 | $query = $this->em->createQuery('select p, f from Doctrine\Tests\Models\ECommerce\ECommerceProduct p join p.features f'); |
||
80 | $result = $query->getResult(); |
||
81 | $product = $result[0]; |
||
82 | |||
83 | $features = $product->getFeatures(); |
||
84 | |||
85 | self::assertInstanceOf(ECommerceFeature::class, $features[0]); |
||
86 | self::assertNotInstanceOf(GhostObjectInterface::class, $features[0]->getProduct()); |
||
87 | self::assertSame($product, $features[0]->getProduct()); |
||
88 | self::assertEquals('Model writing tutorial', $features[0]->getDescription()); |
||
89 | self::assertInstanceOf(ECommerceFeature::class, $features[1]); |
||
90 | self::assertSame($product, $features[1]->getProduct()); |
||
91 | self::assertNotInstanceOf(GhostObjectInterface::class, $features[1]->getProduct()); |
||
92 | self::assertEquals('Annotations examples', $features[1]->getDescription()); |
||
93 | } |
||
94 | |||
95 | public function testLazyLoadsObjectsOnTheOwningSide() |
||
96 | { |
||
97 | $this->createFixture(); |
||
98 | |||
99 | $query = $this->em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p'); |
||
100 | $result = $query->getResult(); |
||
101 | $product = $result[0]; |
||
102 | $features = $product->getFeatures(); |
||
103 | |||
104 | self::assertFalse($features->isInitialized()); |
||
105 | self::assertInstanceOf(ECommerceFeature::class, $features[0]); |
||
106 | self::assertTrue($features->isInitialized()); |
||
107 | self::assertSame($product, $features[0]->getProduct()); |
||
108 | self::assertEquals('Model writing tutorial', $features[0]->getDescription()); |
||
109 | self::assertInstanceOf(ECommerceFeature::class, $features[1]); |
||
110 | self::assertSame($product, $features[1]->getProduct()); |
||
111 | self::assertEquals('Annotations examples', $features[1]->getDescription()); |
||
112 | } |
||
113 | |||
114 | public function testLazyLoadsObjectsOnTheInverseSide() |
||
115 | { |
||
116 | $this->createFixture(); |
||
117 | |||
118 | $query = $this->em->createQuery('select f from Doctrine\Tests\Models\ECommerce\ECommerceFeature f'); |
||
119 | $features = $query->getResult(); |
||
120 | |||
121 | /* @var $product GhostObjectInterface|ECommerceFeature */ |
||
122 | $product = $features[0]->getProduct(); |
||
123 | self::assertInstanceOf(GhostObjectInterface::class, $product); |
||
124 | self::assertInstanceOf(ECommerceProduct::class, $product); |
||
125 | self::assertFalse($product->isProxyInitialized()); |
||
126 | self::assertSame('Doctrine Cookbook', $product->getName()); |
||
127 | self::assertTrue($product->isProxyInitialized()); |
||
128 | } |
||
129 | |||
130 | public function testLazyLoadsObjectsOnTheInverseSide2() |
||
131 | { |
||
132 | //$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
||
0 ignored issues
–
show
|
|||
133 | $this->createFixture(); |
||
134 | |||
135 | $query = $this->em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p'); |
||
136 | $features = $query->getResult(); |
||
137 | |||
138 | $product = $features[0]->getProduct(); |
||
139 | self::assertNotInstanceOf(GhostObjectInterface::class, $product); |
||
140 | self::assertInstanceOf(ECommerceProduct::class, $product); |
||
141 | self::assertSame('Doctrine Cookbook', $product->getName()); |
||
142 | |||
143 | self::assertFalse($product->getFeatures()->isInitialized()); |
||
144 | |||
145 | // This would trigger lazy-load |
||
146 | //self::assertEquals(2, $product->getFeatures()->count()); |
||
147 | //self::assertTrue($product->getFeatures()->contains($features[0])); |
||
148 | //self::assertTrue($product->getFeatures()->contains($features[1])); |
||
149 | |||
150 | //$this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
77% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them.
Loading history...
|
|||
151 | } |
||
152 | |||
153 | public function testJoinFromOwningSide() |
||
154 | { |
||
155 | $query = $this->em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p'); |
||
156 | $features = $query->getResult(); |
||
157 | self::assertCount(0, $features); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @group DDC-1637 |
||
162 | */ |
||
163 | public function testMatching() |
||
164 | { |
||
165 | $this->createFixture(); |
||
166 | |||
167 | $product = $this->em->find(ECommerceProduct::class, $this->product->getId()); |
||
168 | $features = $product->getFeatures(); |
||
169 | |||
170 | $results = $features->matching(new Criteria( |
||
171 | Criteria::expr()->eq('description', 'Model writing tutorial') |
||
172 | )); |
||
173 | |||
174 | self::assertInstanceOf(Collection::class, $results); |
||
175 | self::assertCount(1, $results); |
||
176 | |||
177 | $results = $features->matching(new Criteria()); |
||
178 | |||
179 | self::assertInstanceOf(Collection::class, $results); |
||
180 | self::assertCount(2, $results); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @group DDC-2340 |
||
185 | */ |
||
186 | public function testMatchingOnDirtyCollection() |
||
187 | { |
||
188 | $this->createFixture(); |
||
189 | |||
190 | $product = $this->em->find(ECommerceProduct::class, $this->product->getId()); |
||
191 | |||
192 | $thirdFeature = new ECommerceFeature(); |
||
193 | $thirdFeature->setDescription('Model writing tutorial'); |
||
194 | |||
195 | $features = $product->getFeatures(); |
||
196 | $features->add($thirdFeature); |
||
197 | |||
198 | $results = $features->matching(new Criteria( |
||
199 | Criteria::expr()->eq('description', 'Model writing tutorial') |
||
200 | )); |
||
201 | |||
202 | self::assertCount(2, $results); |
||
203 | } |
||
204 | |||
205 | public function testMatchingBis() |
||
206 | { |
||
207 | $this->createFixture(); |
||
208 | |||
209 | $product = $this->em->find(ECommerceProduct::class, $this->product->getId()); |
||
210 | $features = $product->getFeatures(); |
||
211 | |||
212 | $thirdFeature = new ECommerceFeature(); |
||
213 | $thirdFeature->setDescription('Third feature'); |
||
214 | $product->addFeature($thirdFeature); |
||
215 | |||
216 | $results = $features->matching(new Criteria( |
||
217 | Criteria::expr()->eq('description', 'Third feature') |
||
218 | )); |
||
219 | |||
220 | self::assertInstanceOf(Collection::class, $results); |
||
221 | self::assertCount(1, $results); |
||
222 | |||
223 | $results = $features->matching(new Criteria()); |
||
224 | |||
225 | self::assertInstanceOf(Collection::class, $results); |
||
226 | self::assertCount(3, $results); |
||
227 | } |
||
228 | |||
229 | private function createFixture() |
||
230 | { |
||
231 | $this->product->addFeature($this->firstFeature); |
||
232 | $this->product->addFeature($this->secondFeature); |
||
233 | $this->em->persist($this->product); |
||
234 | |||
235 | $this->em->flush(); |
||
236 | $this->em->clear(); |
||
237 | } |
||
238 | |||
239 | public function assertFeatureForeignKeyIs($value, ECommerceFeature $feature) |
||
240 | { |
||
241 | $foreignKey = $this->em->getConnection()->executeQuery( |
||
242 | 'SELECT product_id FROM ecommerce_features WHERE id=?', |
||
243 | [$feature->getId()] |
||
244 | )->fetchColumn(); |
||
245 | self::assertEquals($value, $foreignKey); |
||
246 | } |
||
247 | } |
||
248 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.