These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\TaggingBundle\Tests\Entity; |
||
4 | |||
5 | use Closure; |
||
6 | use DateTime; |
||
7 | use Doctrine\Common\Collections\ArrayCollection; |
||
8 | use Doctrine\DBAL\Connection; |
||
9 | use Doctrine\DBAL\Platforms\MySqlPlatform; |
||
10 | use Doctrine\ORM\AbstractQuery; |
||
11 | use Doctrine\ORM\Configuration; |
||
12 | use Doctrine\ORM\EntityManager; |
||
13 | use Doctrine\ORM\EntityRepository; |
||
14 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
15 | use Doctrine\ORM\Query\Expr; |
||
16 | use Doctrine\ORM\QueryBuilder; |
||
17 | use Kunstmaan\NodeBundle\Entity\AbstractPage; |
||
18 | use Kunstmaan\TaggingBundle\Entity\LazyLoadingTaggableInterface; |
||
19 | use Kunstmaan\TaggingBundle\Entity\Tag; |
||
20 | use Kunstmaan\TaggingBundle\Entity\Taggable; |
||
21 | use Kunstmaan\TaggingBundle\Entity\TaggableTrait; |
||
22 | use Kunstmaan\TaggingBundle\Entity\TagManager; |
||
23 | use PHPUnit\Framework\TestCase; |
||
24 | |||
25 | class Query extends AbstractQuery |
||
26 | { |
||
27 | public function getSQL() |
||
28 | { |
||
29 | } |
||
30 | |||
31 | protected function _doExecute() |
||
32 | { |
||
33 | } |
||
34 | |||
35 | public function getResult($hydrationMode = self::HYDRATE_OBJECT) |
||
36 | { |
||
37 | return new ArrayCollection(); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | class FakePage extends AbstractPage implements Taggable |
||
0 ignored issues
–
show
|
|||
42 | { |
||
43 | use TaggableTrait; |
||
44 | |||
45 | public function getPossibleChildTypes() |
||
46 | { |
||
47 | return []; |
||
48 | } |
||
49 | |||
50 | public function getTaggableId() |
||
51 | { |
||
52 | return 777; |
||
53 | } |
||
54 | |||
55 | public function getTaggableType() |
||
56 | { |
||
57 | return self::class; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | class Lazy implements LazyLoadingTaggableInterface |
||
0 ignored issues
–
show
|
|||
62 | { |
||
63 | use TaggableTrait; |
||
64 | |||
65 | private $loader; |
||
66 | |||
67 | public function setTagLoader(Closure $loader) |
||
68 | { |
||
69 | $this->loader = $loader; |
||
70 | } |
||
71 | |||
72 | public function getLoader() |
||
73 | { |
||
74 | return $this->loader; |
||
75 | } |
||
76 | |||
77 | public function getId() |
||
78 | { |
||
79 | return 5; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Class TagManagerTest |
||
85 | */ |
||
86 | class TagManagerTest extends TestCase |
||
0 ignored issues
–
show
|
|||
87 | { |
||
88 | /** @var TagManager $object */ |
||
89 | private $object; |
||
90 | |||
91 | private $em; |
||
92 | |||
93 | public function setUp() |
||
94 | { |
||
95 | $tag = new Tag(); |
||
96 | |||
97 | $comparison = $this->getMockBuilder(Expr\Comparison::class) |
||
98 | ->disableOriginalConstructor() |
||
99 | ->getMock(); |
||
100 | |||
101 | $expr = $this->getMockBuilder(Expr::class) |
||
102 | ->disableOriginalConstructor() |
||
103 | ->getMock(); |
||
104 | |||
105 | $expr->expects($this->any()) |
||
106 | ->method('eq') |
||
107 | ->willReturn($comparison); |
||
108 | |||
109 | $query = $this->getMockBuilder(Query::class) |
||
110 | ->disableOriginalConstructor() |
||
111 | ->getMock(); |
||
112 | |||
113 | $builder = $this->getMockBuilder(QueryBuilder::class) |
||
114 | ->disableOriginalConstructor() |
||
115 | ->getMock(); |
||
116 | |||
117 | $builder->expects($this->any()) |
||
118 | ->method('select') |
||
119 | ->willReturn($builder); |
||
120 | |||
121 | $builder->expects($this->any()) |
||
122 | ->method('from') |
||
123 | ->willReturn($builder); |
||
124 | |||
125 | $builder->expects($this->any()) |
||
126 | ->method('where') |
||
127 | ->willReturn($builder); |
||
128 | |||
129 | $builder->expects($this->any()) |
||
130 | ->method('expr') |
||
131 | ->willReturn($expr); |
||
132 | |||
133 | $builder->expects($this->any()) |
||
134 | ->method('getQuery') |
||
135 | ->willReturn($query); |
||
136 | |||
137 | $builder->expects($this->any()) |
||
138 | ->method('innerJoin') |
||
139 | ->willReturn($builder); |
||
140 | |||
141 | $builder->expects($this->any()) |
||
142 | ->method('setParameter') |
||
143 | ->willReturn($builder); |
||
144 | |||
145 | $query->expects($this->any()) |
||
146 | ->method('getOneOrNullResult') |
||
147 | ->willReturn(new Tag()); |
||
148 | |||
149 | $query->expects($this->any()) |
||
150 | ->method('getResult') |
||
151 | ->willReturn([]); |
||
152 | |||
153 | $em = $this->getMockBuilder(EntityManager::class) |
||
154 | ->disableOriginalConstructor() |
||
155 | ->getMock(); |
||
156 | |||
157 | $repo = $this->getMockBuilder(EntityRepository::class) |
||
158 | ->disableOriginalConstructor() |
||
159 | ->getMock(); |
||
160 | |||
161 | $repo->expects($this->any()) |
||
162 | ->method('find') |
||
163 | ->willReturn($tag); |
||
164 | |||
165 | $repo->expects($this->any()) |
||
166 | ->method('findAll') |
||
167 | ->willReturn(new ArrayCollection()); |
||
168 | |||
169 | $em->expects($this->any()) |
||
170 | ->method('getRepository') |
||
171 | ->willReturn($repo); |
||
172 | |||
173 | $em->expects($this->any()) |
||
174 | ->method('createQueryBuilder') |
||
175 | ->willReturn($builder); |
||
176 | |||
177 | $this->object = new TagManager($em); |
||
178 | |||
179 | $this->em = $em; |
||
180 | } |
||
181 | |||
182 | public function testFindById() |
||
183 | { |
||
184 | $result = $this->object->findById(666); |
||
185 | |||
186 | $this->assertInstanceOf(Tag::class, $result); |
||
187 | } |
||
188 | |||
189 | public function testFindByIdReturnsNull() |
||
190 | { |
||
191 | $em = $this->getMockBuilder(EntityManager::class) |
||
192 | ->disableOriginalConstructor() |
||
193 | ->getMock(); |
||
194 | |||
195 | $object = new TagManager($em); |
||
196 | $this->assertNull($object->findById(null)); |
||
197 | } |
||
198 | |||
199 | public function testFindAll() |
||
200 | { |
||
201 | $this->assertInstanceOf(ArrayCollection::class, $this->object->findAll()); |
||
202 | } |
||
203 | |||
204 | public function testFindRelatedItems() |
||
205 | { |
||
206 | $meta = new ClassMetadata(Random::class); |
||
207 | |||
208 | $connection = $this->getMockBuilder(Connection::class) |
||
209 | ->disableOriginalConstructor() |
||
210 | ->getMock(); |
||
211 | |||
212 | $connection->expects($this->any()) |
||
213 | ->method('getDatabasePlatform') |
||
214 | ->willReturn(new MySqlPlatform()); |
||
215 | |||
216 | $this->em->expects($this->any()) |
||
217 | ->method('getConnection') |
||
218 | ->willReturn($connection); |
||
219 | |||
220 | $query = $this->getMockBuilder(Query::class) |
||
221 | ->disableOriginalConstructor() |
||
222 | ->getMock(); |
||
223 | |||
224 | $query->expects($this->any()) |
||
225 | ->method('getResult') |
||
226 | ->willReturn(new ArrayCollection()); |
||
227 | |||
228 | $this->em->expects($this->any()) |
||
229 | ->method('createNativeQuery') |
||
230 | ->willReturn($query); |
||
231 | |||
232 | $this->em->expects($this->any()) |
||
233 | ->method('getClassMetadata') |
||
234 | ->willReturn($meta); |
||
235 | |||
236 | $this->em->expects($this->any()) |
||
237 | ->method('getClassMetadata') |
||
238 | ->willReturn($meta); |
||
239 | |||
240 | $this->object = new TagManager($this->em); |
||
241 | |||
242 | $item = new Random(); |
||
243 | $results = $this->object->findRelatedItems($item, Random::class, 'en'); |
||
244 | $this->assertInstanceOf(ArrayCollection::class, $results); |
||
245 | } |
||
246 | |||
247 | public function testFindRelatedItemsReturnsNull() |
||
248 | { |
||
249 | $item = new Random(); |
||
250 | $results = $this->object->findRelatedItems($item, DateTime::class, 'en'); |
||
251 | $this->assertNull($results); |
||
252 | } |
||
253 | |||
254 | public function testFindRelatedItemsWithAbstractPage() |
||
255 | { |
||
256 | $meta = new ClassMetadata(Random::class); |
||
257 | |||
258 | $connection = $this->getMockBuilder(Connection::class) |
||
259 | ->disableOriginalConstructor() |
||
260 | ->getMock(); |
||
261 | |||
262 | $connection->expects($this->any()) |
||
263 | ->method('getDatabasePlatform') |
||
264 | ->willReturn(new MySqlPlatform()); |
||
265 | |||
266 | $this->em->expects($this->any()) |
||
267 | ->method('getConnection') |
||
268 | ->willReturn($connection); |
||
269 | |||
270 | $query = $this->getMockBuilder(Query::class) |
||
271 | ->disableOriginalConstructor() |
||
272 | ->getMock(); |
||
273 | |||
274 | $query->expects($this->any()) |
||
275 | ->method('getResult') |
||
276 | ->willReturn(new ArrayCollection()); |
||
277 | |||
278 | $this->em->expects($this->any()) |
||
279 | ->method('createNativeQuery') |
||
280 | ->willReturn($query); |
||
281 | |||
282 | $this->em->expects($this->any()) |
||
283 | ->method('getClassMetadata') |
||
284 | ->willReturn($meta); |
||
285 | |||
286 | $this->em->expects($this->any()) |
||
287 | ->method('getClassMetadata') |
||
288 | ->willReturn($meta); |
||
289 | |||
290 | $item = new FakePage(); |
||
291 | $results = $this->object->findRelatedItems($item, Random::class, 'en'); |
||
292 | $this->assertInstanceOf(ArrayCollection::class, $results); |
||
293 | } |
||
294 | |||
295 | public function testTagging() |
||
296 | { |
||
297 | $config = $this->getMockBuilder(Configuration::class) |
||
298 | ->disableOriginalConstructor() |
||
299 | ->getMock(); |
||
300 | |||
301 | $config->expects($this->any()) |
||
302 | ->method('getCustomHydrationMode') |
||
303 | ->willReturn(null); |
||
304 | |||
305 | $config->expects($this->any()) |
||
306 | ->method('addCustomHydrationMode') |
||
307 | ->willReturn(null); |
||
308 | |||
309 | $this->em->expects($this->any()) |
||
310 | ->method('getConfiguration') |
||
311 | ->willReturn($config); |
||
312 | |||
313 | $resource = new FakePage(); |
||
314 | $this->assertNull($this->object->loadTagging($resource)); |
||
315 | } |
||
316 | |||
317 | public function testLoadTagging() |
||
318 | { |
||
319 | $config = $this->getMockBuilder(Configuration::class) |
||
320 | ->disableOriginalConstructor() |
||
321 | ->getMock(); |
||
322 | |||
323 | $config->expects($this->any()) |
||
324 | ->method('getCustomHydrationMode') |
||
325 | ->willReturn(null); |
||
326 | |||
327 | $config->expects($this->any()) |
||
328 | ->method('addCustomHydrationMode') |
||
329 | ->willReturn(null); |
||
330 | |||
331 | $this->em->expects($this->any()) |
||
332 | ->method('getConfiguration') |
||
333 | ->willReturn($config); |
||
334 | |||
335 | $resource = new Lazy(); |
||
336 | $this->assertNull($this->object->loadTagging($resource)); |
||
337 | $loader = $resource->getLoader(); |
||
338 | $loader($resource); |
||
339 | } |
||
340 | } |
||
341 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.