|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas\Tests\Doctrine\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Zenstruck\Porpaginas\Tests\Doctrine\Fixtures\ORMEntity; |
|
7
|
|
|
use Zenstruck\Porpaginas\Tests\Doctrine\Fixtures\ORMEntityRepository; |
|
8
|
|
|
use Zenstruck\Porpaginas\Tests\Doctrine\HasEntityManager; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Kevin Bond <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
final class ORMEntityRepositoryTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
use HasEntityManager; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @test |
|
19
|
|
|
*/ |
|
20
|
|
|
public function can_iterate_and_count(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->persistEntities(4); |
|
23
|
|
|
|
|
24
|
|
|
$repository = new ORMEntityRepository($this->em); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
$this->assertCount(4, \iterator_to_array($repository)); |
|
27
|
|
|
$this->assertCount(4, $repository); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @test |
|
32
|
|
|
*/ |
|
33
|
|
|
public function can_create_batch_processor(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$this->persistEntities(4); |
|
36
|
|
|
|
|
37
|
|
|
$processor = (new ORMEntityRepository($this->em))->batchProcessor(); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->assertCount(4, \iterator_to_array($processor)); |
|
40
|
|
|
$this->assertCount(4, $processor); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function calls_are_passed_to_inner_repository(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->persistEntities(4); |
|
49
|
|
|
|
|
50
|
|
|
$repository = new ORMEntityRepository($this->em); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$entity = $repository->findOneByValue('value 2'); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$this->assertInstanceOf(ORMEntity::class, $entity); |
|
55
|
|
|
$this->assertSame('value 2', $entity->value); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @test |
|
60
|
|
|
*/ |
|
61
|
|
|
public function can_find(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->persistEntities(4); |
|
64
|
|
|
|
|
65
|
|
|
$repository = new ORMEntityRepository($this->em); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$entity = $repository->find(1); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertInstanceOf(ORMEntity::class, $entity); |
|
70
|
|
|
$this->assertSame('value 1', $entity->value); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function can_find_all(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->persistEntities(4); |
|
79
|
|
|
|
|
80
|
|
|
$repository = new ORMEntityRepository($this->em); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$this->assertCount(4, $repository->findAll()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
*/ |
|
88
|
|
|
public function can_find_one_by(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->persistEntities(4); |
|
91
|
|
|
|
|
92
|
|
|
$repository = new ORMEntityRepository($this->em); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$entity = $repository->findOneBy(['value' => 'value 2']); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertInstanceOf(ORMEntity::class, $entity); |
|
97
|
|
|
$this->assertSame('value 2', $entity->value); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @test |
|
102
|
|
|
*/ |
|
103
|
|
|
public function can_find_one_by_with_order(): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->persistEntities(4); |
|
106
|
|
|
|
|
107
|
|
|
$repository = new ORMEntityRepository($this->em); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$entity = $repository->findOneBy([], ['id' => 'DESC']); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertInstanceOf(ORMEntity::class, $entity); |
|
112
|
|
|
$this->assertSame('value 4', $entity->value); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @test |
|
117
|
|
|
*/ |
|
118
|
|
|
public function can_find_by(): void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->persistEntities(4); |
|
121
|
|
|
|
|
122
|
|
|
$repository = new ORMEntityRepository($this->em); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$this->assertCount(1, $repository->findBy(['value' => 'value 2'])); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|