Completed
Pull Request — master (#5775)
by Han Hui
22:06
created

EntityRepositoryTest::testInvalidOrientation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\Tests\Models\CMS\CmsUser;
6
use Doctrine\Tests\Models\CMS\CmsEmail;
7
use Doctrine\Tests\Models\CMS\CmsAddress;
8
use Doctrine\Tests\Models\CMS\CmsGroup;
9
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
10
use Doctrine\Common\Collections\Criteria;
11
use Doctrine\Common\Collections\ArrayCollection;
12
13
/**
14
 * @author robo
15
 */
16
class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
17
{
18
    protected function setUp() {
19
        $this->useModelSet('cms');
20
        parent::setUp();
21
    }
22
23
    public function tearDown()
24
    {
25
        if ($this->_em) {
26
            $this->_em->getConfiguration()->setEntityNamespaces(array());
27
        }
28
        parent::tearDown();
29
    }
30
31
    public function loadFixture()
32
    {
33
        $user = new CmsUser;
34
        $user->name = 'Roman';
35
        $user->username = 'romanb';
36
        $user->status = 'freak';
37
        $this->_em->persist($user);
38
39
        $user2 = new CmsUser;
40
        $user2->name = 'Guilherme';
41
        $user2->username = 'gblanco';
42
        $user2->status = 'dev';
43
        $this->_em->persist($user2);
44
45
        $user3 = new CmsUser;
46
        $user3->name = 'Benjamin';
47
        $user3->username = 'beberlei';
48
        $user3->status = null;
49
        $this->_em->persist($user3);
50
51
        $user4 = new CmsUser;
52
        $user4->name = 'Alexander';
53
        $user4->username = 'asm89';
54
        $user4->status = 'dev';
55
        $this->_em->persist($user4);
56
57
        $this->_em->flush();
58
59
        $user1Id = $user->getId();
60
61
        unset($user);
62
        unset($user2);
63
        unset($user3);
64
        unset($user4);
65
66
        $this->_em->clear();
67
68
        return $user1Id;
69
    }
70
71
    public function loadAssociatedFixture()
72
    {
73
        $address = new CmsAddress();
74
        $address->city = "Berlin";
75
        $address->country = "Germany";
76
        $address->street = "Foostreet";
77
        $address->zip = "12345";
78
79
        $user = new CmsUser();
80
        $user->name = 'Roman';
81
        $user->username = 'romanb';
82
        $user->status = 'freak';
83
        $user->setAddress($address);
84
85
        $this->_em->persist($user);
86
        $this->_em->persist($address);
87
        $this->_em->flush();
88
        $this->_em->clear();
89
90
        return array($user->id, $address->id);
91
    }
92
93
    public function loadFixtureUserEmail()
94
    {
95
        $user1 = new CmsUser();
96
        $user2 = new CmsUser();
97
        $user3 = new CmsUser();
98
99
        $email1 = new CmsEmail();
100
        $email2 = new CmsEmail();
101
        $email3 = new CmsEmail();
102
103
        $user1->name     = 'Test 1';
104
        $user1->username = 'test1';
105
        $user1->status   = 'active';
106
107
        $user2->name     = 'Test 2';
108
        $user2->username = 'test2';
109
        $user2->status   = 'active';
110
111
        $user3->name     = 'Test 3';
112
        $user3->username = 'test3';
113
        $user3->status   = 'active';
114
115
        $email1->email   = '[email protected]';
116
        $email2->email   = '[email protected]';
117
        $email3->email   = '[email protected]';
118
119
        $user1->setEmail($email1);
120
        $user2->setEmail($email2);
121
        $user3->setEmail($email3);
122
123
        $this->_em->persist($user1);
124
        $this->_em->persist($user2);
125
        $this->_em->persist($user3);
126
127
        $this->_em->persist($email1);
128
        $this->_em->persist($email2);
129
        $this->_em->persist($email3);
130
131
        $this->_em->flush();
132
        $this->_em->clear();
133
134
        return array($user1, $user2, $user3);
135
    }
136
137
    public function loadUserWithGroupsFixture()
138
    {
139
        $user = new CmsUser();
140
        $user->username = 'test_user';
141
        $user->name = 'Test User';
142
        $this->_em->persist($user);
143
144
        $group1 = new CmsGroup();
145
        $group1->name = 'Test Group 1';
146
        $this->_em->persist($group1);
147
148
        $group2 = new CmsGroup();
149
        $group2->name = 'Test Group 2';
150
        $this->_em->persist($group2);
151
152
        $user->addGroup($group1);
153
        $user->addGroup($group2);
154
155
        $this->_em->flush();
156
        $this->_em->clear();
157
158
        return array($user->id, array($group1->id, $group2->id));
159
    }
160
161
    public function buildUser($name, $username, $status, $address)
162
    {
163
        $user = new CmsUser();
164
        $user->name     = $name;
165
        $user->username = $username;
166
        $user->status   = $status;
167
        $user->setAddress($address);
168
169
        $this->_em->persist($user);
170
        $this->_em->flush();
171
172
        return $user;
173
    }
174
175
    public function buildAddress($country, $city, $street, $zip)
176
    {
177
        $address = new CmsAddress();
178
        $address->country = $country;
179
        $address->city    = $city;
180
        $address->street  = $street;
181
        $address->zip     = $zip;
182
183
        $this->_em->persist($address);
184
        $this->_em->flush();
185
186
        return $address;
187
    }
188
189
    public function testBasicFind()
190
    {
191
        $user1Id = $this->loadFixture();
192
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
193
194
        $user = $repos->find($user1Id);
195
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$user);
196
        $this->assertEquals('Roman', $user->name);
197
        $this->assertEquals('freak', $user->status);
198
    }
199
200
    public function testFindByField()
201
    {
202
        $user1Id = $this->loadFixture();
0 ignored issues
show
Unused Code introduced by
$user1Id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
203
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
204
205
        $users = $repos->findBy(array('status' => 'dev'));
206
        $this->assertEquals(2, count($users));
207
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$users[0]);
208
        $this->assertEquals('Guilherme', $users[0]->name);
209
        $this->assertEquals('dev', $users[0]->status);
210
    }
211
212
    public function testFindByAssociationWithIntegerAsParameter()
213
    {
214
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
215
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
216
217
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
218
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
219
220
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
221
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
0 ignored issues
show
Unused Code introduced by
$user3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
222
223
        unset($address1);
224
        unset($address2);
225
        unset($address3);
226
227
        $this->_em->clear();
228
229
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
230
        $addresses  = $repository->findBy(array('user' => array($user1->getId(), $user2->getId())));
231
232
        $this->assertEquals(2, count($addresses));
233
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress',$addresses[0]);
234
    }
235
236
    public function testFindByAssociationWithObjectAsParameter()
237
    {
238
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
239
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
240
241
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
242
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
243
244
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
245
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
0 ignored issues
show
Unused Code introduced by
$user3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
246
247
        unset($address1);
248
        unset($address2);
249
        unset($address3);
250
251
        $this->_em->clear();
252
253
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
254
        $addresses  = $repository->findBy(array('user' => array($user1, $user2)));
255
256
        $this->assertEquals(2, count($addresses));
257
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress',$addresses[0]);
258
    }
259
260
    public function testFindFieldByMagicCall()
261
    {
262
        $user1Id = $this->loadFixture();
0 ignored issues
show
Unused Code introduced by
$user1Id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
263
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
264
265
        $users = $repos->findByStatus('dev');
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
266
        $this->assertEquals(2, count($users));
267
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$users[0]);
268
        $this->assertEquals('Guilherme', $users[0]->name);
269
        $this->assertEquals('dev', $users[0]->status);
270
    }
271
272
    public function testFindAll()
273
    {
274
        $user1Id = $this->loadFixture();
0 ignored issues
show
Unused Code introduced by
$user1Id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
275
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
276
277
        $users = $repos->findAll();
278
        $this->assertEquals(4, count($users));
279
    }
280
281
    public function testFindByAlias()
282
    {
283
        $user1Id = $this->loadFixture();
0 ignored issues
show
Unused Code introduced by
$user1Id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
284
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
0 ignored issues
show
Unused Code introduced by
$repos is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
285
286
        $this->_em->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS');
287
288
        $repos = $this->_em->getRepository('CMS:CmsUser');
289
290
        $users = $repos->findAll();
291
        $this->assertEquals(4, count($users));
292
    }
293
294
    /**
295
     * @expectedException \Doctrine\ORM\ORMException
296
     */
297
    public function testExceptionIsThrownWhenCallingFindByWithoutParameter() {
298
        $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
299
                  ->findByStatus();
300
    }
301
302
    /**
303
     * @expectedException \Doctrine\ORM\ORMException
304
     */
305
    public function testExceptionIsThrownWhenUsingInvalidFieldName() {
306
        $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
0 ignored issues
show
Documentation Bug introduced by
The method findByThisFieldDoesNotExist does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
307
                  ->findByThisFieldDoesNotExist('testvalue');
308
    }
309
310
    /**
311
     * @group locking
312
     * @group DDC-178
313
     */
314
    public function testPessimisticReadLockWithoutTransaction_ThrowsException()
315
    {
316
        $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
317
318
        $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
319
                  ->find(1, \Doctrine\DBAL\LockMode::PESSIMISTIC_READ);
320
    }
321
322
    /**
323
     * @group locking
324
     * @group DDC-178
325
     */
326
    public function testPessimisticWriteLockWithoutTransaction_ThrowsException()
327
    {
328
        $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
329
330
        $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
331
                  ->find(1, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE);
332
    }
333
334
    /**
335
     * @group locking
336
     * @group DDC-178
337
     */
338
    public function testOptimisticLockUnversionedEntity_ThrowsException()
339
    {
340
        $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
341
342
        $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
343
                  ->find(1, \Doctrine\DBAL\LockMode::OPTIMISTIC);
344
    }
345
346
    /**
347
     * @group locking
348
     * @group DDC-178
349
     */
350
    public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException()
351
    {
352
        $user = new CmsUser;
353
        $user->name = 'Roman';
354
        $user->username = 'romanb';
355
        $user->status = 'freak';
356
        $this->_em->persist($user);
357
        $this->_em->flush();
358
359
        $userId = $user->id;
360
361
        $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId);
362
363
        $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
364
        $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId, \Doctrine\DBAL\LockMode::OPTIMISTIC);
365
    }
366
367
    /**
368
     * @group DDC-819
369
     */
370
    public function testFindMagicCallByNullValue()
371
    {
372
        $this->loadFixture();
373
374
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
375
376
        $users = $repos->findByStatus(null);
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
377
        $this->assertEquals(1, count($users));
378
    }
379
380
    /**
381
     * @group DDC-819
382
     */
383
    public function testInvalidMagicCall()
384
    {
385
        $this->setExpectedException('BadMethodCallException');
386
387
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
388
        $repos->foo();
0 ignored issues
show
Documentation Bug introduced by
The method foo does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
389
    }
390
391
    /**
392
     * @group DDC-817
393
     */
394
    public function testFindByAssociationKey_ExceptionOnInverseSide()
395
    {
396
        list($userId, $addressId) = $this->loadAssociatedFixture();
0 ignored issues
show
Unused Code introduced by
The assignment to $userId is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
397
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
398
399
        $this->setExpectedException('Doctrine\ORM\ORMException', "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.");
400
        $user = $repos->findBy(array('address' => $addressId));
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
401
    }
402
403
    /**
404
     * @group DDC-817
405
     */
406
    public function testFindOneByAssociationKey()
407
    {
408
        list($userId, $addressId) = $this->loadAssociatedFixture();
409
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
410
        $address = $repos->findOneBy(array('user' => $userId));
411
412
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address);
413
        $this->assertEquals($addressId, $address->id);
414
    }
415
416
    /**
417
     * @group GH-5770
418
     */
419
    public function testFindOneByAssociationKeyManyToMany()
420
    {
421
        list($userId, $groupIds) = $this->loadUserWithGroupsFixture();
422
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
423
        $user = $repos->findOneBy(array('groups' => $groupIds[0]));
424
425
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user);
426
        $this->assertEquals($userId, $user->id);
427
    }
428
429
    /**
430
     * @group DDC-1241
431
     */
432
    public function testFindOneByOrderBy()
433
    {
434
    	$this->loadFixture();
435
436
    	$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
437
    	$userAsc = $repos->findOneBy(array(), array("username" => "ASC"));
438
    	$userDesc = $repos->findOneBy(array(), array("username" => "DESC"));
439
440
    	$this->assertNotSame($userAsc, $userDesc);
441
    }
442
443
    /**
444
     * @group DDC-817
445
     */
446
    public function testFindByAssociationKey()
447
    {
448
        list($userId, $addressId) = $this->loadAssociatedFixture();
449
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
450
        $addresses = $repos->findBy(array('user' => $userId));
451
452
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses);
453
        $this->assertEquals(1, count($addresses));
454
        $this->assertEquals($addressId, $addresses[0]->id);
455
    }
456
457
    /**
458
     * @group GH-5770
459
     */
460
    public function testFindByAssociationKeyManyToMany()
461
    {
462
        list($userId, $groupIds) = $this->loadUserWithGroupsFixture();
463
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
464
        $users = $repos->findBy(array('groups' => $groupIds[0]));
465
466
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $users);
467
        $this->assertEquals(1, count($users));
468
        $this->assertEquals($userId, $users[0]->id);
469
    }
470
471
    /**
472
     * @group DDC-817
473
     */
474
    public function testFindAssociationByMagicCall()
475
    {
476
        list($userId, $addressId) = $this->loadAssociatedFixture();
477
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
478
        $addresses = $repos->findByUser($userId);
0 ignored issues
show
Documentation Bug introduced by
The method findByUser does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
479
480
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses);
481
        $this->assertEquals(1, count($addresses));
482
        $this->assertEquals($addressId, $addresses[0]->id);
483
    }
484
485
    /**
486
     * @group DDC-817
487
     */
488
    public function testFindOneAssociationByMagicCall()
489
    {
490
        list($userId, $addressId) = $this->loadAssociatedFixture();
491
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
492
        $address = $repos->findOneByUser($userId);
0 ignored issues
show
Documentation Bug introduced by
The method findOneByUser does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
493
494
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address);
495
        $this->assertEquals($addressId, $address->id);
496
    }
497
498
    public function testValidNamedQueryRetrieval()
499
    {
500
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
501
502
        $query = $repos->createNamedQuery('all');
503
504
        $this->assertInstanceOf('Doctrine\ORM\Query', $query);
505
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $query->getDQL());
506
    }
507
508
    public function testInvalidNamedQueryRetrieval()
509
    {
510
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
511
512
        $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
513
514
        $repos->createNamedQuery('invalidNamedQuery');
515
    }
516
517
    /**
518
     * @group DDC-1087
519
     */
520
    public function testIsNullCriteriaDoesNotGenerateAParameter()
521
    {
522
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
523
        $users = $repos->findBy(array('status' => null, 'username' => 'romanb'));
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
524
525
        $params = $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['params'];
526
        $this->assertEquals(1, count($params), "Should only execute with one parameter.");
527
        $this->assertEquals(array('romanb'), $params);
528
    }
529
530
    public function testIsNullCriteria()
531
    {
532
        $this->loadFixture();
533
534
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
535
536
        $users = $repos->findBy(array('status' => null));
537
        $this->assertEquals(1, count($users));
538
    }
539
540
    /**
541
     * @group DDC-1094
542
     */
543
    public function testFindByLimitOffset()
544
    {
545
        $this->loadFixture();
546
547
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
548
549
        $users1 = $repos->findBy(array(), null, 1, 0);
550
        $users2 = $repos->findBy(array(), null, 1, 1);
551
552
        $this->assertEquals(4, count($repos->findBy(array())));
553
        $this->assertEquals(1, count($users1));
554
        $this->assertEquals(1, count($users2));
555
        $this->assertNotSame($users1[0], $users2[0]);
556
    }
557
558
    /**
559
     * @group DDC-1094
560
     */
561
    public function testFindByOrderBy()
562
    {
563
        $this->loadFixture();
564
565
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
566
        $usersAsc = $repos->findBy(array(), array("username" => "ASC"));
567
        $usersDesc = $repos->findBy(array(), array("username" => "DESC"));
568
569
        $this->assertEquals(4, count($usersAsc), "Pre-condition: only four users in fixture");
570
        $this->assertEquals(4, count($usersDesc), "Pre-condition: only four users in fixture");
571
        $this->assertSame($usersAsc[0], $usersDesc[3]);
572
        $this->assertSame($usersAsc[3], $usersDesc[0]);
573
    }
574
575
    /**
576
     * @group DDC-1376
577
     */
578
    public function testFindByOrderByAssociation()
579
    {
580
        $this->loadFixtureUserEmail();
581
582
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
583
        $resultAsc  = $repository->findBy(array(), array('email' => 'ASC'));
584
        $resultDesc = $repository->findBy(array(), array('email' => 'DESC'));
585
586
        $this->assertCount(3, $resultAsc);
587
        $this->assertCount(3, $resultDesc);
588
589
        $this->assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId());
590
        $this->assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId());
591
    }
592
593
    /**
594
     * @group DDC-1426
595
     */
596
    public function testFindFieldByMagicCallOrderBy()
597
    {
598
        $this->loadFixture();
599
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
600
601
        $usersAsc = $repos->findByStatus('dev', array('username' => "ASC"));
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
602
        $usersDesc = $repos->findByStatus('dev', array('username' => "DESC"));
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
603
604
        $this->assertEquals(2, count($usersAsc));
605
        $this->assertEquals(2, count($usersDesc));
606
607
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$usersAsc[0]);
608
        $this->assertEquals('Alexander', $usersAsc[0]->name);
609
        $this->assertEquals('dev', $usersAsc[0]->status);
610
611
        $this->assertSame($usersAsc[0], $usersDesc[1]);
612
        $this->assertSame($usersAsc[1], $usersDesc[0]);
613
    }
614
615
    /**
616
     * @group DDC-1426
617
     */
618
    public function testFindFieldByMagicCallLimitOffset()
619
    {
620
        $this->loadFixture();
621
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
622
623
        $users1 = $repos->findByStatus('dev', array(), 1, 0);
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
624
        $users2 = $repos->findByStatus('dev', array(), 1, 1);
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
625
626
        $this->assertEquals(1, count($users1));
627
        $this->assertEquals(1, count($users2));
628
        $this->assertNotSame($users1[0], $users2[0]);
629
    }
630
631
    /**
632
     * @group DDC-753
633
     */
634
    public function testDefaultRepositoryClassName()
635
    {
636
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository");
637
        $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository");
638
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository");
639
640
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository');
641
        $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository", $repos);
642
        $this->assertTrue($repos->isDefaultRepository());
0 ignored issues
show
Documentation Bug introduced by
The method isDefaultRepository does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
643
644
645
        $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository');
646
        $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753CustomRepository", $repos);
647
        $this->assertTrue($repos->isCustomRepository());
0 ignored issues
show
Documentation Bug introduced by
The method isCustomRepository does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
648
649
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository");
650
        $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\ORM\EntityRepository");
651
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository");
652
653
    }
654
655
    /**
656
     * @group DDC-753
657
     * @expectedException Doctrine\ORM\ORMException
658
     * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. It must be a Doctrine\Common\Persistence\ObjectRepository.
659
     */
660
    public function testSetDefaultRepositoryInvalidClassError()
661
    {
662
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository");
663
        $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository");
664
    }
665
666
    /**
667
     * @group DDC-3257
668
     */
669
    public function testSingleRepositoryInstanceForDifferentEntityAliases()
670
    {
671
        $config = $this->_em->getConfiguration();
672
673
        $config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS');
674
        $config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS');
675
676
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
677
678
        $this->assertSame($repository, $this->_em->getRepository('Aliased:CmsUser'));
679
        $this->assertSame($repository, $this->_em->getRepository('AliasedAgain:CmsUser'));
680
    }
681
682
    /**
683
     * @group DDC-3257
684
     */
685
    public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash()
686
    {
687
        $this->assertSame(
688
            $this->_em->getRepository('\\Doctrine\\Tests\\Models\\CMS\\CmsUser'),
689
            $this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsUser')
690
        );
691
    }
692
693
    /**
694
     * @group DDC-1376
695
     *
696
     * @expectedException Doctrine\ORM\ORMException
697
     * @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association.
698
     */
699
    public function testInvalidOrderByAssociation()
700
    {
701
        $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
702
            ->findBy(array('status' => 'test'), array('address' => 'ASC'));
703
    }
704
705
    /**
706
     * @group DDC-1500
707
     */
708
    public function testInvalidOrientation()
709
    {
710
        $this->setExpectedException('Doctrine\ORM\ORMException', 'Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username');
711
712
        $repo = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
713
        $repo->findBy(array('status' => 'test'), array('username' => 'INVALID'));
714
    }
715
716
    /**
717
     * @group DDC-1713
718
     */
719
    public function testFindByAssociationArray()
720
    {
721
        $repo = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsArticle');
722
        $data = $repo->findBy(array('user' => array(1, 2, 3)));
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
723
724
        $query = array_pop($this->_sqlLoggerStack->queries);
725
        $this->assertEquals(array(1,2,3), $query['params'][0]);
726
        $this->assertEquals(\Doctrine\DBAL\Connection::PARAM_INT_ARRAY, $query['types'][0]);
727
    }
728
729
    /**
730
     * @group DDC-1637
731
     */
732
    public function testMatchingEmptyCriteria()
733
    {
734
        $this->loadFixture();
735
736
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
737
        $users = $repository->matching(new Criteria());
738
739
        $this->assertEquals(4, count($users));
740
    }
741
742
    /**
743
     * @group DDC-1637
744
     */
745
    public function testMatchingCriteriaEqComparison()
746
    {
747
        $this->loadFixture();
748
749
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
750
        $users = $repository->matching(new Criteria(
751
            Criteria::expr()->eq('username', 'beberlei')
752
        ));
753
754
        $this->assertEquals(1, count($users));
755
    }
756
757
    /**
758
     * @group DDC-1637
759
     */
760
    public function testMatchingCriteriaNeqComparison()
761
    {
762
        $this->loadFixture();
763
764
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
765
        $users = $repository->matching(new Criteria(
766
            Criteria::expr()->neq('username', 'beberlei')
767
        ));
768
769
        $this->assertEquals(3, count($users));
770
    }
771
772
    /**
773
     * @group DDC-1637
774
     */
775
    public function testMatchingCriteriaInComparison()
776
    {
777
        $this->loadFixture();
778
779
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
780
        $users = $repository->matching(new Criteria(
781
            Criteria::expr()->in('username', array('beberlei', 'gblanco'))
782
        ));
783
784
        $this->assertEquals(2, count($users));
785
    }
786
787
    /**
788
     * @group DDC-1637
789
     */
790
    public function testMatchingCriteriaNotInComparison()
791
    {
792
        $this->loadFixture();
793
794
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
795
        $users = $repository->matching(new Criteria(
796
            Criteria::expr()->notIn('username', array('beberlei', 'gblanco', 'asm89'))
797
        ));
798
799
        $this->assertEquals(1, count($users));
800
    }
801
802
    /**
803
     * @group DDC-1637
804
     */
805
    public function testMatchingCriteriaLtComparison()
806
    {
807
        $firstUserId = $this->loadFixture();
808
809
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
810
        $users = $repository->matching(new Criteria(
811
            Criteria::expr()->lt('id', $firstUserId + 1)
812
        ));
813
814
        $this->assertEquals(1, count($users));
815
    }
816
817
    /**
818
     * @group DDC-1637
819
     */
820
    public function testMatchingCriteriaLeComparison()
821
    {
822
        $firstUserId = $this->loadFixture();
823
824
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
825
        $users = $repository->matching(new Criteria(
826
            Criteria::expr()->lte('id', $firstUserId + 1)
827
        ));
828
829
        $this->assertEquals(2, count($users));
830
    }
831
832
    /**
833
     * @group DDC-1637
834
     */
835
    public function testMatchingCriteriaGtComparison()
836
    {
837
        $firstUserId = $this->loadFixture();
838
839
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
840
        $users = $repository->matching(new Criteria(
841
            Criteria::expr()->gt('id', $firstUserId)
842
        ));
843
844
        $this->assertEquals(3, count($users));
845
    }
846
847
    /**
848
     * @group DDC-1637
849
     */
850
    public function testMatchingCriteriaGteComparison()
851
    {
852
        $firstUserId = $this->loadFixture();
853
854
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
855
        $users = $repository->matching(new Criteria(
856
            Criteria::expr()->gte('id', $firstUserId)
857
        ));
858
859
        $this->assertEquals(4, count($users));
860
    }
861
862
    /**
863
     * @group DDC-2430
864
     */
865
    public function testMatchingCriteriaAssocationByObjectInMemory()
866
    {
867
        list($userId, $addressId) = $this->loadAssociatedFixture();
0 ignored issues
show
Unused Code introduced by
The assignment to $addressId is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
868
869
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId);
870
871
        $criteria = new Criteria(
872
            Criteria::expr()->eq('user', $user)
873
        );
874
875
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
876
        $addresses = $repository->matching($criteria);
877
878
        $this->assertEquals(1, count($addresses));
879
880
        $addresses = new ArrayCollection($repository->findAll());
881
882
        $this->assertEquals(1, count($addresses->matching($criteria)));
883
    }
884
885
    /**
886
     * @group DDC-2430
887
     */
888
    public function testMatchingCriteriaAssocationInWithArray()
889
    {
890
        list($userId, $addressId) = $this->loadAssociatedFixture();
0 ignored issues
show
Unused Code introduced by
The assignment to $addressId is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
891
892
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId);
893
894
        $criteria = new Criteria(
895
            Criteria::expr()->in('user', array($user))
896
        );
897
898
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
899
        $addresses = $repository->matching($criteria);
900
901
        $this->assertEquals(1, count($addresses));
902
903
        $addresses = new ArrayCollection($repository->findAll());
904
905
        $this->assertEquals(1, count($addresses->matching($criteria)));
906
    }
907
908
    public function testMatchingCriteriaContainsComparison()
909
    {
910
        $this->loadFixture();
911
912
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
913
914
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar')));
915
        $this->assertEquals(0, count($users));
916
917
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom')));
918
        $this->assertEquals(1, count($users));
919
920
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev')));
921
        $this->assertEquals(2, count($users));
922
    }
923
924
    /**
925
     * @group DDC-2478
926
     */
927
    public function testMatchingCriteriaNullAssocComparison()
928
    {
929
        $fixtures       = $this->loadFixtureUserEmail();
930
        $user           = $this->_em->merge($fixtures[0]);
931
        $repository     = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
932
        $criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email'));
933
        $criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null));
934
935
        $user->setEmail(null);
936
        $this->_em->persist($user);
937
        $this->_em->flush();
938
        $this->_em->clear();
939
940
        $usersIsNull = $repository->matching($criteriaIsNull);
941
        $usersEqNull = $repository->matching($criteriaEqNull);
942
943
        $this->assertCount(1, $usersIsNull);
944
        $this->assertCount(1, $usersEqNull);
945
946
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $usersIsNull[0]);
947
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $usersEqNull[0]);
948
949
        $this->assertNull($usersIsNull[0]->getEmail());
950
        $this->assertNull($usersEqNull[0]->getEmail());
951
    }
952
953
    /**
954
     * @group DDC-2055
955
     */
956
    public function testCreateResultSetMappingBuilder()
957
    {
958
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
959
        $rsm = $repository->createResultSetMappingBuilder('u');
960
961
        $this->assertInstanceOf('Doctrine\ORM\Query\ResultSetMappingBuilder', $rsm);
962
        $this->assertEquals(array('u' => 'Doctrine\Tests\Models\CMS\CmsUser'), $rsm->aliasMap);
963
    }
964
965
    /**
966
     * @group DDC-3045
967
     */
968
    public function testFindByFieldInjectionPrevented()
969
    {
970
        $this->setExpectedException('Doctrine\ORM\ORMException', 'Unrecognized field: ');
971
972
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
973
        $repository->findBy(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test'));
974
    }
975
976
    /**
977
     * @group DDC-3045
978
     */
979
    public function testFindOneByFieldInjectionPrevented()
980
    {
981
        $this->setExpectedException('Doctrine\ORM\ORMException', 'Unrecognized field: ');
982
983
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
984
        $repository->findOneBy(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test'));
985
    }
986
987
    /**
988
     * @group DDC-3045
989
     */
990
    public function testMatchingInjectionPrevented()
991
    {
992
        $this->setExpectedException('Doctrine\ORM\ORMException', 'Unrecognized field: ');
993
994
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
995
        $result     = $repository->matching(new Criteria(
996
            Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei')
997
        ));
998
999
        // Because repository returns a lazy collection, we call toArray to force initialization
1000
        $result->toArray();
1001
    }
1002
1003
    /**
1004
     * @group DDC-3045
1005
     */
1006
    public function testFindInjectionPrevented()
1007
    {
1008
        $this->setExpectedException('Doctrine\ORM\ORMException', 'Unrecognized identifier fields: ');
1009
1010
        $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
1011
        $repository->find(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test', 'id' => 1));
1012
    }
1013
1014
    /**
1015
     * @group DDC-3056
1016
     */
1017
    public function testFindByNullValueInInCondition()
1018
    {
1019
        $user1 = new CmsUser();
1020
        $user2 = new CmsUser();
1021
1022
        $user1->username = 'ocramius';
1023
        $user1->name = 'Marco';
1024
        $user2->status = null;
1025
        $user2->username = 'deeky666';
1026
        $user2->name = 'Steve';
1027
        $user2->status = 'dbal maintainer';
1028
1029
        $this->_em->persist($user1);
1030
        $this->_em->persist($user2);
1031
        $this->_em->flush();
1032
1033
        $users = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => array(null)));
1034
1035
        $this->assertCount(1, $users);
1036
        $this->assertSame($user1, reset($users));
1037
    }
1038
1039
    /**
1040
     * @group DDC-3056
1041
     */
1042
    public function testFindByNullValueInMultipleInCriteriaValues()
1043
    {
1044
        $user1 = new CmsUser();
1045
        $user2 = new CmsUser();
1046
1047
        $user1->username = 'ocramius';
1048
        $user1->name = 'Marco';
1049
        $user2->status = null;
1050
        $user2->username = 'deeky666';
1051
        $user2->name = 'Steve';
1052
        $user2->status = 'dbal maintainer';
1053
1054
        $this->_em->persist($user1);
1055
        $this->_em->persist($user2);
1056
        $this->_em->flush();
1057
1058
        $users = $this
1059
            ->_em
1060
            ->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
1061
            ->findBy(array('status' => array('foo', null)));
1062
1063
        $this->assertCount(1, $users);
1064
        $this->assertSame($user1, reset($users));
1065
    }
1066
1067
    /**
1068
     * @group DDC-3056
1069
     */
1070
    public function testFindMultipleByNullValueInMultipleInCriteriaValues()
1071
    {
1072
        $user1 = new CmsUser();
1073
        $user2 = new CmsUser();
1074
1075
        $user1->username = 'ocramius';
1076
        $user1->name = 'Marco';
1077
        $user2->status = null;
1078
        $user2->username = 'deeky666';
1079
        $user2->name = 'Steve';
1080
        $user2->status = 'dbal maintainer';
1081
1082
        $this->_em->persist($user1);
1083
        $this->_em->persist($user2);
1084
        $this->_em->flush();
1085
1086
        $users = $this
1087
            ->_em
1088
            ->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
1089
            ->findBy(array('status' => array('dbal maintainer', null)));
1090
1091
        $this->assertCount(2, $users);
1092
1093
        foreach ($users as $user) {
1094
            $this->assertTrue(in_array($user, array($user1, $user2)));
1095
        }
1096
    }
1097
}
1098
1099