Completed
Push — 3.x ( dff789...a13f27 )
by Grégoire
02:46
created

BaseGroupedMapperTest::testIfEndException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 testIfEndException(): void
207
    {
208
        $this->expectException(\LogicException::class);
209
        $this->expectExceptionMessage('No open ifTrue() or ifFalse(), you cannot use ifEnd()');
210
211
        $this->baseGroupedMapper->ifEnd();
212
    }
213
214
    public function labelDataProvider(): array
215
    {
216
        return [
217
            'nominal use case not translated' => [false, 'fooGroup1', null, 'fooGroup1'],
218
            'nominal use case translated' => [true, 'fooGroup1', null, 'label_foogroup1'],
219
            'custom label not translated' => [false, 'fooGroup1', 'custom_label', 'custom_label'],
220
            'custom label translated' => [true, 'fooGroup1', 'custom_label', 'custom_label'],
221
        ];
222
    }
223
224
    /**
225
     * @dataProvider labelDataProvider
226
     */
227
    public function testLabel(bool $translated, string $name, ?string $label, string $expectedLabel): void
228
    {
229
        $container = $this->baseGroupedMapper
230
            ->getAdmin()
231
            ->getConfigurationPool()
232
            ->getContainer();
233
234
        $container
235
            ->method('getParameter')
236
            ->willReturn($translated);
237
238
        $options = [];
239
240
        if (null !== $label) {
241
            $options['label'] = $label;
242
        }
243
244
        $this->baseGroupedMapper->with($name, $options);
245
246
        $this->assertSame($translated ? 'label_default' : 'default', $this->tabs['default']['label']);
247
        $this->assertSame($expectedLabel, $this->groups[$name]['label']);
248
    }
249
250
    public function getTabs(): array
251
    {
252
        return $this->tabs;
253
    }
254
255
    public function setTabs(array $tabs): void
256
    {
257
        $this->tabs = $tabs;
258
    }
259
260
    public function getTestGroups(): array
261
    {
262
        return $this->groups;
263
    }
264
265
    public function setTestGroups(array $groups): void
266
    {
267
        $this->groups = $groups;
268
    }
269
}
270