Failed Conditions
Pull Request — develop (#6935)
by Michael
167:08 queued 149:28
created

EntityRepositoryTest::loadFixture()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
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 loadFixture()
38
    {
39
        $user = new CmsUser;
40
        $user->name = 'Roman';
41
        $user->username = 'romanb';
42
        $user->status = 'freak';
43
        $this->em->persist($user);
44
45
        $user2 = new CmsUser;
46
        $user2->name = 'Guilherme';
47
        $user2->username = 'gblanco';
48
        $user2->status = 'dev';
49
        $this->em->persist($user2);
50
51
        $user3 = new CmsUser;
52
        $user3->name = 'Benjamin';
53
        $user3->username = 'beberlei';
54
        $user3->status = null;
55
        $this->em->persist($user3);
56
57
        $user4 = new CmsUser;
58
        $user4->name = 'Alexander';
59
        $user4->username = 'asm89';
60
        $user4->status = 'dev';
61
        $this->em->persist($user4);
62
63
        $this->em->flush();
64
65
        $user1Id = $user->getId();
66
67
        unset($user, $user2, $user3, $user4);
68
69
        $this->em->clear();
70
71
        return $user1Id;
72
    }
73
74
    public function loadAssociatedFixture()
75
    {
76
        $address = new CmsAddress();
77
        $address->city = "Berlin";
78
        $address->country = "Germany";
79
        $address->street = "Foostreet";
80
        $address->zip = "12345";
81
82
        $user = new CmsUser();
83
        $user->name = 'Roman';
84
        $user->username = 'romanb';
85
        $user->status = 'freak';
86
        $user->setAddress($address);
87
88
        $this->em->persist($user);
89
        $this->em->persist($address);
90
        $this->em->flush();
91
        $this->em->clear();
92
93
        return [$user->id, $address->id];
94
    }
95
96
    public function loadFixtureUserEmail()
97
    {
98
        $user1 = new CmsUser();
99
        $user2 = new CmsUser();
100
        $user3 = new CmsUser();
101
102
        $email1 = new CmsEmail();
103
        $email2 = new CmsEmail();
104
        $email3 = new CmsEmail();
105
106
        $user1->name     = 'Test 1';
107
        $user1->username = 'test1';
108
        $user1->status   = 'active';
109
110
        $user2->name     = 'Test 2';
111
        $user2->username = 'test2';
112
        $user2->status   = 'active';
113
114
        $user3->name     = 'Test 3';
115
        $user3->username = 'test3';
116
        $user3->status   = 'active';
117
118
        $email1->email   = '[email protected]';
119
        $email2->email   = '[email protected]';
120
        $email3->email   = '[email protected]';
121
122
        $user1->setEmail($email1);
123
        $user2->setEmail($email2);
124
        $user3->setEmail($email3);
125
126
        $this->em->persist($user1);
127
        $this->em->persist($user2);
128
        $this->em->persist($user3);
129
130
        $this->em->persist($email1);
131
        $this->em->persist($email2);
132
        $this->em->persist($email3);
133
134
        $this->em->flush();
135
        $this->em->clear();
136
137
        return [$user1, $user2, $user3];
138
    }
139
140
    public function buildUser($name, $username, $status, $address)
141
    {
142
        $user = new CmsUser();
143
        $user->name     = $name;
144
        $user->username = $username;
145
        $user->status   = $status;
146
        $user->setAddress($address);
147
148
        $this->em->persist($user);
149
        $this->em->flush();
150
151
        return $user;
152
    }
153
154
    public function buildAddress($country, $city, $street, $zip)
155
    {
156
        $address = new CmsAddress();
157
        $address->country = $country;
158
        $address->city    = $city;
159
        $address->street  = $street;
160
        $address->zip     = $zip;
161
162
        $this->em->persist($address);
163
        $this->em->flush();
164
165
        return $address;
166
    }
167
168
    public function testBasicFind()
169
    {
170
        $user1Id = $this->loadFixture();
171
        $repos = $this->em->getRepository(CmsUser::class);
172
173
        $user = $repos->find($user1Id);
174
        self::assertInstanceOf(CmsUser::class, $user);
175
        self::assertEquals('Roman', $user->name);
176
        self::assertEquals('freak', $user->status);
177
    }
178
179
    public function testFindByField()
180
    {
181
        $user1Id = $this->loadFixture();
0 ignored issues
show
Unused Code introduced by
The assignment to $user1Id is dead and can be removed.
Loading history...
182
        $repos = $this->em->getRepository(CmsUser::class);
183
184
        $users = $repos->findBy(['status' => 'dev']);
185
        self::assertCount(2, $users);
186
        self::assertInstanceOf(CmsUser::class, $users[0]);
187
        self::assertEquals('Guilherme', $users[0]->name);
188
        self::assertEquals('dev', $users[0]->status);
189
    }
190
191
    public function testFindByAssociationWithIntegerAsParameter()
192
    {
193
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
194
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
195
196
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
197
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
198
199
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
200
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
0 ignored issues
show
Unused Code introduced by
The assignment to $user3 is dead and can be removed.
Loading history...
201
202
        unset($address1, $address2, $address3);
203
204
        $this->em->clear();
205
206
        $repository = $this->em->getRepository(CmsAddress::class);
207
        $addresses  = $repository->findBy(['user' => [$user1->getId(), $user2->getId()]]);
208
209
        self::assertCount(2, $addresses);
210
        self::assertInstanceOf(CmsAddress::class, $addresses[0]);
211
    }
212
213
    public function testFindByAssociationWithObjectAsParameter()
214
    {
215
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
216
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
217
218
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
219
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
220
221
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
222
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
0 ignored issues
show
Unused Code introduced by
The assignment to $user3 is dead and can be removed.
Loading history...
223
224
        unset($address1, $address2, $address3);
225
226
        $this->em->clear();
227
228
        $repository = $this->em->getRepository(CmsAddress::class);
229
        $addresses  = $repository->findBy(['user' => [$user1, $user2]]);
230
231
        self::assertCount(2, $addresses);
232
        self::assertInstanceOf(CmsAddress::class, $addresses[0]);
233
    }
234
235
    public function testFindFieldByMagicCall()
236
    {
237
        $user1Id = $this->loadFixture();
0 ignored issues
show
Unused Code introduced by
The assignment to $user1Id is dead and can be removed.
Loading history...
238
        $repos = $this->em->getRepository(CmsUser::class);
239
240
        $users = $repos->findByStatus('dev');
0 ignored issues
show
Bug introduced by
The method findByStatus() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findBy()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

240
        /** @scrutinizer ignore-call */ 
241
        $users = $repos->findByStatus('dev');

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.

Loading history...
241
        self::assertCount(2, $users);
242
        self::assertInstanceOf(CmsUser::class, $users[0]);
243
        self::assertEquals('Guilherme', $users[0]->name);
244
        self::assertEquals('dev', $users[0]->status);
245
    }
246
247
    public function testFindAll()
248
    {
249
        $user1Id = $this->loadFixture();
0 ignored issues
show
Unused Code introduced by
The assignment to $user1Id is dead and can be removed.
Loading history...
250
        $repos = $this->em->getRepository(CmsUser::class);
251
252
        $users = $repos->findAll();
253
        self::assertCount(4, $users);
254
    }
255
256
    public function testCount()
257
    {
258
        $this->loadFixture();
259
        $repos = $this->em->getRepository(CmsUser::class);
260
261
        $userCount = $repos->count([]);
262
        self::assertSame(4, $userCount);
263
264
        $userCount = $repos->count(['status' => 'dev']);
265
        self::assertSame(2, $userCount);
266
267
        $userCount = $repos->count(['status' => 'nonexistent']);
268
        self::assertSame(0, $userCount);
269
    }
270
271
    public function testCountBy()
272
    {
273
        $this->loadFixture();
274
        $repos = $this->em->getRepository(CmsUser::class);
275
276
        $userCount = $repos->countByStatus('dev');
277
        self::assertSame(2, $userCount);
278
    }
279
280
    /**
281
     * @expectedException \Doctrine\ORM\ORMException
282
     */
283
    public function testExceptionIsThrownWhenCallingFindByWithoutParameter()
284
    {
285
        $this->em->getRepository(CmsUser::class)
286
                  ->findByStatus();
287
    }
288
289
    /**
290
     * @expectedException \Doctrine\ORM\ORMException
291
     */
292
    public function testExceptionIsThrownWhenUsingInvalidFieldName()
293
    {
294
        $this->em->getRepository(CmsUser::class)
295
                  ->findByThisFieldDoesNotExist('testvalue');
0 ignored issues
show
Bug introduced by
The method findByThisFieldDoesNotExist() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findBy()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

295
                  ->/** @scrutinizer ignore-call */ findByThisFieldDoesNotExist('testvalue');

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.

Loading history...
296
    }
297
298
    /**
299
     * @group locking
300
     * @group DDC-178
301
     */
302
    public function testPessimisticReadLockWithoutTransaction_ThrowsException()
303
    {
304
        $this->expectException(TransactionRequiredException::class);
305
306
        $this->em->getRepository(CmsUser::class)
307
                  ->find(1, LockMode::PESSIMISTIC_READ);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Common\Persiste...bjectRepository::find() has too many arguments starting with Doctrine\DBAL\LockMode::PESSIMISTIC_READ. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

307
                  ->/** @scrutinizer ignore-call */ find(1, LockMode::PESSIMISTIC_READ);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
308
    }
309
310
    /**
311
     * @group locking
312
     * @group DDC-178
313
     */
314
    public function testPessimisticWriteLockWithoutTransaction_ThrowsException()
315
    {
316
        $this->expectException(TransactionRequiredException::class);
317
318
        $this->em->getRepository(CmsUser::class)
319
                  ->find(1, LockMode::PESSIMISTIC_WRITE);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Common\Persiste...bjectRepository::find() has too many arguments starting with Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

319
                  ->/** @scrutinizer ignore-call */ find(1, LockMode::PESSIMISTIC_WRITE);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
320
    }
321
322
    /**
323
     * @group locking
324
     * @group DDC-178
325
     */
326
    public function testOptimisticLockUnversionedEntity_ThrowsException()
327
    {
328
        $this->expectException(OptimisticLockException::class);
329
330
        $this->em->getRepository(CmsUser::class)
331
                  ->find(1, LockMode::OPTIMISTIC);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Common\Persiste...bjectRepository::find() has too many arguments starting with Doctrine\DBAL\LockMode::OPTIMISTIC. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

331
                  ->/** @scrutinizer ignore-call */ find(1, LockMode::OPTIMISTIC);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
332
    }
333
334
    /**
335
     * @group locking
336
     * @group DDC-178
337
     */
338
    public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException()
339
    {
340
        $user = new CmsUser;
341
        $user->name = 'Roman';
342
        $user->username = 'romanb';
343
        $user->status = 'freak';
344
        $this->em->persist($user);
345
        $this->em->flush();
346
347
        $userId = $user->id;
348
349
        $this->em->find(CmsUser::class, $userId);
350
351
        $this->expectException(OptimisticLockException::class);
352
353
        $this->em->find(CmsUser::class, $userId, LockMode::OPTIMISTIC);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Common\Persistence\ObjectManager::find() has too many arguments starting with Doctrine\DBAL\LockMode::OPTIMISTIC. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

353
        $this->em->/** @scrutinizer ignore-call */ 
354
                   find(CmsUser::class, $userId, LockMode::OPTIMISTIC);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
354
    }
355
356
    /**
357
     * @group DDC-819
358
     */
359
    public function testFindMagicCallByNullValue()
360
    {
361
        $this->loadFixture();
362
363
        $repos = $this->em->getRepository(CmsUser::class);
364
365
        $users = $repos->findByStatus(null);
366
        self::assertCount(1, $users);
367
    }
368
369
    /**
370
     * @group DDC-819
371
     */
372
    public function testInvalidMagicCall()
373
    {
374
        $this->expectException(\BadMethodCallException::class);
375
376
        $repos = $this->em->getRepository(CmsUser::class);
377
        $repos->foo();
378
    }
379
380
    /**
381
     * @group DDC-817
382
     */
383
    public function testFindByAssociationKey_ExceptionOnInverseSide()
384
    {
385
        list($userId, $addressId) = $this->loadAssociatedFixture();
386
        $repos = $this->em->getRepository(CmsUser::class);
387
388
        $this->expectException(ORMException::class);
389
        $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.");
390
391
        $user = $repos->findBy(['address' => $addressId]);
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
392
    }
393
394
    /**
395
     * @group DDC-817
396
     */
397
    public function testFindOneByAssociationKey()
398
    {
399
        list($userId, $addressId) = $this->loadAssociatedFixture();
400
        $repos = $this->em->getRepository(CmsAddress::class);
401
        $address = $repos->findOneBy(['user' => $userId]);
402
403
        self::assertInstanceOf(CmsAddress::class, $address);
404
        self::assertEquals($addressId, $address->id);
405
    }
406
407
    /**
408
     * @group DDC-1241
409
     */
410
    public function testFindOneByOrderBy()
411
    {
412
        $this->loadFixture();
413
414
        $repos = $this->em->getRepository(CmsUser::class);
415
        $userAsc = $repos->findOneBy([], ["username" => "ASC"]);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Common\Persiste...Repository::findOneBy() has too many arguments starting with array('username' => 'ASC'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

415
        /** @scrutinizer ignore-call */ 
416
        $userAsc = $repos->findOneBy([], ["username" => "ASC"]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
416
        $userDesc = $repos->findOneBy([], ["username" => "DESC"]);
417
418
        self::assertNotSame($userAsc, $userDesc);
419
    }
420
421
    /**
422
     * @group DDC-817
423
     */
424
    public function testFindByAssociationKey()
425
    {
426
        list($userId, $addressId) = $this->loadAssociatedFixture();
427
        $repos = $this->em->getRepository(CmsAddress::class);
428
        $addresses = $repos->findBy(['user' => $userId]);
429
430
        self::assertContainsOnly(CmsAddress::class, $addresses);
431
        self::assertCount(1, $addresses);
432
        self::assertEquals($addressId, $addresses[0]->id);
433
    }
434
435
    /**
436
     * @group DDC-817
437
     */
438
    public function testFindAssociationByMagicCall()
439
    {
440
        list($userId, $addressId) = $this->loadAssociatedFixture();
441
        $repos = $this->em->getRepository(CmsAddress::class);
442
        $addresses = $repos->findByUser($userId);
0 ignored issues
show
Bug introduced by
The method findByUser() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findBy()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

442
        /** @scrutinizer ignore-call */ 
443
        $addresses = $repos->findByUser($userId);

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.

Loading history...
443
444
        self::assertContainsOnly(CmsAddress::class, $addresses);
445
        self::assertCount(1, $addresses);
446
        self::assertEquals($addressId, $addresses[0]->id);
447
    }
448
449
    /**
450
     * @group DDC-817
451
     */
452
    public function testFindOneAssociationByMagicCall()
453
    {
454
        list($userId, $addressId) = $this->loadAssociatedFixture();
455
        $repos = $this->em->getRepository(CmsAddress::class);
456
        $address = $repos->findOneByUser($userId);
0 ignored issues
show
Bug introduced by
The method findOneByUser() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findOneBy()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

456
        /** @scrutinizer ignore-call */ 
457
        $address = $repos->findOneByUser($userId);

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.

Loading history...
457
458
        self::assertInstanceOf(CmsAddress::class, $address);
459
        self::assertEquals($addressId, $address->id);
460
    }
461
462
    public function testValidNamedQueryRetrieval()
463
    {
464
        $repos = $this->em->getRepository(CmsUser::class);
465
466
        $query = $repos->createNamedQuery('all');
467
468
        self::assertInstanceOf(Query::class, $query);
469
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $query->getDQL());
470
    }
471
472
    public function testInvalidNamedQueryRetrieval()
473
    {
474
        $repos = $this->em->getRepository(CmsUser::class);
475
476
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
477
478
        $repos->createNamedQuery('invalidNamedQuery');
479
    }
480
481
    /**
482
     * @group DDC-1087
483
     */
484
    public function testIsNullCriteriaDoesNotGenerateAParameter()
485
    {
486
        $repos = $this->em->getRepository(CmsUser::class);
487
        $users = $repos->findBy(['status' => null, 'username' => 'romanb']);
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
488
489
        $params = $this->sqlLoggerStack->queries[$this->sqlLoggerStack->currentQuery]['params'];
490
        self::assertCount(1, $params, "Should only execute with one parameter.");
491
        self::assertEquals(['romanb'], $params);
492
    }
493
494
    public function testIsNullCriteria()
495
    {
496
        $this->loadFixture();
497
498
        $repos = $this->em->getRepository(CmsUser::class);
499
500
        $users = $repos->findBy(['status' => null]);
501
        self::assertCount(1, $users);
502
    }
503
504
    /**
505
     * @group DDC-1094
506
     */
507
    public function testFindByLimitOffset()
508
    {
509
        $this->loadFixture();
510
511
        $repos = $this->em->getRepository(CmsUser::class);
512
513
        $users1 = $repos->findBy([], null, 1, 0);
514
        $users2 = $repos->findBy([], null, 1, 1);
515
516
        self::assertCount(4, $repos->findBy([]));
517
        self::assertCount(1, $users1);
518
        self::assertCount(1, $users2);
519
        self::assertNotSame($users1[0], $users2[0]);
520
    }
521
522
    /**
523
     * @group DDC-1094
524
     */
525
    public function testFindByOrderBy()
526
    {
527
        $this->loadFixture();
528
529
        $repos = $this->em->getRepository(CmsUser::class);
530
        $usersAsc = $repos->findBy([], ["username" => "ASC"]);
531
        $usersDesc = $repos->findBy([], ["username" => "DESC"]);
532
533
        self::assertCount(4, $usersAsc, "Pre-condition: only four users in fixture");
534
        self::assertCount(4, $usersDesc, "Pre-condition: only four users in fixture");
535
        self::assertSame($usersAsc[0], $usersDesc[3]);
536
        self::assertSame($usersAsc[3], $usersDesc[0]);
537
    }
538
539
    /**
540
     * @group DDC-1376
541
     */
542
    public function testFindByOrderByAssociation()
543
    {
544
        $this->loadFixtureUserEmail();
545
546
        $repository = $this->em->getRepository(CmsUser::class);
547
        $resultAsc  = $repository->findBy([], ['email' => 'ASC']);
548
        $resultDesc = $repository->findBy([], ['email' => 'DESC']);
549
550
        self::assertCount(3, $resultAsc);
551
        self::assertCount(3, $resultDesc);
552
553
        self::assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId());
554
        self::assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId());
555
    }
556
557
    /**
558
     * @group DDC-1426
559
     */
560
    public function testFindFieldByMagicCallOrderBy()
561
    {
562
        $this->loadFixture();
563
        $repos = $this->em->getRepository(CmsUser::class);
564
565
        $usersAsc = $repos->findByStatus('dev', ['username' => "ASC"]);
566
        $usersDesc = $repos->findByStatus('dev', ['username' => "DESC"]);
567
568
        self::assertCount(2, $usersAsc);
569
        self::assertCount(2, $usersDesc);
570
571
        self::assertInstanceOf(CmsUser::class, $usersAsc[0]);
572
        self::assertEquals('Alexander', $usersAsc[0]->name);
573
        self::assertEquals('dev', $usersAsc[0]->status);
574
575
        self::assertSame($usersAsc[0], $usersDesc[1]);
576
        self::assertSame($usersAsc[1], $usersDesc[0]);
577
    }
578
579
    /**
580
     * @group DDC-1426
581
     */
582
    public function testFindFieldByMagicCallLimitOffset()
583
    {
584
        $this->loadFixture();
585
        $repos = $this->em->getRepository(CmsUser::class);
586
587
        $users1 = $repos->findByStatus('dev', [], 1, 0);
588
        $users2 = $repos->findByStatus('dev', [], 1, 1);
589
590
        self::assertCount(1, $users1);
591
        self::assertCount(1, $users2);
592
        self::assertNotSame($users1[0], $users2[0]);
593
    }
594
595
    /**
596
     * @group DDC-753
597
     */
598
    public function testDefaultRepositoryClassName()
599
    {
600
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
601
        $this->em->getConfiguration()->setDefaultRepositoryClassName(DDC753DefaultRepository::class);
602
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class);
603
604
        $repos = $this->em->getRepository(DDC753EntityWithDefaultCustomRepository::class);
605
        self::assertInstanceOf(DDC753DefaultRepository::class, $repos);
606
        self::assertTrue($repos->isDefaultRepository());
607
608
609
        $repos = $this->em->getRepository(DDC753EntityWithCustomRepository::class);
610
        self::assertInstanceOf(DDC753CustomRepository::class, $repos);
611
        self::assertTrue($repos->isCustomRepository());
612
613
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class);
614
        $this->em->getConfiguration()->setDefaultRepositoryClassName(EntityRepository::class);
615
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
616
    }
617
618
    /**
619
     * @group DDC-753
620
     * @expectedException Doctrine\ORM\ORMException
621
     * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. It must be a Doctrine\Common\Persistence\ObjectRepository.
622
     */
623
    public function testSetDefaultRepositoryInvalidClassError()
624
    {
625
        self::assertEquals($this->em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
626
        $this->em->getConfiguration()->setDefaultRepositoryClassName(DDC753InvalidRepository::class);
627
    }
628
629
    /**
630
     * @group DDC-3257
631
     */
632
    public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash()
633
    {
634
        self::assertSame(
635
            $this->em->getRepository('\\' . CmsUser::class),
636
            $this->em->getRepository(CmsUser::class)
637
        );
638
    }
639
640
    /**
641
     * @group DDC-1376
642
     *
643
     * @expectedException Doctrine\ORM\ORMException
644
     * @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association.
645
     */
646
    public function testInvalidOrderByAssociation()
647
    {
648
        $this->em->getRepository(CmsUser::class)
649
            ->findBy(['status' => 'test'], ['address' => 'ASC']);
650
    }
651
652
    /**
653
     * @group DDC-1500
654
     */
655
    public function testInvalidOrientation()
656
    {
657
        $this->expectException(ORMException::class);
658
        $this->expectExceptionMessage('Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username');
659
660
        $repo = $this->em->getRepository(CmsUser::class);
661
        $repo->findBy(['status' => 'test'], ['username' => 'INVALID']);
662
    }
663
664
    /**
665
     * @group DDC-1713
666
     */
667
    public function testFindByAssociationArray()
668
    {
669
        $repo = $this->em->getRepository(CmsAddress::class);
670
        $data = $repo->findBy(['user' => [1, 2, 3]]);
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
671
672
        $query = array_pop($this->sqlLoggerStack->queries);
673
        self::assertEquals([1,2,3], $query['params'][0]);
674
        self::assertEquals(Connection::PARAM_INT_ARRAY, $query['types'][0]);
675
    }
676
677
    /**
678
     * @group DDC-1637
679
     */
680
    public function testMatchingEmptyCriteria()
681
    {
682
        $this->loadFixture();
683
684
        $repository = $this->em->getRepository(CmsUser::class);
685
        $users = $repository->matching(new Criteria());
686
687
        self::assertCount(4, $users);
688
    }
689
690
    /**
691
     * @group DDC-1637
692
     */
693
    public function testMatchingCriteriaEqComparison()
694
    {
695
        $this->loadFixture();
696
697
        $repository = $this->em->getRepository(CmsUser::class);
698
        $users = $repository->matching(new Criteria(
699
            Criteria::expr()->eq('username', 'beberlei')
700
        ));
701
702
        self::assertCount(1, $users);
703
    }
704
705
    /**
706
     * @group DDC-1637
707
     */
708
    public function testMatchingCriteriaNeqComparison()
709
    {
710
        $this->loadFixture();
711
712
        $repository = $this->em->getRepository(CmsUser::class);
713
        $users = $repository->matching(new Criteria(
714
            Criteria::expr()->neq('username', 'beberlei')
715
        ));
716
717
        self::assertCount(3, $users);
718
    }
719
720
    /**
721
     * @group DDC-1637
722
     */
723
    public function testMatchingCriteriaInComparison()
724
    {
725
        $this->loadFixture();
726
727
        $repository = $this->em->getRepository(CmsUser::class);
728
        $users = $repository->matching(new Criteria(
729
            Criteria::expr()->in('username', ['beberlei', 'gblanco'])
730
        ));
731
732
        self::assertCount(2, $users);
733
    }
734
735
    /**
736
     * @group DDC-1637
737
     */
738
    public function testMatchingCriteriaNotInComparison()
739
    {
740
        $this->loadFixture();
741
742
        $repository = $this->em->getRepository(CmsUser::class);
743
        $users = $repository->matching(new Criteria(
744
            Criteria::expr()->notIn('username', ['beberlei', 'gblanco', 'asm89'])
745
        ));
746
747
        self::assertCount(1, $users);
748
    }
749
750
    /**
751
     * @group DDC-1637
752
     */
753
    public function testMatchingCriteriaLtComparison()
754
    {
755
        $firstUserId = $this->loadFixture();
756
757
        $repository = $this->em->getRepository(CmsUser::class);
758
        $users = $repository->matching(new Criteria(
759
            Criteria::expr()->lt('id', $firstUserId + 1)
760
        ));
761
762
        self::assertCount(1, $users);
763
    }
764
765
    /**
766
     * @group DDC-1637
767
     */
768
    public function testMatchingCriteriaLeComparison()
769
    {
770
        $firstUserId = $this->loadFixture();
771
772
        $repository = $this->em->getRepository(CmsUser::class);
773
        $users = $repository->matching(new Criteria(
774
            Criteria::expr()->lte('id', $firstUserId + 1)
775
        ));
776
777
        self::assertCount(2, $users);
778
    }
779
780
    /**
781
     * @group DDC-1637
782
     */
783
    public function testMatchingCriteriaGtComparison()
784
    {
785
        $firstUserId = $this->loadFixture();
786
787
        $repository = $this->em->getRepository(CmsUser::class);
788
        $users = $repository->matching(new Criteria(
789
            Criteria::expr()->gt('id', $firstUserId)
790
        ));
791
792
        self::assertCount(3, $users);
793
    }
794
795
    /**
796
     * @group DDC-1637
797
     */
798
    public function testMatchingCriteriaGteComparison()
799
    {
800
        $firstUserId = $this->loadFixture();
801
802
        $repository = $this->em->getRepository(CmsUser::class);
803
        $users = $repository->matching(new Criteria(
804
            Criteria::expr()->gte('id', $firstUserId)
805
        ));
806
807
        self::assertCount(4, $users);
808
    }
809
810
    /**
811
     * @group DDC-2430
812
     */
813
    public function testMatchingCriteriaAssocationByObjectInMemory()
814
    {
815
        list($userId, $addressId) = $this->loadAssociatedFixture();
816
817
        $user = $this->em->find(CmsUser::class, $userId);
818
819
        $criteria = new Criteria(
820
            Criteria::expr()->eq('user', $user)
821
        );
822
823
        $repository = $this->em->getRepository(CmsAddress::class);
824
        $addresses = $repository->matching($criteria);
825
826
        self::assertCount(1, $addresses);
827
828
        $addresses = new ArrayCollection($repository->findAll());
829
830
        self::assertCount(1, $addresses->matching($criteria));
831
    }
832
833
    /**
834
     * @group DDC-2430
835
     */
836
    public function testMatchingCriteriaAssocationInWithArray()
837
    {
838
        list($userId, $addressId) = $this->loadAssociatedFixture();
839
840
        $user = $this->em->find(CmsUser::class, $userId);
841
842
        $criteria = new Criteria(
843
            Criteria::expr()->in('user', [$user])
844
        );
845
846
        $repository = $this->em->getRepository(CmsAddress::class);
847
        $addresses = $repository->matching($criteria);
848
849
        self::assertCount(1, $addresses);
850
851
        $addresses = new ArrayCollection($repository->findAll());
852
853
        self::assertCount(1, $addresses->matching($criteria));
854
    }
855
856
    public function testMatchingCriteriaContainsComparison()
857
    {
858
        $this->loadFixture();
859
860
        $repository = $this->em->getRepository(CmsUser::class);
861
862
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar')));
863
        self::assertCount(0, $users);
864
865
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom')));
866
        self::assertCount(1, $users);
867
868
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev')));
869
        self::assertCount(2, $users);
870
    }
871
872
    public function testMatchingCriteriaStartsWithComparison()
873
    {
874
        $this->loadFixture();
875
876
        $repository = $this->em->getRepository(CmsUser::class);
877
878
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'Foo')));
879
        self::assertCount(0, $users);
880
881
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'R')));
882
        self::assertCount(1, $users);
883
884
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('status', 'de')));
885
        self::assertCount(2, $users);
886
    }
887
888
    public function testMatchingCriteriaEndsWithComparison()
889
    {
890
        $this->loadFixture();
891
892
        $repository = $this->em->getRepository(CmsUser::class);
893
894
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'foo')));
895
        self::assertCount(0, $users);
896
897
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'oman')));
898
        self::assertCount(1, $users);
899
900
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('status', 'ev')));
901
        self::assertCount(2, $users);
902
    }
903
904
    /**
905
     * @group DDC-2478
906
     */
907
    public function testMatchingCriteriaNullAssocComparison()
908
    {
909
        $fixtures       = $this->loadFixtureUserEmail();
910
        $user           = $this->em->find(get_class($fixtures[0]), $fixtures[0]->id);
911
        $repository     = $this->em->getRepository(CmsUser::class);
912
        $criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email'));
913
        $criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null));
914
915
        $user->setEmail(null);
916
        $this->em->flush();
917
        $this->em->clear();
918
919
        $usersIsNull = $repository->matching($criteriaIsNull);
920
        $usersEqNull = $repository->matching($criteriaEqNull);
921
922
        self::assertCount(1, $usersIsNull);
923
        self::assertCount(1, $usersEqNull);
924
925
        self::assertInstanceOf(CmsUser::class, $usersIsNull[0]);
926
        self::assertInstanceOf(CmsUser::class, $usersEqNull[0]);
927
928
        self::assertNull($usersIsNull[0]->getEmail());
929
        self::assertNull($usersEqNull[0]->getEmail());
930
    }
931
932
    /**
933
     * @group DDC-2055
934
     */
935
    public function testCreateResultSetMappingBuilder()
936
    {
937
        $repository = $this->em->getRepository(CmsUser::class);
938
        $rsm = $repository->createResultSetMappingBuilder('u');
939
940
        self::assertInstanceOf(Query\ResultSetMappingBuilder::class, $rsm);
941
        self::assertEquals(['u' => CmsUser::class], $rsm->aliasMap);
942
    }
943
944
    /**
945
     * @group DDC-3045
946
     */
947
    public function testFindByFieldInjectionPrevented()
948
    {
949
        $this->expectException(ORMException::class);
950
        $this->expectExceptionMessage('Unrecognized field: ');
951
952
        $repository = $this->em->getRepository(CmsUser::class);
953
        $repository->findBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']);
954
    }
955
956
    /**
957
     * @group DDC-3045
958
     */
959
    public function testFindOneByFieldInjectionPrevented()
960
    {
961
        $this->expectException(ORMException::class);
962
        $this->expectExceptionMessage('Unrecognized field: ');
963
964
        $repository = $this->em->getRepository(CmsUser::class);
965
        $repository->findOneBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']);
966
    }
967
968
    /**
969
     * @group DDC-3045
970
     */
971
    public function testMatchingInjectionPrevented()
972
    {
973
        $this->expectException(ORMException::class);
974
        $this->expectExceptionMessage('Unrecognized field: ');
975
976
        $repository = $this->em->getRepository(CmsUser::class);
977
        $result     = $repository->matching(new Criteria(
978
            Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei')
979
        ));
980
981
        // Because repository returns a lazy collection, we call toArray to force initialization
982
        $result->toArray();
983
    }
984
985
    /**
986
     * @group DDC-3045
987
     */
988
    public function testFindInjectionPrevented()
989
    {
990
        $this->expectException(ORMException::class);
991
        $this->expectExceptionMessage('Unrecognized identifier fields: ');
992
993
        $repository = $this->em->getRepository(CmsUser::class);
994
        $repository->find(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test', 'id' => 1]);
995
    }
996
997
    /**
998
     * @group DDC-3056
999
     */
1000
    public function testFindByNullValueInInCondition()
1001
    {
1002
        $user1 = new CmsUser();
1003
        $user2 = new CmsUser();
1004
1005
        $user1->username = 'ocramius';
1006
        $user1->name = 'Marco';
1007
        $user2->status = null;
1008
        $user2->username = 'deeky666';
1009
        $user2->name = 'Steve';
1010
        $user2->status = 'dbal maintainer';
1011
1012
        $this->em->persist($user1);
1013
        $this->em->persist($user2);
1014
        $this->em->flush();
1015
1016
        $users = $this->em->getRepository(CmsUser::class)->findBy(['status' => [null]]);
1017
1018
        self::assertCount(1, $users);
1019
        self::assertSame($user1, reset($users));
1020
    }
1021
1022
    /**
1023
     * @group DDC-3056
1024
     */
1025
    public function testFindByNullValueInMultipleInCriteriaValues()
1026
    {
1027
        $user1 = new CmsUser();
1028
        $user2 = new CmsUser();
1029
1030
        $user1->username = 'ocramius';
1031
        $user1->name = 'Marco';
1032
        $user2->status = null;
1033
        $user2->username = 'deeky666';
1034
        $user2->name = 'Steve';
1035
        $user2->status = 'dbal maintainer';
1036
1037
        $this->em->persist($user1);
1038
        $this->em->persist($user2);
1039
        $this->em->flush();
1040
1041
        $users = $this
1042
            ->em
1043
            ->getRepository(CmsUser::class)
1044
            ->findBy(['status' => ['foo', null]]);
1045
1046
        self::assertCount(1, $users);
1047
        self::assertSame($user1, reset($users));
1048
    }
1049
1050
    /**
1051
     * @group DDC-3056
1052
     */
1053
    public function testFindMultipleByNullValueInMultipleInCriteriaValues()
1054
    {
1055
        $user1 = new CmsUser();
1056
        $user2 = new CmsUser();
1057
1058
        $user1->username = 'ocramius';
1059
        $user1->name = 'Marco';
1060
        $user2->status = null;
1061
        $user2->username = 'deeky666';
1062
        $user2->name = 'Steve';
1063
        $user2->status = 'dbal maintainer';
1064
1065
        $this->em->persist($user1);
1066
        $this->em->persist($user2);
1067
        $this->em->flush();
1068
1069
        $users = $this
1070
            ->em
1071
            ->getRepository(CmsUser::class)
1072
            ->findBy(['status' => ['dbal maintainer', null]]);
1073
1074
        self::assertCount(2, $users);
1075
1076
        foreach ($users as $user) {
1077
            self::assertContains($user, [$user1, $user2]);
1078
        }
1079
    }
1080
}
1081