Complex classes like PoolTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PoolTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class PoolTest extends TestCase |
||
25 | { |
||
26 | /** |
||
27 | * @var Container |
||
28 | */ |
||
29 | private $container; |
||
30 | |||
31 | /** |
||
32 | * @var Pool |
||
33 | */ |
||
34 | private $pool; |
||
35 | |||
36 | protected function setUp(): void |
||
37 | { |
||
38 | $this->container = new Container(); |
||
39 | $this->pool = new Pool($this->container, 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']); |
||
40 | } |
||
41 | |||
42 | public function testGetGroups(): void |
||
43 | { |
||
44 | $this->container->set('sonata.user.admin.group1', $this->createMock(AdminInterface::class)); |
||
45 | |||
46 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
||
47 | |||
48 | $this->pool->setAdminGroups([ |
||
49 | 'adminGroup1' => ['sonata.user.admin.group1' => []], |
||
50 | ]); |
||
51 | |||
52 | $result = $this->pool->getGroups(); |
||
53 | $this->assertArrayHasKey('adminGroup1', $result); |
||
54 | $this->assertArrayHasKey('sonata.user.admin.group1', $result['adminGroup1']); |
||
55 | } |
||
56 | |||
57 | public function testHasGroup(): void |
||
58 | { |
||
59 | $this->pool->setAdminGroups([ |
||
60 | 'adminGroup1' => [], |
||
61 | ]); |
||
62 | |||
63 | $this->assertTrue($this->pool->hasGroup('adminGroup1')); |
||
64 | $this->assertFalse($this->pool->hasGroup('adminGroup2')); |
||
65 | } |
||
66 | |||
67 | public function testGetDashboardGroups(): void |
||
68 | { |
||
69 | $adminGroup1 = $this->createMock(AdminInterface::class); |
||
70 | $adminGroup1->expects($this->once())->method('showIn')->willReturn(true); |
||
71 | |||
72 | $adminGroup2 = $this->createMock(AdminInterface::class); |
||
73 | $adminGroup2->expects($this->once())->method('showIn')->willReturn(false); |
||
74 | |||
75 | $adminGroup3 = $this->createMock(AdminInterface::class); |
||
76 | $adminGroup3->expects($this->once())->method('showIn')->willReturn(false); |
||
77 | |||
78 | $this->container->set('sonata.user.admin.group1', $adminGroup1); |
||
79 | $this->container->set('sonata.user.admin.group2', $adminGroup2); |
||
80 | $this->container->set('sonata.user.admin.group3', $adminGroup3); |
||
81 | |||
82 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']); |
||
83 | |||
84 | $this->pool->setAdminGroups([ |
||
85 | 'adminGroup1' => [ |
||
86 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')], |
||
87 | ], |
||
88 | 'adminGroup2' => [ |
||
89 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')], |
||
90 | ], |
||
91 | 'adminGroup3' => [ |
||
92 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')], |
||
93 | ], |
||
94 | ]); |
||
95 | |||
96 | $groups = $this->pool->getDashboardGroups(); |
||
97 | |||
98 | $this->assertCount(1, $groups); |
||
99 | $this->assertSame($adminGroup1, $groups['adminGroup1']['items']['itemKey']); |
||
100 | } |
||
101 | |||
102 | public function testGetAdminsByGroupWhenGroupNotSet(): void |
||
103 | { |
||
104 | $this->expectException(\InvalidArgumentException::class); |
||
105 | |||
106 | $this->pool->setAdminGroups([ |
||
107 | 'adminGroup1' => [], |
||
108 | ]); |
||
109 | |||
110 | $this->pool->getAdminsByGroup('adminGroup2'); |
||
111 | } |
||
112 | |||
113 | public function testGetAdminsByGroupWhenGroupIsEmpty(): void |
||
114 | { |
||
115 | $this->pool->setAdminGroups([ |
||
116 | 'adminGroup1' => [], |
||
117 | ]); |
||
118 | |||
119 | $this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1')); |
||
120 | } |
||
121 | |||
122 | public function testGetAdminsByGroup(): void |
||
123 | { |
||
124 | $this->container->set('sonata.admin1', $this->createMock(AdminInterface::class)); |
||
125 | $this->container->set('sonata.admin2', $this->createMock(AdminInterface::class)); |
||
126 | $this->container->set('sonata.admin3', $this->createMock(AdminInterface::class)); |
||
127 | |||
128 | $this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']); |
||
129 | $this->pool->setAdminGroups([ |
||
130 | 'adminGroup1' => [ |
||
131 | 'items' => [ |
||
132 | $this->getItemArray('sonata.admin1'), |
||
133 | $this->getItemArray('sonata.admin2'), |
||
134 | ], |
||
135 | ], |
||
136 | 'adminGroup2' => [ |
||
137 | 'items' => [$this->getItemArray('sonata.admin3')], |
||
138 | ], |
||
139 | ]); |
||
140 | |||
141 | $this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1')); |
||
142 | $this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2')); |
||
143 | } |
||
144 | |||
145 | public function testGetAdminForClassWithInvalidFormat(): void |
||
146 | { |
||
147 | $this->expectException(\RuntimeException::class); |
||
148 | |||
149 | $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
||
150 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
151 | |||
152 | $this->pool->getAdminByClass('someclass'); |
||
153 | } |
||
154 | |||
155 | public function testGetAdminForClassWithTooManyRegisteredAdmin(): void |
||
156 | { |
||
157 | $this->expectException(\RuntimeException::class); |
||
158 | |||
159 | $this->pool->setAdminClasses([ |
||
160 | 'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'], |
||
161 | ]); |
||
162 | |||
163 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
164 | $this->pool->getAdminByClass('someclass'); |
||
165 | } |
||
166 | |||
167 | public function testGetAdminForClassWhenAdminClassIsSet(): void |
||
168 | { |
||
169 | $this->container->set('sonata.user.admin.group1', $this->createMock(AdminInterface::class)); |
||
170 | |||
171 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
||
172 | $this->pool->setAdminClasses([ |
||
173 | 'someclass' => ['sonata.user.admin.group1'], |
||
174 | ]); |
||
175 | |||
176 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
177 | $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByClass('someclass')); |
||
178 | } |
||
179 | |||
180 | public function testGetInstanceWithUndefinedServiceId(): void |
||
181 | { |
||
182 | $this->expectException(\InvalidArgumentException::class); |
||
183 | $this->expectExceptionMessage('Admin service "sonata.news.admin.post" not found in admin pool.'); |
||
184 | |||
185 | $this->pool->getInstance('sonata.news.admin.post'); |
||
186 | } |
||
187 | |||
188 | public function testGetInstanceWithUndefinedServiceIdAndExistsOther(): void |
||
189 | { |
||
190 | $this->pool->setAdminServiceIds([ |
||
191 | 'sonata.news.admin.post', |
||
192 | 'sonata.news.admin.category', |
||
193 | ]); |
||
194 | |||
195 | $this->expectException(\InvalidArgumentException::class); |
||
196 | $this->expectExceptionMessage('Admin service "sonata.news.admin.pos" not found in admin pool. ' |
||
197 | .'Did you mean "sonata.news.admin.post" ' |
||
198 | .'or one of those: [sonata.news.admin.category]?'); |
||
199 | |||
200 | $this->pool->getInstance('sonata.news.admin.pos'); |
||
201 | } |
||
202 | |||
203 | public function testGetAdminByAdminCode(): void |
||
204 | { |
||
205 | $this->container->set('sonata.news.admin.post', $this->createMock(AdminInterface::class)); |
||
206 | |||
207 | $this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
||
208 | |||
209 | $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByAdminCode('sonata.news.admin.post')); |
||
210 | } |
||
211 | |||
212 | public function testGetAdminByAdminCodeForChildClass(): void |
||
213 | { |
||
214 | $adminMock = $this->createMock(AdminInterface::class); |
||
215 | $adminMock |
||
216 | ->method('hasChild') |
||
217 | ->willReturn(true); |
||
218 | |||
219 | $childAdmin = $this->createMock(AdminInterface::class); |
||
220 | |||
221 | $adminMock->expects($this->once()) |
||
222 | ->method('getChild') |
||
223 | ->with($this->equalTo('sonata.news.admin.comment')) |
||
224 | ->willReturn($childAdmin); |
||
225 | |||
226 | $this->container->set('sonata.news.admin.post', $adminMock); |
||
227 | |||
228 | $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.comment']); |
||
229 | |||
230 | $this->assertSame($childAdmin, $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment')); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @group legacy |
||
235 | * |
||
236 | * @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.50 and will throw an exception in 4.0. |
||
237 | */ |
||
238 | public function testGetAdminByAdminCodeWithInvalidCode(): void |
||
239 | { |
||
240 | $adminMock = $this->createMock(AdminInterface::class); |
||
241 | $adminMock |
||
242 | ->method('hasChild') |
||
243 | ->willReturn(false); |
||
244 | |||
245 | $this->container->set('sonata.news.admin.post', $adminMock); |
||
246 | $this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
||
247 | |||
248 | // NEXT_MAJOR: remove the assertion around getAdminByAdminCode(), remove the "@group" and "@expectedDeprecation" annotations, and uncomment the following line |
||
249 | // $this->expectException(\InvalidArgumentException::class); |
||
250 | $this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid')); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @dataProvider getNonStringAdminServiceNames |
||
255 | * |
||
256 | * @group legacy |
||
257 | * |
||
258 | * @expectedDeprecation Passing a non string value as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.51 and will cause a \TypeError in 4.0. |
||
259 | */ |
||
260 | public function testGetAdminByAdminCodeWithNonStringCode($adminId): void |
||
261 | { |
||
262 | // NEXT_MAJOR: remove the assertion around getAdminByAdminCode(), remove the "@group" and "@expectedDeprecation" annotations, and uncomment the following line |
||
263 | // $this->expectException(\TypeError::class); |
||
264 | $this->assertFalse($this->pool->getAdminByAdminCode($adminId)); |
||
265 | } |
||
266 | |||
267 | public function getNonStringAdminServiceNames(): array |
||
268 | { |
||
269 | return [ |
||
270 | [null], |
||
271 | [false], |
||
272 | [1], |
||
273 | [['some_value']], |
||
274 | [new \stdClass()], |
||
275 | ]; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @group legacy |
||
280 | * |
||
281 | * @expectedDeprecation Passing an invalid admin hierarchy inside argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.51 and will throw an exception in 4.0. |
||
282 | */ |
||
283 | public function testGetAdminByAdminCodeWithCodeNotChild(): void |
||
284 | { |
||
285 | $adminMock = $this->createMock(AdminInterface::class); |
||
286 | $adminMock |
||
287 | ->method('hasChild') |
||
288 | ->willReturn(false); |
||
289 | |||
290 | $this->container->set('sonata.news.admin.post', $adminMock); |
||
291 | $this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.valid']); |
||
292 | $this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid')); |
||
293 | |||
294 | // NEXT_MAJOR: remove the "@group" and "@expectedDeprecation" annotations, the previous assertion and uncomment the following lines |
||
295 | // $this->expectException(\InvalidArgumentException::class); |
||
296 | // $this->expectExceptionMessage('Argument 1 passed to Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() must contain a valid admin hierarchy, "sonata.news.admin.valid" is not a valid child for "sonata.news.admin.post"'); |
||
297 | // |
||
298 | // $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.valid'); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @dataProvider getEmptyRootAdminServiceNames |
||
303 | */ |
||
304 | public function testGetAdminByAdminCodeWithInvalidRootCode(string $adminId): void |
||
305 | { |
||
306 | $adminMock = $this->createMock(AdminInterface::class); |
||
307 | $adminMock->expects($this->never()) |
||
308 | ->method('hasChild'); |
||
309 | |||
310 | /** @var MockObject|Pool $poolMock */ |
||
311 | $poolMock = $this->getMockBuilder(Pool::class) |
||
312 | ->setConstructorArgs([$this->container, 'Sonata', '/path/to/logo.png']) |
||
313 | ->disableOriginalClone() |
||
314 | ->setMethodsExcept(['getAdminByAdminCode']) |
||
315 | ->getMock(); |
||
316 | $poolMock->expects($this->never()) |
||
|
|||
317 | ->method('getInstance'); |
||
318 | |||
319 | $this->expectException(\InvalidArgumentException::class); |
||
320 | $this->expectExceptionMessage('Root admin code must contain a valid admin reference, empty string given.'); |
||
321 | $poolMock->getAdminByAdminCode($adminId); |
||
322 | } |
||
323 | |||
324 | public function getEmptyRootAdminServiceNames() |
||
325 | { |
||
326 | return [ |
||
327 | [''], |
||
328 | [' '], |
||
329 | ['|sonata.news.admin.child_of_empty_code'], |
||
330 | ]; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @dataProvider getInvalidChildAdminServiceNames |
||
335 | * |
||
336 | * @group legacy |
||
337 | * |
||
338 | * @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.50 and will throw an exception in 4.0. |
||
339 | */ |
||
340 | public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): void |
||
341 | { |
||
342 | $adminMock = $this->createMock(AdminInterface::class); |
||
343 | $adminMock |
||
344 | ->method('hasChild') |
||
345 | ->willReturn(false); |
||
346 | $adminMock->expects($this->never()) |
||
347 | ->method('getChild'); |
||
348 | |||
349 | /** @var MockObject|Pool $poolMock */ |
||
350 | $poolMock = $this->getMockBuilder(Pool::class) |
||
351 | ->setConstructorArgs([$this->container, 'Sonata', '/path/to/logo.png']) |
||
352 | ->disableOriginalClone() |
||
353 | ->setMethodsExcept(['getAdminByAdminCode']) |
||
354 | ->getMock(); |
||
355 | $poolMock |
||
356 | ->method('getInstance') |
||
357 | ->willReturn($adminMock); |
||
358 | |||
359 | // NEXT_MAJOR: remove the assertion around getAdminByAdminCode(), remove the "@group" and "@expectedDeprecation" annotations, and uncomment the following line |
||
360 | // $this->expectException(\InvalidArgumentException::class); |
||
361 | $this->assertFalse($poolMock->getAdminByAdminCode($adminId)); |
||
362 | } |
||
363 | |||
364 | public function getInvalidChildAdminServiceNames() |
||
372 | |||
373 | /** |
||
374 | * @dataProvider getAdminServiceNamesToCheck |
||
375 | */ |
||
376 | public function testHasAdminByAdminCode(string $adminId): void |
||
377 | { |
||
378 | $adminMock = $this->createMock(AdminInterface::class); |
||
379 | |||
380 | if (false !== strpos($adminId, '|')) { |
||
381 | $childAdminMock = $this->createMock(AdminInterface::class); |
||
382 | $adminMock |
||
383 | ->method('hasChild') |
||
384 | ->willReturn(true); |
||
385 | $adminMock->expects($this->once()) |
||
386 | ->method('getChild') |
||
387 | ->with($this->equalTo('sonata.news.admin.comment')) |
||
388 | ->willReturn($childAdminMock); |
||
389 | } else { |
||
390 | $adminMock->expects($this->never()) |
||
391 | ->method('hasChild'); |
||
392 | $adminMock->expects($this->never()) |
||
393 | ->method('getChild'); |
||
402 | |||
403 | public function getAdminServiceNamesToCheck() |
||
410 | |||
411 | /** |
||
412 | * @dataProvider getNonStringAdminServiceNames |
||
413 | */ |
||
414 | public function testHasAdminByAdminCodeWithNonStringCode($adminId): void |
||
419 | |||
420 | /** |
||
421 | * @dataProvider getInvalidAdminServiceNamesToCheck |
||
422 | */ |
||
423 | public function testHasAdminByAdminCodeWithInvalidCodes(string $adminId): void |
||
434 | |||
435 | public function getInvalidAdminServiceNamesToCheck() |
||
443 | |||
444 | public function testHasAdminByAdminCodeWithNonExistentCode(): void |
||
448 | |||
449 | /** |
||
450 | * @dataProvider getInvalidChildAdminServiceNamesToCheck |
||
451 | * |
||
452 | * @group legacy |
||
453 | * |
||
454 | * @expectedDeprecation Passing an invalid admin %s argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.%s and will throw an exception in 4.0. |
||
455 | */ |
||
456 | public function testHasAdminByAdminCodeWithInvalidChildCodes(string $adminId): void |
||
471 | |||
472 | public function getInvalidChildAdminServiceNamesToCheck(): array |
||
480 | |||
481 | public function testGetAdminClasses(): void |
||
486 | |||
487 | public function testGetAdminGroups(): void |
||
492 | |||
493 | public function testGetAdminServiceIds(): void |
||
498 | |||
499 | public function testGetContainer(): void |
||
503 | |||
504 | /** |
||
505 | * @group legacy |
||
506 | */ |
||
507 | public function testTemplate(): void |
||
518 | |||
519 | /** |
||
520 | * @group legacy |
||
521 | */ |
||
522 | public function testSetGetTemplates(): void |
||
542 | |||
543 | public function testGetTitleLogo(): void |
||
547 | |||
548 | public function testGetTitle(): void |
||
552 | |||
553 | public function testGetOption(): void |
||
559 | |||
560 | public function testOptionDefault(): void |
||
564 | |||
565 | private function getItemArray(string $serviceId): array |
||
574 | } |
||
575 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.