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