Completed
Push — 3.x ( a5a386...9dc6d5 )
by Christian
02:59
created

BaseGroupedMapperTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 2
dl 0
loc 232
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 58 1
A testWith() 0 8 1
A testEnd() 0 4 1
A testTab() 0 8 1
A testTab2() 0 8 1
A testFluidInterface() 0 4 1
A testGroupNotClosedException() 0 8 1
A testGroupInTabException() 0 8 1
A testTabInTabException() 0 8 1
A testHasOpenTab() 0 10 1
A testIfTrueApply() 0 5 1
A testIfTrueNotApply() 0 5 1
A testIfFalseApply() 0 5 1
A testIfFalseNotApply() 0 5 1
A testEndException() 0 7 1
A labelDataProvider() 0 9 1
A testLabel() 0 22 3
A getTabs() 0 4 1
A setTabs() 0 4 1
A getTestGroups() 0 4 1
A setTestGroups() 0 4 1
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\Mapper;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AbstractAdmin;
18
use Sonata\AdminBundle\Admin\Pool;
19
use Sonata\AdminBundle\Builder\BuilderInterface;
20
use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
21
use Sonata\AdminBundle\Tests\Fixtures\Admin\AbstractDummyGroupedMapper;
22
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
/**
26
 * Test for BaseGroupedMapper.
27
 *
28
 * @author Andrej Hudec <[email protected]>
29
 */
30
class BaseGroupedMapperTest extends TestCase
31
{
32
    /**
33
     * @var BaseGroupedMapper
34
     */
35
    protected $baseGroupedMapper;
36
37
    private $tabs;
38
    private $groups;
39
40
    public function setUp(): void
41
    {
42
        $admin = $this->getMockBuilder(AbstractAdmin::class)
43
            ->disableOriginalConstructor()
44
            ->getMock();
45
46
        $labelStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
47
        $labelStrategy
48
            ->method('getLabel')
49
            ->willReturnCallback(static function (string $label): string {
50
                return 'label_'.strtolower($label);
51
            });
52
53
        $admin
54
            ->method('getLabelTranslatorStrategy')
55
            ->willReturn($labelStrategy);
56
57
        $container = $this->getMockForAbstractClass(ContainerInterface::class);
58
        $configurationPool = new Pool($container, 'myTitle', 'myLogoTitle');
59
60
        $admin
61
            ->method('getConfigurationPool')
62
            ->willReturn($configurationPool);
63
64
        $builder = $this->getMockForAbstractClass(BuilderInterface::class);
65
66
        $this->baseGroupedMapper = $this->getMockForAbstractClass(
67
            AbstractDummyGroupedMapper::class,
68
            [$builder, $admin]
69
        );
70
71
        $this->tabs = [];
72
        $this->groups = [];
73
74
        $this->baseGroupedMapper
75
            ->method('getTabs')
76
            ->willReturnCallback(function () {
77
                return $this->getTabs();
78
            });
79
80
        $this->baseGroupedMapper
81
            ->method('setTabs')
82
            ->willReturnCallback(function (array $tabs): void {
83
                $this->setTabs($tabs);
84
            });
85
86
        $this->baseGroupedMapper
87
            ->method('getGroups')
88
            ->willReturnCallback(function () {
89
                return $this->getTestGroups();
90
            });
91
92
        $this->baseGroupedMapper
93
            ->method('setGroups')
94
            ->willReturnCallback(function (array $groups): void {
95
                $this->setTestGroups($groups);
96
            });
97
    }
98
99
    public function testWith(): void
100
    {
101
        $this->assertCount(0, $this->tabs);
102
        $this->assertCount(0, $this->groups);
103
        $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooGroup'));
104
        $this->assertCount(1, $this->tabs);
105
        $this->assertCount(1, $this->groups);
106
    }
107
108
    public function testEnd(): void
109
    {
110
        $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooGroup'));
111
    }
112
113
    public function testTab(): void
114
    {
115
        $this->assertCount(0, $this->tabs);
116
        $this->assertCount(0, $this->groups);
117
        $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->tab('fooTab'));
118
        $this->assertCount(1, $this->tabs);
119
        $this->assertCount(0, $this->groups);
120
    }
121
122
    public function testTab2(): void
123
    {
124
        $this->assertCount(0, $this->tabs);
125
        $this->assertCount(0, $this->groups);
126
        $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooTab', ['tab' => true]));
127
        $this->assertCount(1, $this->tabs);
128
        $this->assertCount(0, $this->groups);
129
    }
130
131
    public function testFluidInterface(): void
132
    {
133
        $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->tab('fooTab')->with('fooGroup1')->end()->with('fooGroup2')->end()->with('fooGroup3')->end()->end()->tab('barTab')->with('barGroup1')->end()->with('barGroup2')->end()->with('barGroup3')->end()->end());
134
    }
135
136
    public function testGroupNotClosedException(): void
137
    {
138
        $this->expectException(\LogicException::class);
139
        $this->expectExceptionMessage('You should close previous group "fooGroup1" with end() before adding new tab "fooGroup2".');
140
141
        $this->baseGroupedMapper->with('fooGroup1');
142
        $this->baseGroupedMapper->with('fooGroup2');
143
    }
144
145
    public function testGroupInTabException(): void
146
    {
147
        $this->expectException(\LogicException::class);
148
        $this->expectExceptionMessage('New tab was added automatically when you have added field or group. You should close current tab before adding new one OR add tabs before adding groups and fields.');
149
150
        $this->baseGroupedMapper->with('fooGroup');
151
        $this->baseGroupedMapper->tab('fooTab');
152
    }
153
154
    public function testTabInTabException(): void
155
    {
156
        $this->expectException(\LogicException::class);
157
        $this->expectExceptionMessage('You should close previous tab "fooTab" with end() before adding new tab "barTab".');
158
159
        $this->baseGroupedMapper->tab('fooTab');
160
        $this->baseGroupedMapper->tab('barTab');
161
    }
162
163
    public function testHasOpenTab(): void
164
    {
165
        $this->assertFalse($this->baseGroupedMapper->hasOpenTab(), '->hasOpenTab() returns false when there are no tabs');
166
167
        $this->baseGroupedMapper->tab('fooTab');
168
        $this->assertTrue($this->baseGroupedMapper->hasOpenTab(), '->hasOpenTab() returns true when there is an open tab');
169
170
        $this->baseGroupedMapper->end();
171
        $this->assertFalse($this->baseGroupedMapper->hasOpenTab(), '->hasOpenTab() returns false when all tabs are closed');
172
    }
173
174
    public function testIfTrueApply(): void
175
    {
176
        $this->baseGroupedMapper->ifTrue(true)->tab('fooTab')->ifEnd();
177
        $this->assertTrue($this->baseGroupedMapper->hasOpenTab());
178
    }
179
180
    public function testIfTrueNotApply(): void
181
    {
182
        $this->baseGroupedMapper->ifTrue(false)->tab('fooTab')->ifEnd();
183
        $this->assertFalse($this->baseGroupedMapper->hasOpenTab());
184
    }
185
186
    public function testIfFalseApply(): void
187
    {
188
        $this->baseGroupedMapper->ifFalse(false)->tab('fooTab')->ifEnd();
189
        $this->assertTrue($this->baseGroupedMapper->hasOpenTab());
190
    }
191
192
    public function testIfFalseNotApply(): void
193
    {
194
        $this->baseGroupedMapper->ifFalse(true)->tab('fooTab')->ifEnd();
195
        $this->assertFalse($this->baseGroupedMapper->hasOpenTab());
196
    }
197
198
    public function testEndException(): void
199
    {
200
        $this->expectException(\LogicException::class);
201
        $this->expectExceptionMessage('No open tabs or groups, you cannot use end()');
202
203
        $this->baseGroupedMapper->end();
204
    }
205
206
    public function labelDataProvider()
207
    {
208
        return [
209
            'nominal use case not translated' => [false, 'fooGroup1', null, 'fooGroup1'],
210
            'nominal use case translated' => [true, 'fooGroup1', null, 'label_foogroup1'],
211
            'custom label not translated' => [false, 'fooGroup1', 'custom_label', 'custom_label'],
212
            'custom label translated' => [true, 'fooGroup1', 'custom_label', 'custom_label'],
213
        ];
214
    }
215
216
    /**
217
     * @dataProvider labelDataProvider
218
     */
219
    public function testLabel(string $translated, string $name, ?string $label, string $expectedLabel): void
220
    {
221
        $container = $this->baseGroupedMapper
222
            ->getAdmin()
223
            ->getConfigurationPool()
224
            ->getContainer();
225
226
        $container
227
            ->method('getParameter')
228
            ->willReturn($translated);
229
230
        $options = [];
231
232
        if (null !== $label) {
233
            $options['label'] = $label;
234
        }
235
236
        $this->baseGroupedMapper->with($name, $options);
237
238
        $this->assertSame($translated ? 'label_default' : 'default', $this->tabs['default']['label']);
239
        $this->assertSame($expectedLabel, $this->groups[$name]['label']);
240
    }
241
242
    public function getTabs(): array
243
    {
244
        return $this->tabs;
245
    }
246
247
    public function setTabs(array $tabs): void
248
    {
249
        $this->tabs = $tabs;
250
    }
251
252
    public function getTestGroups(): array
253
    {
254
        return $this->groups;
255
    }
256
257
    public function setTestGroups(array $groups): void
258
    {
259
        $this->groups = $groups;
260
    }
261
}
262