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 |
||
23 | class PoolTest extends TestCase |
||
24 | { |
||
25 | /** |
||
26 | * @var Container |
||
27 | */ |
||
28 | private $container; |
||
29 | |||
30 | /** |
||
31 | * @var Pool |
||
32 | */ |
||
33 | private $pool; |
||
34 | |||
35 | protected function setUp(): void |
||
36 | { |
||
37 | $this->container = new Container(); |
||
38 | $this->pool = new Pool($this->container, 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']); |
||
39 | } |
||
40 | |||
41 | public function testGetGroups(): void |
||
42 | { |
||
43 | $this->container->set('sonata.user.admin.group1', $this->createMock(AdminInterface::class)); |
||
44 | |||
45 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
||
46 | |||
47 | $this->pool->setAdminGroups([ |
||
48 | 'adminGroup1' => ['sonata.user.admin.group1' => []], |
||
49 | ]); |
||
50 | |||
51 | $result = $this->pool->getGroups(); |
||
52 | $this->assertArrayHasKey('adminGroup1', $result); |
||
53 | $this->assertArrayHasKey('sonata.user.admin.group1', $result['adminGroup1']); |
||
54 | } |
||
55 | |||
56 | public function testHasGroup(): void |
||
57 | { |
||
58 | $this->pool->setAdminGroups([ |
||
59 | 'adminGroup1' => [], |
||
60 | ]); |
||
61 | |||
62 | $this->assertTrue($this->pool->hasGroup('adminGroup1')); |
||
63 | $this->assertFalse($this->pool->hasGroup('adminGroup2')); |
||
64 | } |
||
65 | |||
66 | public function testGetDashboardGroups(): void |
||
67 | { |
||
68 | $adminGroup1 = $this->createMock(AdminInterface::class); |
||
69 | $adminGroup1->expects($this->once())->method('showIn')->willReturn(true); |
||
70 | |||
71 | $adminGroup2 = $this->createMock(AdminInterface::class); |
||
72 | $adminGroup2->expects($this->once())->method('showIn')->willReturn(false); |
||
73 | |||
74 | $adminGroup3 = $this->createMock(AdminInterface::class); |
||
75 | $adminGroup3->expects($this->once())->method('showIn')->willReturn(false); |
||
76 | |||
77 | $this->container->set('sonata.user.admin.group1', $adminGroup1); |
||
78 | $this->container->set('sonata.user.admin.group2', $adminGroup2); |
||
79 | $this->container->set('sonata.user.admin.group3', $adminGroup3); |
||
80 | |||
81 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']); |
||
82 | |||
83 | $this->pool->setAdminGroups([ |
||
84 | 'adminGroup1' => [ |
||
85 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')], |
||
86 | ], |
||
87 | 'adminGroup2' => [ |
||
88 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')], |
||
89 | ], |
||
90 | 'adminGroup3' => [ |
||
91 | 'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')], |
||
92 | ], |
||
93 | ]); |
||
94 | |||
95 | $groups = $this->pool->getDashboardGroups(); |
||
96 | |||
97 | $this->assertCount(1, $groups); |
||
98 | $this->assertSame($adminGroup1, $groups['adminGroup1']['items']['itemKey']); |
||
99 | } |
||
100 | |||
101 | public function testGetAdminsByGroupWhenGroupNotSet(): void |
||
102 | { |
||
103 | $this->expectException(\InvalidArgumentException::class); |
||
104 | |||
105 | $this->pool->setAdminGroups([ |
||
106 | 'adminGroup1' => [], |
||
107 | ]); |
||
108 | |||
109 | $this->pool->getAdminsByGroup('adminGroup2'); |
||
110 | } |
||
111 | |||
112 | public function testGetAdminsByGroupWhenGroupIsEmpty(): void |
||
113 | { |
||
114 | $this->pool->setAdminGroups([ |
||
115 | 'adminGroup1' => [], |
||
116 | ]); |
||
117 | |||
118 | $this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1')); |
||
119 | } |
||
120 | |||
121 | public function testGetAdminsByGroup(): void |
||
122 | { |
||
123 | $this->container->set('sonata.admin1', $this->createMock(AdminInterface::class)); |
||
124 | $this->container->set('sonata.admin2', $this->createMock(AdminInterface::class)); |
||
125 | $this->container->set('sonata.admin3', $this->createMock(AdminInterface::class)); |
||
126 | |||
127 | $this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']); |
||
128 | $this->pool->setAdminGroups([ |
||
129 | 'adminGroup1' => [ |
||
130 | 'items' => [ |
||
131 | $this->getItemArray('sonata.admin1'), |
||
132 | $this->getItemArray('sonata.admin2'), |
||
133 | ], |
||
134 | ], |
||
135 | 'adminGroup2' => [ |
||
136 | 'items' => [$this->getItemArray('sonata.admin3')], |
||
137 | ], |
||
138 | ]); |
||
139 | |||
140 | $this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1')); |
||
141 | $this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2')); |
||
142 | } |
||
143 | |||
144 | public function testGetAdminForClassWithInvalidFormat(): void |
||
145 | { |
||
146 | $this->expectException(\RuntimeException::class); |
||
147 | |||
148 | $this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
||
149 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
150 | |||
151 | $this->pool->getAdminByClass('someclass'); |
||
152 | } |
||
153 | |||
154 | public function testGetAdminForClassWithTooManyRegisteredAdmin(): void |
||
155 | { |
||
156 | $this->expectException(\RuntimeException::class); |
||
157 | |||
158 | $this->pool->setAdminClasses([ |
||
159 | 'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'], |
||
160 | ]); |
||
161 | |||
162 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
163 | $this->pool->getAdminByClass('someclass'); |
||
164 | } |
||
165 | |||
166 | public function testGetAdminForClassWhenAdminClassIsSet(): void |
||
167 | { |
||
168 | $this->container->set('sonata.user.admin.group1', $this->createMock(AdminInterface::class)); |
||
169 | |||
170 | $this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
||
171 | $this->pool->setAdminClasses([ |
||
172 | 'someclass' => ['sonata.user.admin.group1'], |
||
173 | ]); |
||
174 | |||
175 | $this->assertTrue($this->pool->hasAdminByClass('someclass')); |
||
176 | $this->assertInstanceOf(AdminInterface::class, $this->pool->getAdminByClass('someclass')); |
||
177 | } |
||
178 | |||
179 | public function testGetInstanceWithUndefinedServiceId(): void |
||
186 | |||
187 | public function testGetInstanceWithUndefinedServiceIdAndExistsOther(): void |
||
188 | { |
||
189 | $this->pool->setAdminServiceIds([ |
||
190 | 'sonata.news.admin.post', |
||
191 | 'sonata.news.admin.category', |
||
192 | ]); |
||
193 | |||
194 | $this->expectException(\InvalidArgumentException::class); |
||
195 | $this->expectExceptionMessage('Admin service "sonata.news.admin.pos" not found in admin pool. Did you mean "sonata.news.admin.post" or one of those: [sonata.news.admin.category]?'); |
||
196 | |||
199 | |||
200 | public function testGetAdminByAdminCode(): void |
||
208 | |||
209 | public function testGetAdminByAdminCodeForChildClass(): void |
||
229 | |||
230 | public function testGetAdminByAdminCodeWithInvalidCode(): void |
||
245 | |||
246 | public function testGetAdminByAdminCodeWithCodeNotChild(): void |
||
266 | |||
267 | /** |
||
268 | * @dataProvider getNonStringAdminServiceNames |
||
269 | */ |
||
270 | public function testGetAdminByAdminCodeWithNonStringCode($adminId): void |
||
276 | |||
277 | public function getNonStringAdminServiceNames(): array |
||
287 | |||
288 | /** |
||
289 | * @dataProvider getEmptyRootAdminServiceNames |
||
290 | */ |
||
291 | public function testGetAdminByAdminCodeWithInvalidRootCode(string $adminId): void |
||
310 | |||
311 | public function getEmptyRootAdminServiceNames() |
||
319 | |||
320 | /** |
||
321 | * @dataProvider getInvalidChildAdminServiceNames |
||
322 | */ |
||
323 | public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): void |
||
350 | |||
351 | public function getInvalidChildAdminServiceNames() |
||
359 | |||
360 | /** |
||
361 | * @dataProvider getAdminServiceNamesToCheck |
||
362 | */ |
||
363 | public function testHasAdminByAdminCode(string $adminId): void |
||
389 | |||
390 | public function getAdminServiceNamesToCheck() |
||
397 | |||
398 | /** |
||
399 | * @dataProvider getNonStringAdminServiceNames |
||
400 | */ |
||
401 | public function testHasAdminByAdminCodeWithNonStringCode($adminId): void |
||
406 | |||
407 | /** |
||
408 | * @dataProvider getInvalidAdminServiceNamesToCheck |
||
409 | */ |
||
410 | public function testHasAdminByAdminCodeWithInvalidCodes(string $adminId): void |
||
421 | |||
422 | public function getInvalidAdminServiceNamesToCheck() |
||
430 | |||
431 | public function testHasAdminByAdminCodeWithNonExistentCode(): void |
||
435 | |||
436 | /** |
||
437 | * @dataProvider getInvalidChildAdminServiceNamesToCheck |
||
438 | */ |
||
439 | public function testHasAdminByAdminCodeWithInvalidChildCodes(string $adminId): void |
||
454 | |||
455 | public function getInvalidChildAdminServiceNamesToCheck(): array |
||
463 | |||
464 | public function testGetAdminClasses(): void |
||
469 | |||
470 | public function testGetAdminGroups(): void |
||
475 | |||
476 | public function testGetAdminServiceIds(): void |
||
481 | |||
482 | public function testGetContainer(): void |
||
486 | |||
487 | public function testGetTitleLogo(): void |
||
491 | |||
492 | public function testGetTitle(): void |
||
496 | |||
497 | public function testGetOption(): void |
||
503 | |||
504 | public function testOptionDefault(): void |
||
508 | |||
509 | private function getItemArray(string $serviceId): array |
||
518 | } |
||
519 |
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.