1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Repositories; |
4
|
|
|
|
5
|
|
|
use Bludata\Doctrine\ORM\Repositories\QueryWorker; |
6
|
|
|
use DateTime; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
use Tests\TestCase; |
10
|
|
|
|
11
|
|
|
class RotaRepositoryTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function getClassName() |
14
|
|
|
{ |
15
|
|
|
return 'Rota'; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function getRepository() |
19
|
|
|
{ |
20
|
|
|
return bdRepository($this->getClassName()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getMockObject(array $options = []) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
return $this->getRepository() |
26
|
|
|
->createEntity() |
27
|
|
|
->setNome($this->faker->uuid) |
28
|
|
|
->setAlias($this->faker->uuid); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getFlushedMockObject(array $options = []) |
32
|
|
|
{ |
33
|
|
|
return $this->getMockObject($options) |
34
|
|
|
->persist() |
35
|
|
|
->flush(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getMockArray(array $options = []) |
39
|
|
|
{ |
40
|
|
|
return $this->getMockObject($options) |
41
|
|
|
->toArray(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getFlushedMockArray(array $options = []) |
45
|
|
|
{ |
46
|
|
|
return $this->getFlushedMockObject($options) |
47
|
|
|
->toArray(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetEntityManager() |
51
|
|
|
{ |
52
|
|
|
$this->assertInstanceOf(EntityManager::class, $this->getRepository()->getEntityManager()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGetClassMetadata() |
56
|
|
|
{ |
57
|
|
|
$this->assertInstanceOf(ClassMetadata::class, $this->getRepository()->getClassMetadata()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testCreateQueryWorker() |
61
|
|
|
{ |
62
|
|
|
$this->assertInstanceOf(QueryWorker::class, $this->getRepository()->createQueryWorker()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testFindAll() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$entity = $this->getFlushedMockObject(); |
68
|
|
|
|
69
|
|
|
$result = $this->getRepository() |
70
|
|
|
->findAll() |
71
|
|
|
->getResult(); |
72
|
|
|
|
73
|
|
|
$this->assertGreaterThan(0, $result->count()); |
74
|
|
|
$this->assertInstanceOf($this->getRepository()->getClassName(), $result[0]); |
75
|
|
|
|
76
|
|
|
$filteredResultById = $result->filter(function ($element) use ($entity) { |
77
|
|
|
return $element->getId() === $entity->getId(); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
$this->assertEquals(1, $filteredResultById->count()); |
81
|
|
|
|
82
|
|
|
$filteredResultByDeletedAt = $result->filter(function ($element) use ($entity) { |
|
|
|
|
83
|
|
|
return !is_null($element->getDeletedAt()); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
$this->assertEquals(0, $filteredResultByDeletedAt->count()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testFindOneBy() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$entity = $this->getFlushedMockObject(); |
92
|
|
|
|
93
|
|
|
$result = $this->getRepository() |
94
|
|
|
->findOneBy(['id' => $entity->getId()]); |
95
|
|
|
|
96
|
|
|
$this->assertNotNull($result); |
97
|
|
|
$this->assertInstanceOf($this->getRepository()->getClassName(), $result); |
98
|
|
|
$this->assertEquals($entity->getId(), $result->getId()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function testFind() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$entity = $this->getFlushedMockObject(); |
104
|
|
|
|
105
|
|
|
$result = $this->getRepository() |
106
|
|
|
->find($entity->getId()); |
107
|
|
|
|
108
|
|
|
$this->assertNotNull($result); |
109
|
|
|
$this->assertInstanceOf($this->getRepository()->getClassName(), $result); |
110
|
|
|
$this->assertEquals($entity->getId(), $result->getId()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function testFindAllRemoved() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$entity = $this->getFlushedMockObject() |
116
|
|
|
->remove() |
117
|
|
|
->flush(); |
118
|
|
|
|
119
|
|
|
$result = $this->getRepository() |
120
|
|
|
->findAllRemoved() |
121
|
|
|
->getResult(); |
122
|
|
|
|
123
|
|
|
$this->assertGreaterThan(0, $result->count()); |
124
|
|
|
$this->assertInstanceOf($this->getRepository()->getClassName(), $result[0]); |
125
|
|
|
|
126
|
|
|
$filteredResultById = $result->filter(function ($element) use ($entity) { |
127
|
|
|
return $element->getId() === $entity->getId(); |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
$this->assertEquals(1, $filteredResultById->count()); |
131
|
|
|
|
132
|
|
|
$filteredResultByDeletedAt = $result->filter(function ($element) use ($entity) { |
|
|
|
|
133
|
|
|
return !$element->getDeletedAt(); |
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
$this->assertEquals(0, $filteredResultByDeletedAt->count()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testFindRemoved() |
140
|
|
|
{ |
141
|
|
|
$entity = $this->getFlushedMockObject() |
142
|
|
|
->remove() |
143
|
|
|
->flush(); |
144
|
|
|
|
145
|
|
|
$result = $this->getRepository() |
146
|
|
|
->findRemoved($entity->getId()); |
147
|
|
|
|
148
|
|
|
$this->assertNotNull($result); |
149
|
|
|
$this->assertInstanceOf($this->getRepository()->getClassName(), $result); |
150
|
|
|
$this->assertEquals($entity->getId(), $result->getId()); |
151
|
|
|
$this->assertInstanceOf(DateTime::class, $result->getDeletedAt()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testCreateEntity() |
155
|
|
|
{ |
156
|
|
|
$this->assertInstanceOf($this->getRepository()->getClassName(), $this->getRepository()->createEntity()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testRemove() |
160
|
|
|
{ |
161
|
|
|
$entity = $this->getFlushedMockObject(); |
162
|
|
|
|
163
|
|
|
$this->getRepository() |
164
|
|
|
->remove($entity) |
165
|
|
|
->flush(); |
166
|
|
|
|
167
|
|
|
$result = $this->getRepository() |
168
|
|
|
->find($entity->getId(), false); |
169
|
|
|
|
170
|
|
|
$this->assertNull($result); |
171
|
|
|
|
172
|
|
|
$result = $this->getRepository() |
173
|
|
|
->findRemoved($entity->getId()); |
174
|
|
|
|
175
|
|
|
$this->assertNotNull($result); |
176
|
|
|
$this->assertInstanceOf($this->getRepository()->getClassName(), $result); |
177
|
|
|
$this->assertEquals($entity->getId(), $result->getId()); |
178
|
|
|
$this->assertInstanceOf(DateTime::class, $result->getDeletedAt()); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.