Completed
Push — master ( 3ca682...b22cda )
by Marco
09:29
created

testMatchingCriteriaGteComparison()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

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