|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas\Tests\Doctrine\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
6
|
|
|
use Zenstruck\Porpaginas\Tests\Doctrine\Fixtures\ORMEntity; |
|
7
|
|
|
use Zenstruck\Porpaginas\Tests\Doctrine\Fixtures\ORMEntityRepository; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Kevin Bond <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
final class ORMMatchableRepositoryTest extends MatchableRepositoryTest |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @test |
|
16
|
|
|
*/ |
|
17
|
|
|
public function can_match_one_with_callable(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$this->persistEntities(4); |
|
20
|
|
|
|
|
21
|
|
|
$object = $this->createRepository()->matchOne(function(QueryBuilder $qb, string $alias) { |
|
22
|
|
|
$qb->where("{$alias}.value = :value") |
|
23
|
|
|
->setParameter('value', 'value 2') |
|
24
|
|
|
; |
|
25
|
|
|
}); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertInstanceOf(ORMEntity::class, $object); |
|
28
|
|
|
$this->assertSame('value 2', $object->value); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
*/ |
|
34
|
|
|
public function can_match_with_callable(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->persistEntities(4); |
|
37
|
|
|
|
|
38
|
|
|
$objects = $this->createRepository()->match(function(QueryBuilder $qb, string $alias) { |
|
39
|
|
|
$qb->where("{$alias}.id > :value") |
|
40
|
|
|
->setParameter('value', 2) |
|
41
|
|
|
; |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertCount(2, $objects); |
|
45
|
|
|
$this->assertSame('value 3', \iterator_to_array($objects)[0]->value); |
|
46
|
|
|
$this->assertSame('value 4', \iterator_to_array($objects)[1]->value); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function createRepository(): ORMEntityRepository |
|
50
|
|
|
{ |
|
51
|
|
|
return new ORMEntityRepository($this->em); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|