Completed
Push — master ( 3fd2dd...aba624 )
by Grégoire
04:19
created

tests/Show/ShowMapperTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Show;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\BaseFieldDescription;
19
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
20
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
21
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
22
use Sonata\AdminBundle\Model\ModelManagerInterface;
23
use Sonata\AdminBundle\Show\ShowMapper;
24
use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
25
use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
26
27
/**
28
 * Test for ShowMapper.
29
 *
30
 * @author Andrej Hudec <[email protected]>
31
 */
32
class ShowMapperTest extends TestCase
33
{
34
    /**
35
     * @var ShowMapper
36
     */
37
    private $showMapper;
38
39
    /**
40
     * @var AdminInterface
41
     */
42
    private $admin;
43
44
    /**
45
     * @var ShowBuilderInterface
46
     */
47
    private $showBuilder;
48
49
    /**
50
     * @var FieldDescriptionCollection
51
     */
52
    private $fieldDescriptionCollection;
53
54
    /**
55
     * @var array
56
     */
57
    private $groups;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
58
59
    /**
60
     * @var array
61
     */
62
    private $listShowFields;
63
64
    public function setUp(): void
65
    {
66
        $this->showBuilder = $this->getMockForAbstractClass(ShowBuilderInterface::class);
67
        $this->fieldDescriptionCollection = new FieldDescriptionCollection();
68
        $this->admin = $this->getMockForAbstractClass(AdminInterface::class);
69
70
        $this->admin->expects($this->any())
71
            ->method('getLabel')
72
            ->will($this->returnValue('AdminLabel'));
73
74
        $this->admin->expects($this->any())
75
            ->method('getShowTabs')
76
            ->will($this->returnValue([]));
77
78
        $this->groups = [];
79
        $this->listShowFields = [];
80
81
        $this->admin->expects($this->any())
82
            ->method('getShowGroups')
83
            ->will($this->returnCallback(function () {
84
                return $this->groups;
85
            }));
86
87
        $this->admin->expects($this->any())
88
            ->method('setShowGroups')
89
            ->will($this->returnCallback(function ($showGroups): void {
90
                $this->groups = $showGroups;
91
            }));
92
93
        $this->admin->expects($this->any())
94
            ->method('reorderShowGroup')
95
            ->will($this->returnCallback(function ($group, $keys): void {
96
                $this->groups[$group]['fields'] = array_merge(array_flip($keys), $this->groups[$group]['fields']);
97
            }));
98
99
        $modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
100
101
        $modelManager->expects($this->any())
102
            ->method('getNewFieldDescriptionInstance')
103
            ->will($this->returnCallback(function ($class, $name, array $options = []) {
104
                $fieldDescription = $this->getFieldDescriptionMock();
105
                $fieldDescription->setName($name);
106
                $fieldDescription->setOptions($options);
107
108
                return $fieldDescription;
109
            }));
110
111
        $this->admin->expects($this->any())
112
            ->method('getModelManager')
113
            ->will($this->returnValue($modelManager));
114
115
        $labelTranslatorStrategy = new NoopLabelTranslatorStrategy();
116
117
        $this->admin->expects($this->any())
118
            ->method('getLabelTranslatorStrategy')
119
            ->will($this->returnValue($labelTranslatorStrategy));
120
121
        $this->admin->expects($this->any())
122
            ->method('hasShowFieldDescription')
123
            ->will($this->returnCallback(function ($name) {
124
                if (isset($this->listShowFields[$name])) {
125
                    return true;
126
                }
127
                $this->listShowFields[$name] = true;
128
129
                return false;
130
            }));
131
132
        $this->showBuilder->expects($this->any())
133
            ->method('addField')
134
            ->will($this->returnCallback(static function ($list, $type, $fieldDescription, $admin): void {
135
                $list->add($fieldDescription);
136
            }));
137
138
        $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
139
    }
140
141
    public function testFluidInterface(): void
142
    {
143
        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
144
145
        $this->assertSame($this->showMapper, $this->showMapper->add($fieldDescription));
146
        $this->assertSame($this->showMapper, $this->showMapper->remove('fooName'));
147
        $this->assertSame($this->showMapper, $this->showMapper->reorder([]));
148
    }
149
150
    public function testGet(): void
151
    {
152
        $this->assertFalse($this->showMapper->has('fooName'));
153
154
        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
155
156
        $this->showMapper->add($fieldDescription);
157
        $this->assertSame($fieldDescription, $this->showMapper->get('fooName'));
158
    }
159
160
    public function testAdd(): void
161
    {
162
        $this->showMapper->add('fooName');
163
164
        $this->assertTrue($this->showMapper->has('fooName'));
165
166
        $fieldDescription = $this->showMapper->get('fooName');
167
168
        $this->assertInstanceOf(FieldDescriptionInterface::class, $fieldDescription);
169
        $this->assertSame('fooName', $fieldDescription->getName());
170
        $this->assertSame('fooName', $fieldDescription->getOption('label'));
171
    }
172
173
    public function testIfTrueApply(): void
174
    {
175
        $this->showMapper->ifTrue(true);
176
        $this->showMapper->add('fooName');
177
        $this->showMapper->ifEnd();
178
179
        $this->assertTrue($this->showMapper->has('fooName'));
180
        $fieldDescription = $this->showMapper->get('fooName');
181
182
        $this->assertInstanceOf(FieldDescriptionInterface::class, $fieldDescription);
183
        $this->assertSame('fooName', $fieldDescription->getName());
184
        $this->assertSame('fooName', $fieldDescription->getOption('label'));
185
    }
186
187
    public function testIfTrueNotApply(): void
188
    {
189
        $this->showMapper->ifTrue(false);
190
        $this->showMapper->add('fooName');
191
        $this->showMapper->ifEnd();
192
193
        $this->assertFalse($this->showMapper->has('fooName'));
194
    }
195
196
    public function testIfTrueCombination(): void
197
    {
198
        $this->showMapper->ifTrue(false);
199
        $this->showMapper->add('fooName');
200
        $this->showMapper->ifEnd();
201
        $this->showMapper->add('barName');
202
203
        $this->assertFalse($this->showMapper->has('fooName'));
204
        $this->assertTrue($this->showMapper->has('barName'));
205
        $fieldDescription = $this->showMapper->get('barName');
206
207
        $this->assertInstanceOf(FieldDescriptionInterface::class, $fieldDescription);
208
        $this->assertSame('barName', $fieldDescription->getName());
209
        $this->assertSame('barName', $fieldDescription->getOption('label'));
210
    }
211
212
    public function testIfFalseApply(): void
213
    {
214
        $this->showMapper->ifFalse(false);
215
        $this->showMapper->add('fooName');
216
        $this->showMapper->ifEnd();
217
218
        $this->assertTrue($this->showMapper->has('fooName'));
219
        $fieldDescription = $this->showMapper->get('fooName');
220
221
        $this->assertInstanceOf(FieldDescriptionInterface::class, $fieldDescription);
222
        $this->assertSame('fooName', $fieldDescription->getName());
223
        $this->assertSame('fooName', $fieldDescription->getOption('label'));
224
    }
225
226
    public function testIfFalseNotApply(): void
227
    {
228
        $this->showMapper->ifFalse(true);
229
        $this->showMapper->add('fooName');
230
        $this->showMapper->ifEnd();
231
232
        $this->assertFalse($this->showMapper->has('fooName'));
233
    }
234
235
    public function testIfFalseCombination(): void
236
    {
237
        $this->showMapper->ifFalse(true);
238
        $this->showMapper->add('fooName');
239
        $this->showMapper->ifEnd();
240
        $this->showMapper->add('barName');
241
242
        $this->assertFalse($this->showMapper->has('fooName'));
243
        $this->assertTrue($this->showMapper->has('barName'));
244
        $fieldDescription = $this->showMapper->get('barName');
245
246
        $this->assertInstanceOf(FieldDescriptionInterface::class, $fieldDescription);
247
        $this->assertSame('barName', $fieldDescription->getName());
248
        $this->assertSame('barName', $fieldDescription->getOption('label'));
249
    }
250
251
    public function testIfTrueNested(): void
252
    {
253
        $this->expectException(\RuntimeException::class);
254
        $this->expectExceptionMessage('Cannot nest ifTrue or ifFalse call');
255
256
        $this->showMapper->ifTrue(true);
257
        $this->showMapper->ifTrue(true);
258
    }
259
260
    public function testIfFalseNested(): void
261
    {
262
        $this->expectException(\RuntimeException::class);
263
        $this->expectExceptionMessage('Cannot nest ifTrue or ifFalse call');
264
265
        $this->showMapper->ifFalse(false);
266
        $this->showMapper->ifFalse(false);
267
    }
268
269
    public function testIfCombinationNested(): void
270
    {
271
        $this->expectException(\RuntimeException::class);
272
        $this->expectExceptionMessage('Cannot nest ifTrue or ifFalse call');
273
274
        $this->showMapper->ifTrue(true);
275
        $this->showMapper->ifFalse(false);
276
    }
277
278
    public function testIfFalseCombinationNested2(): void
279
    {
280
        $this->expectException(\RuntimeException::class);
281
        $this->expectExceptionMessage('Cannot nest ifTrue or ifFalse call');
282
283
        $this->showMapper->ifFalse(false);
284
        $this->showMapper->ifTrue(true);
285
    }
286
287
    public function testIfFalseCombinationNested3(): void
288
    {
289
        $this->expectException(\RuntimeException::class);
290
        $this->expectExceptionMessage('Cannot nest ifTrue or ifFalse call');
291
292
        $this->showMapper->ifFalse(true);
293
        $this->showMapper->ifTrue(false);
294
    }
295
296
    public function testIfFalseCombinationNested4(): void
297
    {
298
        $this->expectException(\RuntimeException::class);
299
        $this->expectExceptionMessage('Cannot nest ifTrue or ifFalse call');
300
301
        $this->showMapper->ifTrue(false);
302
        $this->showMapper->ifFalse(true);
303
    }
304
305
    public function testAddRemove(): void
306
    {
307
        $this->assertFalse($this->showMapper->has('fooName'));
308
309
        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
310
311
        $this->showMapper->add($fieldDescription);
312
        $this->assertTrue($this->showMapper->has('fooName'));
313
314
        $this->showMapper->remove('fooName');
315
        $this->assertFalse($this->showMapper->has('fooName'));
316
    }
317
318
    public function testAddException(): void
319
    {
320
        $this->expectException(\RuntimeException::class, 'invalid state');
321
322
        $this->showMapper->add(12345);
323
    }
324
325
    public function testAddDuplicateFieldNameException(): void
326
    {
327
        $name = 'name';
328
        $this->expectException(\RuntimeException::class, sprintf('Duplicate field %s "name" in show mapper. Names should be unique.', $name));
329
330
        $this->showMapper->add($name);
331
        $this->showMapper->add($name);
332
    }
333
334
    public function testKeys(): void
335
    {
336
        $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
337
        $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
338
339
        $this->showMapper->add($fieldDescription1);
340
        $this->showMapper->add($fieldDescription2);
341
342
        $this->assertSame(['fooName1', 'fooName2'], $this->showMapper->keys());
343
    }
344
345
    public function testReorder(): void
346
    {
347
        $this->assertSame([], $this->admin->getShowGroups());
348
349
        $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
350
        $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
351
        $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
352
        $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
353
354
        $this->showMapper->with('Group1');
355
        $this->showMapper->add($fieldDescription1);
356
        $this->showMapper->add($fieldDescription2);
357
        $this->showMapper->add($fieldDescription3);
358
        $this->showMapper->add($fieldDescription4);
359
360
        $this->assertSame([
361
            'Group1' => [
362
                'collapsed' => false,
363
                'class' => false,
364
                'description' => false,
365
                'label' => 'Group1',
366
                'translation_domain' => null,
367
                'name' => 'Group1',
368
                'box_class' => 'box box-primary',
369
                'fields' => ['fooName1' => 'fooName1', 'fooName2' => 'fooName2', 'fooName3' => 'fooName3', 'fooName4' => 'fooName4'],
370
            ], ], $this->admin->getShowGroups());
371
372
        $this->showMapper->reorder(['fooName3', 'fooName2', 'fooName1', 'fooName4']);
373
374
        // print_r is used to compare order of items in associative arrays
375
        $this->assertSame(print_r([
376
            'Group1' => [
377
                'collapsed' => false,
378
                'class' => false,
379
                'description' => false,
380
                'label' => 'Group1',
381
                'translation_domain' => null,
382
                'name' => 'Group1',
383
                'box_class' => 'box box-primary',
384
                'fields' => ['fooName3' => 'fooName3', 'fooName2' => 'fooName2', 'fooName1' => 'fooName1', 'fooName4' => 'fooName4'],
385
            ], ], true), print_r($this->admin->getShowGroups(), true));
386
    }
387
388
    public function testGroupRemovingWithoutTab(): void
389
    {
390
        $this->cleanShowMapper();
391
392
        $this->showMapper->with('groupfoo1');
393
        $this->showMapper->removeGroup('groupfoo1');
394
395
        $this->assertSame([], $this->admin->getShowGroups());
396
    }
397
398
    public function testGroupRemovingWithTab(): void
399
    {
400
        $this->cleanShowMapper();
401
402
        $this->showMapper->tab('mytab')->with('groupfoo2');
403
        $this->showMapper->removeGroup('groupfoo2', 'mytab');
404
405
        $this->assertSame([], $this->admin->getShowGroups());
406
    }
407
408
    public function testGroupRemovingWithoutTabAndWithTabRemoving(): void
409
    {
410
        $this->cleanShowMapper();
411
412
        $this->showMapper->with('groupfoo3');
413
        $this->showMapper->removeGroup('groupfoo3', 'default', true);
414
415
        $this->assertSame([], $this->admin->getShowGroups());
416
        $this->assertSame([], $this->admin->getShowTabs());
417
    }
418
419
    public function testGroupRemovingWithTabAndWithTabRemoving(): void
420
    {
421
        $this->cleanShowMapper();
422
423
        $this->showMapper->tab('mytab2')->with('groupfoo4');
424
        $this->showMapper->removeGroup('groupfoo4', 'mytab2', true);
425
426
        $this->assertSame([], $this->admin->getShowGroups());
427
        $this->assertSame([], $this->admin->getShowTabs());
428
    }
429
430
    public function testEmptyFieldLabel(): void
431
    {
432
        $this->showMapper->add('foo', null, ['label' => false]);
433
434
        $this->assertFalse($this->showMapper->get('foo')->getOption('label'));
435
    }
436
437
    private function cleanShowMapper(): void
438
    {
439
        $this->showBuilder = $this->getMockForAbstractClass(ShowBuilderInterface::class);
440
        $this->fieldDescriptionCollection = new FieldDescriptionCollection();
441
        $this->admin = new CleanAdmin('code', 'class', 'controller');
442
        $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
443
    }
444
445
    private function getFieldDescriptionMock(?string $name = null, ?string $label = null): BaseFieldDescription
446
    {
447
        $fieldDescription = $this->getMockForAbstractClass(BaseFieldDescription::class);
448
449
        if (null !== $name) {
450
            $fieldDescription->setName($name);
451
        }
452
453
        if (null !== $label) {
454
            $fieldDescription->setOption('label', $label);
455
        }
456
457
        return $fieldDescription;
458
    }
459
}
460