We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 40 |
Total Lines | 532 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 8 | Features | 2 |
Complex classes like CrudPanelReadTest 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.
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 CrudPanelReadTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class CrudPanelReadTest extends BaseDBCrudPanelTest |
||
13 | { |
||
14 | private $relationshipColumn = [ |
||
15 | 'name' => 'user_id', |
||
16 | 'type' => 'select', |
||
17 | 'entity' => 'user', |
||
18 | 'attribute' => 'name', |
||
19 | ]; |
||
20 | |||
21 | private $relationshipMultipleColumn = [ |
||
|
|||
22 | 'name' => 'roles', |
||
23 | 'type' => 'select', |
||
24 | 'entity' => 'roles', |
||
25 | 'attribute' => 'name', |
||
26 | 'model' => Role::class, |
||
27 | ]; |
||
28 | |||
29 | private $nonRelationshipColumn = [ |
||
30 | 'name' => 'field1', |
||
31 | 'label' => 'Field1', |
||
32 | ]; |
||
33 | |||
34 | private $articleFieldsArray = [ |
||
35 | [ |
||
36 | 'name' => 'content', |
||
37 | 'label' => 'The Content', |
||
38 | 'type' => 'text', |
||
39 | ], |
||
40 | [ |
||
41 | 'name' => 'metas', |
||
42 | 'label' => 'Metas', |
||
43 | ], |
||
44 | [ |
||
45 | 'name' => 'tags', |
||
46 | ], |
||
47 | [ |
||
48 | 'name' => 'extras', |
||
49 | ], |
||
50 | ]; |
||
51 | |||
52 | private $expectedCreateFormArticleFieldsArray = [ |
||
53 | 'content' => [ |
||
54 | 'name' => 'content', |
||
55 | 'label' => 'The Content', |
||
56 | 'type' => 'text', |
||
57 | ], |
||
58 | 'metas' => [ |
||
59 | 'name' => 'metas', |
||
60 | 'label' => 'Metas', |
||
61 | 'type' => 'text', |
||
62 | ], |
||
63 | 'tags' => [ |
||
64 | 'name' => 'tags', |
||
65 | 'label' => 'Tags', |
||
66 | 'type' => 'text', |
||
67 | ], |
||
68 | 'extras' => [ |
||
69 | 'name' => 'extras', |
||
70 | 'label' => 'Extras', |
||
71 | 'type' => 'text', |
||
72 | ], |
||
73 | ]; |
||
74 | |||
75 | private $expectedUpdateFormArticleFieldsArray = [ |
||
76 | 'content' => [ |
||
77 | 'name' => 'content', |
||
78 | 'label' => 'The Content', |
||
79 | 'type' => 'text', |
||
80 | 'value' => 'Some Content', |
||
81 | ], |
||
82 | 'metas' => [ |
||
83 | 'name' => 'metas', |
||
84 | 'label' => 'Metas', |
||
85 | 'type' => 'text', |
||
86 | 'value' => '{"meta_title":"Meta Title Value","meta_description":"Meta Description Value"}', |
||
87 | ], |
||
88 | 'tags' => [ |
||
89 | 'name' => 'tags', |
||
90 | 'label' => 'Tags', |
||
91 | 'type' => 'text', |
||
92 | 'value' => '{"tags":["tag1","tag2","tag3"]}', |
||
93 | ], |
||
94 | 'extras' => [ |
||
95 | 'name' => 'extras', |
||
96 | 'label' => 'Extras', |
||
97 | 'type' => 'text', |
||
98 | 'value' => '{"extra_details":["detail1","detail2","detail3"]}', |
||
99 | ], |
||
100 | 'id' => [ |
||
101 | 'name' => 'id', |
||
102 | 'type' => 'hidden', |
||
103 | 'value' => 1, |
||
104 | ], |
||
105 | ]; |
||
106 | |||
107 | private $uploadField = [ |
||
108 | 'name' => 'image', |
||
109 | 'label' => 'Image', |
||
110 | 'type' => 'upload', |
||
111 | 'upload' => true, |
||
112 | ]; |
||
113 | |||
114 | private $multipleUploadField = [ |
||
115 | 'name' => 'photos', |
||
116 | 'label' => 'Photos', |
||
117 | 'type' => 'upload_multiple', |
||
118 | 'upload' => true, |
||
119 | ]; |
||
120 | |||
121 | private $defaultPaginator = [[10, 20, 30], ['t1', 't2', 't3']]; |
||
122 | |||
123 | public function testGetEntry() |
||
124 | { |
||
125 | $this->crudPanel->setModel(User::class); |
||
126 | $user = User::find(1); |
||
127 | |||
128 | $entry = $this->crudPanel->getEntry($user->id); |
||
129 | |||
130 | $this->assertEquals($user, $entry); |
||
131 | } |
||
132 | |||
133 | public function testGetEntryWithFakes() |
||
134 | { |
||
135 | $this->markTestIncomplete('Not correctly implemented'); |
||
136 | |||
137 | $this->crudPanel->setModel(Article::class); |
||
138 | $article = Article::find(1); |
||
139 | |||
140 | $entry = $this->crudPanel->getEntry($article->id); |
||
141 | |||
142 | // TODO: the withFakes call is needed for this to work. the state of the model should not be changed by the |
||
143 | // getEntry method. the transformation of the fakes columns should be kept in a different crud panel |
||
144 | // attribute or, at most, the getEntry method should be renamed. |
||
145 | $article->withFakes(); |
||
146 | |||
147 | $this->assertEquals($article, $entry); |
||
148 | } |
||
149 | |||
150 | public function testGetEntryExists() |
||
151 | { |
||
152 | $this->crudPanel->setModel(User::class); |
||
153 | $userEntry = $this->crudPanel->getEntry(1); |
||
154 | |||
155 | $this->assertInstanceOf(User::class, $userEntry); |
||
156 | |||
157 | $this->crudPanel->setModel(Article::class); |
||
158 | $articleEntry = $this->crudPanel->getEntry(1); |
||
159 | |||
160 | $this->assertInstanceOf(Article::class, $articleEntry); |
||
161 | } |
||
162 | |||
163 | public function testGetEntryUnknownId() |
||
164 | { |
||
165 | $this->expectException(ModelNotFoundException::class); |
||
166 | |||
167 | $this->crudPanel->setModel(User::class); |
||
168 | |||
169 | $unknownId = DB::getPdo()->lastInsertId() + 2; |
||
170 | $this->crudPanel->getEntry($unknownId); |
||
171 | } |
||
172 | |||
173 | public function testAutoEagerLoadRelationshipColumns() |
||
174 | { |
||
175 | $this->crudPanel->setModel(Article::class); |
||
176 | $this->crudPanel->setOperation('list'); |
||
177 | $this->crudPanel->addColumn($this->relationshipColumn); |
||
178 | |||
179 | $this->crudPanel->autoEagerLoadRelationshipColumns(); |
||
180 | |||
181 | $this->assertArrayHasKey('user', $this->crudPanel->query->getEagerLoads()); |
||
182 | } |
||
183 | |||
184 | public function testAutoEagerLoadRelationshipColumnsNoRelationships() |
||
185 | { |
||
186 | $this->crudPanel->setModel(Article::class); |
||
187 | $this->crudPanel->addColumn($this->nonRelationshipColumn); |
||
188 | |||
189 | $this->crudPanel->autoEagerLoadRelationshipColumns(); |
||
190 | |||
191 | $this->assertEmpty($this->crudPanel->query->getEagerLoads()); |
||
192 | } |
||
193 | |||
194 | public function testGetEntries() |
||
195 | { |
||
196 | $this->crudPanel->setModel(User::class); |
||
197 | |||
198 | $entries = $this->crudPanel->getEntries(); |
||
199 | |||
200 | $this->assertInstanceOf(Collection::class, $entries); |
||
201 | $this->assertEquals(2, $entries->count()); |
||
202 | $this->assertEquals(User::find(1), $entries->first()); |
||
203 | } |
||
204 | |||
205 | public function testGetEntriesWithFakes() |
||
206 | { |
||
207 | $this->markTestIncomplete('Not correctly implemented'); |
||
208 | |||
209 | $this->crudPanel->setModel(Article::class); |
||
210 | |||
211 | $entries = $this->crudPanel->getEntries(); |
||
212 | |||
213 | // TODO: the getEntries method automatically adds fakes. the state of the models should not be changed by the |
||
214 | // getEntries method. at most, the getEntries method should be renamed. |
||
215 | $this->assertInstanceOf(Collection::class, $entries); |
||
216 | $this->assertEquals(1, $entries->count()); |
||
217 | $this->assertEquals(Article::find(1), $entries->first()); |
||
218 | } |
||
219 | |||
220 | public function testGetFieldsCreateForm() |
||
221 | { |
||
222 | $this->crudPanel->addFields($this->articleFieldsArray); |
||
223 | |||
224 | // TODO: update method documentation. the $form parameter does not default to 'both'. |
||
225 | $fields = $this->crudPanel->getFields('create'); |
||
226 | |||
227 | $this->assertEquals($this->expectedCreateFormArticleFieldsArray, $fields); |
||
228 | } |
||
229 | |||
230 | public function testGetFieldsUpdateForm() |
||
231 | { |
||
232 | $this->crudPanel->setModel(Article::class); |
||
233 | |||
234 | $this->crudPanel->setOperation('update'); |
||
235 | $this->crudPanel->addFields($this->articleFieldsArray); |
||
236 | |||
237 | // TODO: update method documentation. the $form parameter does not default to 'both'. |
||
238 | $fields = $this->crudPanel->getUpdateFields(1); |
||
239 | |||
240 | $this->assertEquals($this->expectedUpdateFormArticleFieldsArray, $fields); |
||
241 | } |
||
242 | |||
243 | public function testHasUploadFieldsCreateForm() |
||
244 | { |
||
245 | $this->crudPanel->addField($this->uploadField, 'create'); |
||
246 | |||
247 | // TODO: update method documentation. the $form parameter does not default to 'both'. |
||
248 | $hasUploadFields = $this->crudPanel->hasUploadFields('create'); |
||
249 | |||
250 | $this->assertTrue($hasUploadFields); |
||
251 | } |
||
252 | |||
253 | public function testHasMultipleUploadFieldsCreateForm() |
||
254 | { |
||
255 | $this->crudPanel->addField($this->multipleUploadField, 'create'); |
||
256 | |||
257 | // TODO: update method documentation. the $form parameter does not default to 'both'. |
||
258 | $hasMultipleUploadFields = $this->crudPanel->hasUploadFields('create'); |
||
259 | |||
260 | $this->assertTrue($hasMultipleUploadFields); |
||
261 | } |
||
262 | |||
263 | public function testHasUploadFieldsUpdateForm() |
||
264 | { |
||
265 | $this->crudPanel->setModel(Article::class); |
||
266 | $this->crudPanel->addField($this->uploadField, 'update'); |
||
267 | |||
268 | // TODO: update method documentation. the $form parameter does not default to 'both'. |
||
269 | $hasUploadFields = $this->crudPanel->hasUploadFields('update', 1); |
||
270 | |||
271 | $this->assertTrue($hasUploadFields); |
||
272 | } |
||
273 | |||
274 | public function testEnableDetailsRow() |
||
275 | { |
||
276 | $this->crudPanel->setOperation('create'); |
||
277 | $this->crudPanel->enableDetailsRow(); |
||
278 | |||
279 | $this->assertTrue($this->crudPanel->getOperationSetting('detailsRow')); |
||
280 | } |
||
281 | |||
282 | public function testDisableDetailsRow() |
||
283 | { |
||
284 | $this->crudPanel->setOperation('list'); |
||
285 | $this->crudPanel->disableDetailsRow(); |
||
286 | |||
287 | $this->assertFalse($this->crudPanel->get('list.detailsRow')); |
||
288 | } |
||
289 | |||
290 | public function testSetDefaultPageLength() |
||
291 | { |
||
292 | $pageLength = 20; |
||
293 | $this->crudPanel->setDefaultPageLength($pageLength); |
||
294 | |||
295 | $this->assertEquals($pageLength, $this->crudPanel->getDefaultPageLength()); |
||
296 | } |
||
297 | |||
298 | public function testGetDefaultPageLength() |
||
299 | { |
||
300 | $defaultPageLength = $this->crudPanel->getDefaultPageLength(); |
||
301 | |||
302 | $this->assertEquals(25, $defaultPageLength); |
||
303 | } |
||
304 | |||
305 | public function testEnableExportButtons() |
||
306 | { |
||
307 | $this->crudPanel->enableExportButtons(); |
||
308 | |||
309 | $this->assertTrue($this->crudPanel->exportButtons()); |
||
310 | } |
||
311 | |||
312 | public function testGetExportButtons() |
||
313 | { |
||
314 | $exportButtons = $this->crudPanel->exportButtons(); |
||
315 | |||
316 | $this->assertFalse($exportButtons); |
||
317 | } |
||
318 | |||
319 | public function testGetRelatedEntriesAttributesFromBelongsToMany() |
||
320 | { |
||
321 | $this->crudPanel->setModel(User::class); |
||
322 | $this->crudPanel->setOperation('list'); |
||
323 | $user = $this->crudPanel->getModel()->where('id', 2)->first(); |
||
324 | $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'roles', 'name'); |
||
325 | $this->assertEquals([1 => 'admin', 2 => 'user'], $entries); |
||
326 | } |
||
327 | |||
328 | public function testGetRelatedEntriesAttributesFromBelongsToManyWithAcessor() |
||
329 | { |
||
330 | $this->crudPanel->setModel(User::class); |
||
331 | $this->crudPanel->setOperation('list'); |
||
332 | $user = $this->crudPanel->getModel()->where('id', 2)->first(); |
||
333 | $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'roles', 'role_name'); |
||
334 | $this->assertEquals([1 => 'admin++', 2 => 'user++'], $entries); |
||
335 | } |
||
336 | |||
337 | public function testGetRelatedEntriesAttributesFromHasMany() |
||
338 | { |
||
339 | $this->crudPanel->setModel(User::class); |
||
340 | $this->crudPanel->setOperation('list'); |
||
341 | $user = $this->crudPanel->getModel()->first(); |
||
342 | $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'articles', 'content'); |
||
343 | $this->assertCount(1, $entries); |
||
344 | } |
||
345 | |||
346 | public function testGetRelatedEntriesAttributesFromHasManyWithAcessor() |
||
347 | { |
||
348 | $this->crudPanel->setModel(User::class); |
||
349 | $this->crudPanel->setOperation('list'); |
||
350 | $user = $this->crudPanel->getModel()->first(); |
||
351 | $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'articles', 'content_composed'); |
||
352 | $this->assertCount(1, $entries); |
||
353 | } |
||
354 | |||
355 | public function testGetRelatedEntriesAttributesFromBelongsTo() |
||
362 | } |
||
363 | |||
364 | public function testGetRelatedEntriesAttributesFromBelongsToWithAcessor() |
||
365 | { |
||
366 | $this->crudPanel->setModel(Article::class); |
||
367 | $this->crudPanel->setOperation('list'); |
||
368 | $article = $this->crudPanel->getModel()->first(); |
||
369 | $entries = $this->crudPanel->getRelatedEntriesAttributes($article, 'user', 'name_composed'); |
||
370 | $this->assertCount(1, $entries); |
||
371 | } |
||
372 | |||
373 | public function testGetRelatedEntriesAttributesFromHasOne() |
||
374 | { |
||
375 | $this->crudPanel->setModel(User::class); |
||
376 | $this->crudPanel->setOperation('list'); |
||
377 | $user = $this->crudPanel->getModel()->first(); |
||
378 | $entries = $this->crudPanel->getRelatedEntriesAttributes($user, 'accountDetails', 'nickname'); |
||
379 | $this->assertCount(1, $entries); |
||
380 | } |
||
381 | |||
382 | public function testGetRelatedEntriesAttributesFromHasOneWithAcessor() |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Tests define paginator length with single array [20, 30, 40]. |
||
393 | */ |
||
394 | public function testCrudPanelChangePaginatorLengthSingleArrayNoLabels() |
||
395 | { |
||
396 | $this->crudPanel->setModel(User::class); |
||
397 | $this->crudPanel->setOperation('list'); |
||
398 | $this->crudPanel->setPageLengthMenu([20, 30, 40]); |
||
399 | $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu')); |
||
400 | $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
401 | $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[1])); |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Tests define paginator length with single array [20 => 'v', 30 => 't', 40 => 'q']. |
||
406 | */ |
||
407 | public function testCrudPanelChangePaginatorLengthSingleArrayWithLabels() |
||
408 | { |
||
409 | $this->crudPanel->setModel(User::class); |
||
410 | $this->crudPanel->setOperation('list'); |
||
411 | $this->crudPanel->setPageLengthMenu([20 => 'v', 30 => 't', 40 => 'q']); |
||
412 | $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu')); |
||
413 | $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
414 | $this->assertEquals($this->crudPanel->getOperationSetting('pageLengthMenu')[1], ['v', 't', 'q']); |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Tests define paginator length with and 'all' options as -1 as defined in previous versions of BP. |
||
419 | */ |
||
420 | public function testCrudPanelPaginatorWithAllAsOption() |
||
421 | { |
||
422 | $this->crudPanel->setModel(User::class); |
||
423 | $this->crudPanel->setOperation('list'); |
||
424 | $this->crudPanel->setPageLengthMenu([-1 => 'All']); |
||
425 | $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu')); |
||
426 | $this->assertTrue(in_array(-1, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
427 | |||
428 | $this->crudPanel->setPageLengthMenu([-1, 1]); |
||
429 | $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu')); |
||
430 | $this->assertTrue(in_array(-1, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
431 | |||
432 | $this->crudPanel->setPageLengthMenu(-1); |
||
433 | $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu')); |
||
434 | $this->assertTrue(in_array(-1, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * Tests if paginator aborts when 0 is provided as key. |
||
439 | */ |
||
440 | public function testCrudPanelPaginatorWithZeroAsOption() |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Tests define paginator length with multi array [[20, 30, 40],['v', 't', 'q']]. |
||
476 | */ |
||
477 | public function testCrudPanelChangePaginatorLengthMultiArrayWithLabels() |
||
478 | { |
||
479 | $this->crudPanel->setModel(User::class); |
||
480 | $this->crudPanel->setOperation('list'); |
||
481 | $this->crudPanel->setPageLengthMenu([[20, 30, 40], ['v', 't', 'q']]); |
||
482 | $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu')); |
||
483 | $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
484 | $this->assertEquals($this->crudPanel->getOperationSetting('pageLengthMenu')[1], ['v', 't', 'q']); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Tests define paginator length with multi array [[20, 30, 40]]. |
||
489 | */ |
||
490 | public function testCrudPanelChangePaginatorLengthMultiArrayNoLabels() |
||
491 | { |
||
492 | $this->crudPanel->setModel(User::class); |
||
493 | $this->crudPanel->setOperation('list'); |
||
494 | $this->crudPanel->setPageLengthMenu([[20, 30, 40]]); |
||
495 | $this->assertCount(2, $this->crudPanel->getOperationSetting('pageLengthMenu')); |
||
496 | $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
497 | $this->assertTrue(in_array(40, $this->crudPanel->getOperationSetting('pageLengthMenu')[1])); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Tests define paginator length with single value 40. |
||
502 | */ |
||
503 | public function testCrudPanelChangePaginatorLengthWithSingleValue() |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Tests if table paginator adds default option non-existent at time in the paginator. |
||
515 | */ |
||
516 | public function testCrudPanelPaginatorAddsDefaultOptionNonExistent() |
||
517 | { |
||
518 | $this->crudPanel->setModel(User::class); |
||
519 | $this->crudPanel->setOperation('list'); |
||
520 | $this->crudPanel->setDefaultPageLength(25); |
||
521 | $this->crudPanel->setPageLengthMenu($this->defaultPaginator); |
||
522 | |||
523 | $this->assertCount(2, $this->crudPanel->getPageLengthMenu()); |
||
524 | $this->assertCount(4, $this->crudPanel->getOperationSetting('pageLengthMenu')[0]); |
||
525 | $this->assertTrue(in_array(25, $this->crudPanel->getOperationSetting('pageLengthMenu')[0])); |
||
526 | $this->assertEquals(array_values($this->crudPanel->getPageLengthMenu()[0]), [10, 20, 25, 30]); |
||
527 | $this->assertEquals(array_values($this->crudPanel->getPageLengthMenu()[1]), ['t1', 't2', 25, 't3']); |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Tests if table paginator adds default option existent. |
||
532 | */ |
||
533 | public function testCrudPanelPaginatorAddsDefaultOptionExistent() |
||
544 | } |
||
545 | } |
||
546 |