1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\Tests\ORM\Functional; |
||
6 | |||
7 | use Doctrine\Common\Collections\ArrayCollection; |
||
8 | use Doctrine\Common\Collections\Criteria; |
||
9 | use Doctrine\DBAL\Connection; |
||
10 | use Doctrine\DBAL\LockMode; |
||
11 | use Doctrine\ORM\EntityRepository; |
||
12 | use Doctrine\ORM\OptimisticLockException; |
||
13 | use Doctrine\ORM\ORMException; |
||
14 | use Doctrine\ORM\Query; |
||
15 | use Doctrine\ORM\TransactionRequiredException; |
||
16 | use Doctrine\Tests\Models\CMS\CmsAddress; |
||
17 | use Doctrine\Tests\Models\CMS\CmsEmail; |
||
18 | use Doctrine\Tests\Models\CMS\CmsUser; |
||
19 | use Doctrine\Tests\Models\DDC753\DDC753CustomRepository; |
||
20 | use Doctrine\Tests\Models\DDC753\DDC753DefaultRepository; |
||
21 | use Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository; |
||
22 | use Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository; |
||
23 | use Doctrine\Tests\Models\DDC753\DDC753InvalidRepository; |
||
24 | use Doctrine\Tests\OrmFunctionalTestCase; |
||
25 | |||
26 | /** |
||
27 | * @author robo |
||
28 | */ |
||
29 | class EntityRepositoryTest extends OrmFunctionalTestCase |
||
30 | { |
||
31 | protected function setUp() |
||
32 | { |
||
33 | $this->useModelSet('cms'); |
||
34 | parent::setUp(); |
||
35 | } |
||
36 | |||
37 | public function tearDown() |
||
38 | { |
||
39 | if ($this->em) { |
||
40 | $this->em->getConfiguration()->setEntityNamespaces([]); |
||
41 | } |
||
42 | parent::tearDown(); |
||
43 | } |
||
44 | |||
45 | public function loadFixture() |
||
46 | { |
||
47 | $user = new CmsUser; |
||
48 | $user->name = 'Roman'; |
||
49 | $user->username = 'romanb'; |
||
50 | $user->status = 'freak'; |
||
51 | $this->em->persist($user); |
||
52 | |||
53 | $user2 = new CmsUser; |
||
54 | $user2->name = 'Guilherme'; |
||
55 | $user2->username = 'gblanco'; |
||
56 | $user2->status = 'dev'; |
||
57 | $this->em->persist($user2); |
||
58 | |||
59 | $user3 = new CmsUser; |
||
60 | $user3->name = 'Benjamin'; |
||
61 | $user3->username = 'beberlei'; |
||
62 | $user3->status = null; |
||
63 | $this->em->persist($user3); |
||
64 | |||
65 | $user4 = new CmsUser; |
||
66 | $user4->name = 'Alexander'; |
||
67 | $user4->username = 'asm89'; |
||
68 | $user4->status = 'dev'; |
||
69 | $this->em->persist($user4); |
||
70 | |||
71 | $this->em->flush(); |
||
72 | |||
73 | $user1Id = $user->getId(); |
||
74 | |||
75 | unset($user, $user2, $user3, $user4); |
||
76 | |||
77 | $this->em->clear(); |
||
78 | |||
79 | return $user1Id; |
||
80 | } |
||
81 | |||
82 | public function loadAssociatedFixture() |
||
83 | { |
||
84 | $address = new CmsAddress(); |
||
85 | $address->city = "Berlin"; |
||
86 | $address->country = "Germany"; |
||
87 | $address->street = "Foostreet"; |
||
88 | $address->zip = "12345"; |
||
89 | |||
90 | $user = new CmsUser(); |
||
91 | $user->name = 'Roman'; |
||
92 | $user->username = 'romanb'; |
||
93 | $user->status = 'freak'; |
||
94 | $user->setAddress($address); |
||
95 | |||
96 | $this->em->persist($user); |
||
97 | $this->em->persist($address); |
||
98 | $this->em->flush(); |
||
99 | $this->em->clear(); |
||
100 | |||
101 | return [$user->id, $address->id]; |
||
102 | } |
||
103 | |||
104 | public function loadFixtureUserEmail() |
||
105 | { |
||
106 | $user1 = new CmsUser(); |
||
107 | $user2 = new CmsUser(); |
||
108 | $user3 = new CmsUser(); |
||
109 | |||
110 | $email1 = new CmsEmail(); |
||
111 | $email2 = new CmsEmail(); |
||
112 | $email3 = new CmsEmail(); |
||
113 | |||
114 | $user1->name = 'Test 1'; |
||
115 | $user1->username = 'test1'; |
||
116 | $user1->status = 'active'; |
||
117 | |||
118 | $user2->name = 'Test 2'; |
||
119 | $user2->username = 'test2'; |
||
120 | $user2->status = 'active'; |
||
121 | |||
122 | $user3->name = 'Test 3'; |
||
123 | $user3->username = 'test3'; |
||
124 | $user3->status = 'active'; |
||
125 | |||
126 | $email1->email = '[email protected]'; |
||
127 | $email2->email = '[email protected]'; |
||
128 | $email3->email = '[email protected]'; |
||
129 | |||
130 | $user1->setEmail($email1); |
||
131 | $user2->setEmail($email2); |
||
132 | $user3->setEmail($email3); |
||
133 | |||
134 | $this->em->persist($user1); |
||
135 | $this->em->persist($user2); |
||
136 | $this->em->persist($user3); |
||
137 | |||
138 | $this->em->persist($email1); |
||
139 | $this->em->persist($email2); |
||
140 | $this->em->persist($email3); |
||
141 | |||
142 | $this->em->flush(); |
||
143 | $this->em->clear(); |
||
144 | |||
145 | return [$user1, $user2, $user3]; |
||
146 | } |
||
147 | |||
148 | public function buildUser($name, $username, $status, $address) |
||
149 | { |
||
150 | $user = new CmsUser(); |
||
151 | $user->name = $name; |
||
152 | $user->username = $username; |
||
153 | $user->status = $status; |
||
154 | $user->setAddress($address); |
||
155 | |||
156 | $this->em->persist($user); |
||
157 | $this->em->flush(); |
||
158 | |||
159 | return $user; |
||
160 | } |
||
161 | |||
162 | public function buildAddress($country, $city, $street, $zip) |
||
163 | { |
||
164 | $address = new CmsAddress(); |
||
165 | $address->country = $country; |
||
166 | $address->city = $city; |
||
167 | $address->street = $street; |
||
168 | $address->zip = $zip; |
||
169 | |||
170 | $this->em->persist($address); |
||
171 | $this->em->flush(); |
||
172 | |||
173 | return $address; |
||
174 | } |
||
175 | |||
176 | public function testBasicFind() |
||
177 | { |
||
178 | $user1Id = $this->loadFixture(); |
||
179 | $repos = $this->em->getRepository(CmsUser::class); |
||
180 | |||
181 | $user = $repos->find($user1Id); |
||
182 | self::assertInstanceOf(CmsUser::class, $user); |
||
183 | self::assertEquals('Roman', $user->name); |
||
184 | self::assertEquals('freak', $user->status); |
||
185 | } |
||
186 | |||
187 | public function testFindByField() |
||
188 | { |
||
189 | $user1Id = $this->loadFixture(); |
||
190 | $repos = $this->em->getRepository(CmsUser::class); |
||
191 | |||
192 | $users = $repos->findBy(['status' => 'dev']); |
||
193 | self::assertCount(2, $users); |
||
194 | self::assertInstanceOf(CmsUser::class, $users[0]); |
||
195 | self::assertEquals('Guilherme', $users[0]->name); |
||
196 | self::assertEquals('dev', $users[0]->status); |
||
197 | } |
||
198 | |||
199 | public function testFindByAssociationWithIntegerAsParameter() |
||
200 | { |
||
201 | $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456'); |
||
202 | $user1 = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1); |
||
203 | |||
204 | $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321'); |
||
205 | $user2 = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2); |
||
206 | |||
207 | $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654'); |
||
208 | $user3 = $this->buildUser('Jonathan', 'jwage', 'dev', $address3); |
||
209 | |||
210 | unset($address1, $address2, $address3); |
||
211 | |||
212 | $this->em->clear(); |
||
213 | |||
214 | $repository = $this->em->getRepository(CmsAddress::class); |
||
215 | $addresses = $repository->findBy(['user' => [$user1->getId(), $user2->getId()]]); |
||
216 | |||
217 | self::assertCount(2, $addresses); |
||
218 | self::assertInstanceOf(CmsAddress::class, $addresses[0]); |
||
219 | } |
||
220 | |||
221 | public function testFindByAssociationWithObjectAsParameter() |
||
222 | { |
||
223 | $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456'); |
||
224 | $user1 = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1); |
||
225 | |||
226 | $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321'); |
||
227 | $user2 = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2); |
||
228 | |||
229 | $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654'); |
||
230 | $user3 = $this->buildUser('Jonathan', 'jwage', 'dev', $address3); |
||
231 | |||
232 | unset($address1, $address2, $address3); |
||
233 | |||
234 | $this->em->clear(); |
||
235 | |||
236 | $repository = $this->em->getRepository(CmsAddress::class); |
||
237 | $addresses = $repository->findBy(['user' => [$user1, $user2]]); |
||
238 | |||
239 | self::assertCount(2, $addresses); |
||
240 | self::assertInstanceOf(CmsAddress::class, $addresses[0]); |
||
241 | } |
||
242 | |||
243 | public function testFindFieldByMagicCall() |
||
244 | { |
||
245 | $user1Id = $this->loadFixture(); |
||
246 | $repos = $this->em->getRepository(CmsUser::class); |
||
247 | |||
248 | $users = $repos->findByStatus('dev'); |
||
249 | self::assertCount(2, $users); |
||
250 | self::assertInstanceOf(CmsUser::class, $users[0]); |
||
251 | self::assertEquals('Guilherme', $users[0]->name); |
||
252 | self::assertEquals('dev', $users[0]->status); |
||
253 | } |
||
254 | |||
255 | public function testFindAll() |
||
256 | { |
||
257 | $user1Id = $this->loadFixture(); |
||
258 | $repos = $this->em->getRepository(CmsUser::class); |
||
259 | |||
260 | $users = $repos->findAll(); |
||
261 | self::assertCount(4, $users); |
||
262 | } |
||
263 | |||
264 | public function testFindByAlias() |
||
265 | { |
||
266 | $user1Id = $this->loadFixture(); |
||
267 | $repos = $this->em->getRepository(CmsUser::class); |
||
268 | |||
269 | $this->em->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS'); |
||
270 | |||
271 | $repos = $this->em->getRepository('CMS:CmsUser'); |
||
272 | |||
273 | $users = $repos->findAll(); |
||
274 | self::assertCount(4, $users); |
||
275 | } |
||
276 | |||
277 | public function testCount() |
||
278 | { |
||
279 | $this->loadFixture(); |
||
280 | $repos = $this->em->getRepository(CmsUser::class); |
||
281 | |||
282 | $userCount = $repos->count([]); |
||
283 | self::assertSame(4, $userCount); |
||
284 | |||
285 | $userCount = $repos->count(['status' => 'dev']); |
||
286 | self::assertSame(2, $userCount); |
||
287 | |||
288 | $userCount = $repos->count(['status' => 'nonexistent']); |
||
289 | self::assertSame(0, $userCount); |
||
290 | } |
||
291 | |||
292 | public function testCountBy() |
||
293 | { |
||
294 | $this->loadFixture(); |
||
295 | $repos = $this->em->getRepository(CmsUser::class); |
||
296 | |||
297 | $userCount = $repos->countByStatus('dev'); |
||
298 | self::assertSame(2, $userCount); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @expectedException \Doctrine\ORM\ORMException |
||
303 | */ |
||
304 | public function testExceptionIsThrownWhenCallingFindByWithoutParameter() |
||
305 | { |
||
306 | $this->em->getRepository(CmsUser::class) |
||
307 | ->findByStatus(); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @expectedException \Doctrine\ORM\ORMException |
||
312 | */ |
||
313 | public function testExceptionIsThrownWhenUsingInvalidFieldName() |
||
314 | { |
||
315 | $this->em->getRepository(CmsUser::class) |
||
316 | ->findByThisFieldDoesNotExist('testvalue'); |
||
0 ignored issues
–
show
|
|||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @group locking |
||
321 | * @group DDC-178 |
||
322 | */ |
||
323 | public function testPessimisticReadLockWithoutTransaction_ThrowsException() |
||
324 | { |
||
325 | $this->expectException(TransactionRequiredException::class); |
||
326 | |||
327 | $this->em->getRepository(CmsUser::class) |
||
328 | ->find(1, LockMode::PESSIMISTIC_READ); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @group locking |
||
333 | * @group DDC-178 |
||
334 | */ |
||
335 | public function testPessimisticWriteLockWithoutTransaction_ThrowsException() |
||
336 | { |
||
337 | $this->expectException(TransactionRequiredException::class); |
||
338 | |||
339 | $this->em->getRepository(CmsUser::class) |
||
340 | ->find(1, LockMode::PESSIMISTIC_WRITE); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @group locking |
||
345 | * @group DDC-178 |
||
346 | */ |
||
347 | public function testOptimisticLockUnversionedEntity_ThrowsException() |
||
348 | { |
||
349 | $this->expectException(OptimisticLockException::class); |
||
350 | |||
351 | $this->em->getRepository(CmsUser::class) |
||
352 | ->find(1, LockMode::OPTIMISTIC); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @group locking |
||
357 | * @group DDC-178 |
||
358 | */ |
||
359 | public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException() |
||
360 | { |
||
361 | $user = new CmsUser; |
||
362 | $user->name = 'Roman'; |
||
363 | $user->username = 'romanb'; |
||
364 | $user->status = 'freak'; |
||
365 | $this->em->persist($user); |
||
366 | $this->em->flush(); |
||
367 | |||
368 | $userId = $user->id; |
||
369 | |||
370 | $this->em->find(CmsUser::class, $userId); |
||
371 | |||
372 | $this->expectException(OptimisticLockException::class); |
||
373 | |||
374 | $this->em->find(CmsUser::class, $userId, LockMode::OPTIMISTIC); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @group DDC-819 |
||
379 | */ |
||
380 | public function testFindMagicCallByNullValue() |
||
381 | { |
||
382 | $this->loadFixture(); |
||
383 | |||
384 | $repos = $this->em->getRepository(CmsUser::class); |
||
385 | |||
386 | $users = $repos->findByStatus(null); |
||
387 | self::assertCount(1, $users); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @group DDC-819 |
||
392 | */ |
||
393 | public function testInvalidMagicCall() |
||
394 | { |
||
395 | $this->expectException(\BadMethodCallException::class); |
||
396 | |||
397 | $repos = $this->em->getRepository(CmsUser::class); |
||
398 | $repos->foo(); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @group DDC-817 |
||
403 | */ |
||
404 | public function testFindByAssociationKey_ExceptionOnInverseSide() |
||
405 | { |
||
406 | list($userId, $addressId) = $this->loadAssociatedFixture(); |
||
407 | $repos = $this->em->getRepository(CmsUser::class); |
||
408 | |||
409 | $this->expectException(ORMException::class); |
||
410 | $this->expectExceptionMessage("You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association. Find methods only work on owning side associations."); |
||
411 | |||
412 | $user = $repos->findBy(['address' => $addressId]); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @group DDC-817 |
||
417 | */ |
||
418 | public function testFindOneByAssociationKey() |
||
419 | { |
||
420 | list($userId, $addressId) = $this->loadAssociatedFixture(); |
||
421 | $repos = $this->em->getRepository(CmsAddress::class); |
||
422 | $address = $repos->findOneBy(['user' => $userId]); |
||
423 | |||
424 | self::assertInstanceOf(CmsAddress::class, $address); |
||
425 | self::assertEquals($addressId, $address->id); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @group DDC-1241 |
||
430 | */ |
||
431 | public function testFindOneByOrderBy() |
||
432 | { |
||
433 | $this->loadFixture(); |
||
434 | |||
435 | $repos = $this->em->getRepository(CmsUser::class); |
||
436 | $userAsc = $repos->findOneBy([], ["username" => "ASC"]); |
||
437 | $userDesc = $repos->findOneBy([], ["username" => "DESC"]); |
||
438 | |||
439 | self::assertNotSame($userAsc, $userDesc); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @group DDC-817 |
||
444 | */ |
||
445 | public function testFindByAssociationKey() |
||
446 | { |
||
447 | list($userId, $addressId) = $this->loadAssociatedFixture(); |
||
448 | $repos = $this->em->getRepository(CmsAddress::class); |
||
449 | $addresses = $repos->findBy(['user' => $userId]); |
||
450 | |||
451 | self::assertContainsOnly(CmsAddress::class, $addresses); |
||
452 | self::assertCount(1, $addresses); |
||
453 | self::assertEquals($addressId, $addresses[0]->id); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * @group DDC-817 |
||
458 | */ |
||
459 | public function testFindAssociationByMagicCall() |
||
460 | { |
||
461 | list($userId, $addressId) = $this->loadAssociatedFixture(); |
||
462 | $repos = $this->em->getRepository(CmsAddress::class); |
||
463 | $addresses = $repos->findByUser($userId); |
||
464 | |||
465 | self::assertContainsOnly(CmsAddress::class, $addresses); |
||
466 | self::assertCount(1, $addresses); |
||
467 | self::assertEquals($addressId, $addresses[0]->id); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @group DDC-817 |
||
472 | */ |
||
473 | public function testFindOneAssociationByMagicCall() |
||
474 | { |
||
475 | list($userId, $addressId) = $this->loadAssociatedFixture(); |
||
476 | $repos = $this->em->getRepository(CmsAddress::class); |
||
477 | $address = $repos->findOneByUser($userId); |
||
478 | |||
479 | self::assertInstanceOf(CmsAddress::class, $address); |
||
480 | self::assertEquals($addressId, $address->id); |
||
481 | } |
||
482 | |||
483 | public function testValidNamedQueryRetrieval() |
||
484 | { |
||
485 | $repos = $this->em->getRepository(CmsUser::class); |
||
486 | |||
487 | $query = $repos->createNamedQuery('all'); |
||
488 | |||
489 | self::assertInstanceOf(Query::class, $query); |
||
490 | self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $query->getDQL()); |
||
491 | } |
||
492 | |||
493 | public function testInvalidNamedQueryRetrieval() |
||
494 | { |
||
495 | $repos = $this->em->getRepository(CmsUser::class); |
||
496 | |||
497 | $this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
||
498 | |||
499 | $repos->createNamedQuery('invalidNamedQuery'); |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * @group DDC-1087 |
||
504 | */ |
||
505 | public function testIsNullCriteriaDoesNotGenerateAParameter() |
||
506 | { |
||
507 | $repos = $this->em->getRepository(CmsUser::class); |
||
508 | $users = $repos->findBy(['status' => null, 'username' => 'romanb']); |
||
509 | |||
510 | $params = $this->sqlLoggerStack->queries[$this->sqlLoggerStack->currentQuery]['params']; |
||
511 | self::assertCount(1, $params, "Should only execute with one parameter."); |
||
512 | self::assertEquals(['romanb'], $params); |
||
513 | } |
||
514 | |||
515 | public function testIsNullCriteria() |
||
516 | { |
||
517 | $this->loadFixture(); |
||
518 | |||
519 | $repos = $this->em->getRepository(CmsUser::class); |
||
520 | |||
521 | $users = $repos->findBy(['status' => null]); |
||
522 | self::assertCount(1, $users); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * @group DDC-1094 |
||
527 | */ |
||
528 | public function testFindByLimitOffset() |
||
529 | { |
||
530 | $this->loadFixture(); |
||
531 | |||
532 | $repos = $this->em->getRepository(CmsUser::class); |
||
533 | |||
534 | $users1 = $repos->findBy([], null, 1, 0); |
||
535 | $users2 = $repos->findBy([], null, 1, 1); |
||
536 | |||
537 | self::assertCount(4, $repos->findBy([])); |
||
538 | self::assertCount(1, $users1); |
||
539 | self::assertCount(1, $users2); |
||
540 | self::assertNotSame($users1[0], $users2[0]); |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * @group DDC-1094 |
||
545 | */ |
||
546 | public function testFindByOrderBy() |
||
547 | { |
||
548 | $this->loadFixture(); |
||
549 | |||
550 | $repos = $this->em->getRepository(CmsUser::class); |
||
551 | $usersAsc = $repos->findBy([], ["username" => "ASC"]); |
||
552 | $usersDesc = $repos->findBy([], ["username" => "DESC"]); |
||
553 | |||
554 | self::assertCount(4, $usersAsc, "Pre-condition: only four users in fixture"); |
||
555 | self::assertCount(4, $usersDesc, "Pre-condition: only four users in fixture"); |
||
556 | self::assertSame($usersAsc[0], $usersDesc[3]); |
||
557 | self::assertSame($usersAsc[3], $usersDesc[0]); |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * @group DDC-1376 |
||
562 | */ |
||
563 | public function testFindByOrderByAssociation() |
||
564 | { |
||
565 | $this->loadFixtureUserEmail(); |
||
566 | |||
567 | $repository = $this->em->getRepository(CmsUser::class); |
||
568 | $resultAsc = $repository->findBy([], ['email' => 'ASC']); |
||
569 | $resultDesc = $repository->findBy([], ['email' => 'DESC']); |
||
570 | |||
571 | self::assertCount(3, $resultAsc); |
||
572 | self::assertCount(3, $resultDesc); |
||
573 | |||
574 | self::assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId()); |
||
575 | self::assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId()); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @group DDC-1426 |
||
580 | */ |
||
581 | public function testFindFieldByMagicCallOrderBy() |
||
582 | { |
||
583 | $this->loadFixture(); |
||
584 | $repos = $this->em->getRepository(CmsUser::class); |
||
585 | |||
586 | $usersAsc = $repos->findByStatus('dev', ['username' => "ASC"]); |
||
587 | $usersDesc = $repos->findByStatus('dev', ['username' => "DESC"]); |
||
588 | |||
589 | self::assertCount(2, $usersAsc); |
||
590 | self::assertCount(2, $usersDesc); |
||
591 | |||
592 | self::assertInstanceOf(CmsUser::class, $usersAsc[0]); |
||
593 | self::assertEquals('Alexander', $usersAsc[0]->name); |
||
594 | self::assertEquals('dev', $usersAsc[0]->status); |
||
595 | |||
596 | self::assertSame($usersAsc[0], $usersDesc[1]); |
||
597 | self::assertSame($usersAsc[1], $usersDesc[0]); |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * @group DDC-1426 |
||
602 | */ |
||
603 | public function testFindFieldByMagicCallLimitOffset() |
||
604 | { |
||
605 | $this->loadFixture(); |
||
606 | $repos = $this->em->getRepository(CmsUser::class); |
||
607 | |||
608 | $users1 = $repos->findByStatus('dev', [], 1, 0); |
||
609 | $users2 = $repos->findByStatus('dev', [], 1, 1); |
||
610 | |||
611 | self::assertCount(1, $users1); |
||
612 | self::assertCount(1, $users2); |
||
613 | self::assertNotSame($users1[0], $users2[0]); |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * @group DDC-753 |
||
618 | */ |
||
619 | public function testDefaultRepositoryClassName() |
||
620 | { |
||
621 | self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class); |
||
622 | $this->em->getConfiguration()->setDefaultRepositoryClassName(DDC753DefaultRepository::class); |
||
623 | self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class); |
||
624 | |||
625 | $repos = $this->em->getRepository(DDC753EntityWithDefaultCustomRepository::class); |
||
626 | self::assertInstanceOf(DDC753DefaultRepository::class, $repos); |
||
627 | self::assertTrue($repos->isDefaultRepository()); |
||
628 | |||
629 | |||
630 | $repos = $this->em->getRepository(DDC753EntityWithCustomRepository::class); |
||
631 | self::assertInstanceOf(DDC753CustomRepository::class, $repos); |
||
632 | self::assertTrue($repos->isCustomRepository()); |
||
633 | |||
634 | self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class); |
||
635 | $this->em->getConfiguration()->setDefaultRepositoryClassName(EntityRepository::class); |
||
636 | self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class); |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * @group DDC-753 |
||
641 | * @expectedException Doctrine\ORM\ORMException |
||
642 | * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. It must be a Doctrine\Common\Persistence\ObjectRepository. |
||
643 | */ |
||
644 | public function testSetDefaultRepositoryInvalidClassError() |
||
645 | { |
||
646 | self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class); |
||
647 | $this->em->getConfiguration()->setDefaultRepositoryClassName(DDC753InvalidRepository::class); |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * @group DDC-3257 |
||
652 | */ |
||
653 | public function testSingleRepositoryInstanceForDifferentEntityAliases() |
||
654 | { |
||
655 | $config = $this->em->getConfiguration(); |
||
656 | |||
657 | $config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS'); |
||
658 | $config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS'); |
||
659 | |||
660 | $repository = $this->em->getRepository(CmsUser::class); |
||
661 | |||
662 | self::assertSame($repository, $this->em->getRepository('Aliased:CmsUser')); |
||
663 | self::assertSame($repository, $this->em->getRepository('AliasedAgain:CmsUser')); |
||
664 | } |
||
665 | |||
666 | /** |
||
667 | * @group DDC-3257 |
||
668 | */ |
||
669 | public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash() |
||
670 | { |
||
671 | self::assertSame( |
||
672 | $this->em->getRepository('\\' . CmsUser::class), |
||
673 | $this->em->getRepository(CmsUser::class) |
||
674 | ); |
||
675 | } |
||
676 | |||
677 | /** |
||
678 | * @group DDC-1376 |
||
679 | * |
||
680 | * @expectedException Doctrine\ORM\ORMException |
||
681 | * @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association. |
||
682 | */ |
||
683 | public function testInvalidOrderByAssociation() |
||
684 | { |
||
685 | $this->em->getRepository(CmsUser::class) |
||
686 | ->findBy(['status' => 'test'], ['address' => 'ASC']); |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * @group DDC-1500 |
||
691 | */ |
||
692 | public function testInvalidOrientation() |
||
693 | { |
||
694 | $this->expectException(ORMException::class); |
||
695 | $this->expectExceptionMessage('Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username'); |
||
696 | |||
697 | $repo = $this->em->getRepository(CmsUser::class); |
||
698 | $repo->findBy(['status' => 'test'], ['username' => 'INVALID']); |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * @group DDC-1713 |
||
703 | */ |
||
704 | public function testFindByAssociationArray() |
||
705 | { |
||
706 | $repo = $this->em->getRepository(CmsAddress::class); |
||
707 | $data = $repo->findBy(['user' => [1, 2, 3]]); |
||
708 | |||
709 | $query = array_pop($this->sqlLoggerStack->queries); |
||
710 | self::assertEquals([1,2,3], $query['params'][0]); |
||
711 | self::assertEquals(Connection::PARAM_INT_ARRAY, $query['types'][0]); |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * @group DDC-1637 |
||
716 | */ |
||
717 | public function testMatchingEmptyCriteria() |
||
718 | { |
||
719 | $this->loadFixture(); |
||
720 | |||
721 | $repository = $this->em->getRepository(CmsUser::class); |
||
722 | $users = $repository->matching(new Criteria()); |
||
723 | |||
724 | self::assertCount(4, $users); |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * @group DDC-1637 |
||
729 | */ |
||
730 | public function testMatchingCriteriaEqComparison() |
||
731 | { |
||
732 | $this->loadFixture(); |
||
733 | |||
734 | $repository = $this->em->getRepository(CmsUser::class); |
||
735 | $users = $repository->matching(new Criteria( |
||
736 | Criteria::expr()->eq('username', 'beberlei') |
||
737 | )); |
||
738 | |||
739 | self::assertCount(1, $users); |
||
740 | } |
||
741 | |||
742 | /** |
||
743 | * @group DDC-1637 |
||
744 | */ |
||
745 | public function testMatchingCriteriaNeqComparison() |
||
746 | { |
||
747 | $this->loadFixture(); |
||
748 | |||
749 | $repository = $this->em->getRepository(CmsUser::class); |
||
750 | $users = $repository->matching(new Criteria( |
||
751 | Criteria::expr()->neq('username', 'beberlei') |
||
752 | )); |
||
753 | |||
754 | self::assertCount(3, $users); |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * @group DDC-1637 |
||
759 | */ |
||
760 | public function testMatchingCriteriaInComparison() |
||
761 | { |
||
762 | $this->loadFixture(); |
||
763 | |||
764 | $repository = $this->em->getRepository(CmsUser::class); |
||
765 | $users = $repository->matching(new Criteria( |
||
766 | Criteria::expr()->in('username', ['beberlei', 'gblanco']) |
||
767 | )); |
||
768 | |||
769 | self::assertCount(2, $users); |
||
770 | } |
||
771 | |||
772 | /** |
||
773 | * @group DDC-1637 |
||
774 | */ |
||
775 | public function testMatchingCriteriaNotInComparison() |
||
776 | { |
||
777 | $this->loadFixture(); |
||
778 | |||
779 | $repository = $this->em->getRepository(CmsUser::class); |
||
780 | $users = $repository->matching(new Criteria( |
||
781 | Criteria::expr()->notIn('username', ['beberlei', 'gblanco', 'asm89']) |
||
782 | )); |
||
783 | |||
784 | self::assertCount(1, $users); |
||
785 | } |
||
786 | |||
787 | /** |
||
788 | * @group DDC-1637 |
||
789 | */ |
||
790 | public function testMatchingCriteriaLtComparison() |
||
791 | { |
||
792 | $firstUserId = $this->loadFixture(); |
||
793 | |||
794 | $repository = $this->em->getRepository(CmsUser::class); |
||
795 | $users = $repository->matching(new Criteria( |
||
796 | Criteria::expr()->lt('id', $firstUserId + 1) |
||
797 | )); |
||
798 | |||
799 | self::assertCount(1, $users); |
||
800 | } |
||
801 | |||
802 | /** |
||
803 | * @group DDC-1637 |
||
804 | */ |
||
805 | public function testMatchingCriteriaLeComparison() |
||
806 | { |
||
807 | $firstUserId = $this->loadFixture(); |
||
808 | |||
809 | $repository = $this->em->getRepository(CmsUser::class); |
||
810 | $users = $repository->matching(new Criteria( |
||
811 | Criteria::expr()->lte('id', $firstUserId + 1) |
||
812 | )); |
||
813 | |||
814 | self::assertCount(2, $users); |
||
815 | } |
||
816 | |||
817 | /** |
||
818 | * @group DDC-1637 |
||
819 | */ |
||
820 | public function testMatchingCriteriaGtComparison() |
||
821 | { |
||
822 | $firstUserId = $this->loadFixture(); |
||
823 | |||
824 | $repository = $this->em->getRepository(CmsUser::class); |
||
825 | $users = $repository->matching(new Criteria( |
||
826 | Criteria::expr()->gt('id', $firstUserId) |
||
827 | )); |
||
828 | |||
829 | self::assertCount(3, $users); |
||
830 | } |
||
831 | |||
832 | /** |
||
833 | * @group DDC-1637 |
||
834 | */ |
||
835 | public function testMatchingCriteriaGteComparison() |
||
836 | { |
||
837 | $firstUserId = $this->loadFixture(); |
||
838 | |||
839 | $repository = $this->em->getRepository(CmsUser::class); |
||
840 | $users = $repository->matching(new Criteria( |
||
841 | Criteria::expr()->gte('id', $firstUserId) |
||
842 | )); |
||
843 | |||
844 | self::assertCount(4, $users); |
||
845 | } |
||
846 | |||
847 | /** |
||
848 | * @group DDC-2430 |
||
849 | */ |
||
850 | public function testMatchingCriteriaAssocationByObjectInMemory() |
||
851 | { |
||
852 | list($userId, $addressId) = $this->loadAssociatedFixture(); |
||
853 | |||
854 | $user = $this->em->find(CmsUser::class, $userId); |
||
855 | |||
856 | $criteria = new Criteria( |
||
857 | Criteria::expr()->eq('user', $user) |
||
858 | ); |
||
859 | |||
860 | $repository = $this->em->getRepository(CmsAddress::class); |
||
861 | $addresses = $repository->matching($criteria); |
||
862 | |||
863 | self::assertCount(1, $addresses); |
||
864 | |||
865 | $addresses = new ArrayCollection($repository->findAll()); |
||
866 | |||
867 | self::assertCount(1, $addresses->matching($criteria)); |
||
868 | } |
||
869 | |||
870 | /** |
||
871 | * @group DDC-2430 |
||
872 | */ |
||
873 | public function testMatchingCriteriaAssocationInWithArray() |
||
874 | { |
||
875 | list($userId, $addressId) = $this->loadAssociatedFixture(); |
||
876 | |||
877 | $user = $this->em->find(CmsUser::class, $userId); |
||
878 | |||
879 | $criteria = new Criteria( |
||
880 | Criteria::expr()->in('user', [$user]) |
||
881 | ); |
||
882 | |||
883 | $repository = $this->em->getRepository(CmsAddress::class); |
||
884 | $addresses = $repository->matching($criteria); |
||
885 | |||
886 | self::assertCount(1, $addresses); |
||
887 | |||
888 | $addresses = new ArrayCollection($repository->findAll()); |
||
889 | |||
890 | self::assertCount(1, $addresses->matching($criteria)); |
||
891 | } |
||
892 | |||
893 | public function testMatchingCriteriaContainsComparison() |
||
894 | { |
||
895 | $this->loadFixture(); |
||
896 | |||
897 | $repository = $this->em->getRepository(CmsUser::class); |
||
898 | |||
899 | $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar'))); |
||
900 | self::assertCount(0, $users); |
||
901 | |||
902 | $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom'))); |
||
903 | self::assertCount(1, $users); |
||
904 | |||
905 | $users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev'))); |
||
906 | self::assertCount(2, $users); |
||
907 | } |
||
908 | |||
909 | public function testMatchingCriteriaStartsWithComparison() |
||
910 | { |
||
911 | $this->loadFixture(); |
||
912 | |||
913 | $repository = $this->em->getRepository(CmsUser::class); |
||
914 | |||
915 | $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'Foo'))); |
||
916 | self::assertCount(0, $users); |
||
917 | |||
918 | $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'R'))); |
||
919 | self::assertCount(1, $users); |
||
920 | |||
921 | $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('status', 'de'))); |
||
922 | self::assertCount(2, $users); |
||
923 | } |
||
924 | |||
925 | public function testMatchingCriteriaEndsWithComparison() |
||
926 | { |
||
927 | $this->loadFixture(); |
||
928 | |||
929 | $repository = $this->em->getRepository(CmsUser::class); |
||
930 | |||
931 | $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'foo'))); |
||
932 | self::assertCount(0, $users); |
||
933 | |||
934 | $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'oman'))); |
||
935 | self::assertCount(1, $users); |
||
936 | |||
937 | $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('status', 'ev'))); |
||
938 | self::assertCount(2, $users); |
||
939 | } |
||
940 | |||
941 | /** |
||
942 | * @group DDC-2478 |
||
943 | */ |
||
944 | public function testMatchingCriteriaNullAssocComparison() |
||
945 | { |
||
946 | $fixtures = $this->loadFixtureUserEmail(); |
||
947 | $user = $this->em->find(get_class($fixtures[0]), $fixtures[0]->id); |
||
948 | $repository = $this->em->getRepository(CmsUser::class); |
||
949 | $criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email')); |
||
950 | $criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null)); |
||
951 | |||
952 | $user->setEmail(null); |
||
953 | $this->em->flush(); |
||
954 | $this->em->clear(); |
||
955 | |||
956 | $usersIsNull = $repository->matching($criteriaIsNull); |
||
957 | $usersEqNull = $repository->matching($criteriaEqNull); |
||
958 | |||
959 | self::assertCount(1, $usersIsNull); |
||
960 | self::assertCount(1, $usersEqNull); |
||
961 | |||
962 | self::assertInstanceOf(CmsUser::class, $usersIsNull[0]); |
||
963 | self::assertInstanceOf(CmsUser::class, $usersEqNull[0]); |
||
964 | |||
965 | self::assertNull($usersIsNull[0]->getEmail()); |
||
966 | self::assertNull($usersEqNull[0]->getEmail()); |
||
967 | } |
||
968 | |||
969 | /** |
||
970 | * @group DDC-2055 |
||
971 | */ |
||
972 | public function testCreateResultSetMappingBuilder() |
||
973 | { |
||
974 | $repository = $this->em->getRepository(CmsUser::class); |
||
975 | $rsm = $repository->createResultSetMappingBuilder('u'); |
||
976 | |||
977 | self::assertInstanceOf(Query\ResultSetMappingBuilder::class, $rsm); |
||
978 | self::assertEquals(['u' => CmsUser::class], $rsm->aliasMap); |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * @group DDC-3045 |
||
983 | */ |
||
984 | public function testFindByFieldInjectionPrevented() |
||
985 | { |
||
986 | $this->expectException(ORMException::class); |
||
987 | $this->expectExceptionMessage('Unrecognized field: '); |
||
988 | |||
989 | $repository = $this->em->getRepository(CmsUser::class); |
||
990 | $repository->findBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']); |
||
991 | } |
||
992 | |||
993 | /** |
||
994 | * @group DDC-3045 |
||
995 | */ |
||
996 | public function testFindOneByFieldInjectionPrevented() |
||
997 | { |
||
998 | $this->expectException(ORMException::class); |
||
999 | $this->expectExceptionMessage('Unrecognized field: '); |
||
1000 | |||
1001 | $repository = $this->em->getRepository(CmsUser::class); |
||
1002 | $repository->findOneBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']); |
||
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * @group DDC-3045 |
||
1007 | */ |
||
1008 | public function testMatchingInjectionPrevented() |
||
1009 | { |
||
1010 | $this->expectException(ORMException::class); |
||
1011 | $this->expectExceptionMessage('Unrecognized field: '); |
||
1012 | |||
1013 | $repository = $this->em->getRepository(CmsUser::class); |
||
1014 | $result = $repository->matching(new Criteria( |
||
1015 | Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei') |
||
1016 | )); |
||
1017 | |||
1018 | // Because repository returns a lazy collection, we call toArray to force initialization |
||
1019 | $result->toArray(); |
||
1020 | } |
||
1021 | |||
1022 | /** |
||
1023 | * @group DDC-3045 |
||
1024 | */ |
||
1025 | public function testFindInjectionPrevented() |
||
1026 | { |
||
1027 | $this->expectException(ORMException::class); |
||
1028 | $this->expectExceptionMessage('Unrecognized identifier fields: '); |
||
1029 | |||
1030 | $repository = $this->em->getRepository(CmsUser::class); |
||
1031 | $repository->find(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test', 'id' => 1]); |
||
1032 | } |
||
1033 | |||
1034 | /** |
||
1035 | * @group DDC-3056 |
||
1036 | */ |
||
1037 | public function testFindByNullValueInInCondition() |
||
1038 | { |
||
1039 | $user1 = new CmsUser(); |
||
1040 | $user2 = new CmsUser(); |
||
1041 | |||
1042 | $user1->username = 'ocramius'; |
||
1043 | $user1->name = 'Marco'; |
||
1044 | $user2->status = null; |
||
1045 | $user2->username = 'deeky666'; |
||
1046 | $user2->name = 'Steve'; |
||
1047 | $user2->status = 'dbal maintainer'; |
||
1048 | |||
1049 | $this->em->persist($user1); |
||
1050 | $this->em->persist($user2); |
||
1051 | $this->em->flush(); |
||
1052 | |||
1053 | $users = $this->em->getRepository(CmsUser::class)->findBy(['status' => [null]]); |
||
1054 | |||
1055 | self::assertCount(1, $users); |
||
1056 | self::assertSame($user1, reset($users)); |
||
1057 | } |
||
1058 | |||
1059 | /** |
||
1060 | * @group DDC-3056 |
||
1061 | */ |
||
1062 | public function testFindByNullValueInMultipleInCriteriaValues() |
||
1063 | { |
||
1064 | $user1 = new CmsUser(); |
||
1065 | $user2 = new CmsUser(); |
||
1066 | |||
1067 | $user1->username = 'ocramius'; |
||
1068 | $user1->name = 'Marco'; |
||
1069 | $user2->status = null; |
||
1070 | $user2->username = 'deeky666'; |
||
1071 | $user2->name = 'Steve'; |
||
1072 | $user2->status = 'dbal maintainer'; |
||
1073 | |||
1074 | $this->em->persist($user1); |
||
1075 | $this->em->persist($user2); |
||
1076 | $this->em->flush(); |
||
1077 | |||
1078 | $users = $this |
||
1079 | ->em |
||
1080 | ->getRepository(CmsUser::class) |
||
1081 | ->findBy(['status' => ['foo', null]]); |
||
1082 | |||
1083 | self::assertCount(1, $users); |
||
1084 | self::assertSame($user1, reset($users)); |
||
1085 | } |
||
1086 | |||
1087 | /** |
||
1088 | * @group DDC-3056 |
||
1089 | */ |
||
1090 | public function testFindMultipleByNullValueInMultipleInCriteriaValues() |
||
1091 | { |
||
1092 | $user1 = new CmsUser(); |
||
1093 | $user2 = new CmsUser(); |
||
1094 | |||
1095 | $user1->username = 'ocramius'; |
||
1096 | $user1->name = 'Marco'; |
||
1097 | $user2->status = null; |
||
1098 | $user2->username = 'deeky666'; |
||
1099 | $user2->name = 'Steve'; |
||
1100 | $user2->status = 'dbal maintainer'; |
||
1101 | |||
1102 | $this->em->persist($user1); |
||
1103 | $this->em->persist($user2); |
||
1104 | $this->em->flush(); |
||
1105 | |||
1106 | $users = $this |
||
1107 | ->em |
||
1108 | ->getRepository(CmsUser::class) |
||
1109 | ->findBy(['status' => ['dbal maintainer', null]]); |
||
1110 | |||
1111 | self::assertCount(2, $users); |
||
1112 | |||
1113 | foreach ($users as $user) { |
||
1114 | self::assertContains($user, [$user1, $user2]); |
||
1115 | } |
||
1116 | } |
||
1117 | } |
||
1118 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.