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\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->expects($this->any()) |
||
48 | ->method('getLabel') |
||
49 | ->willReturnCallback(static function ($label) { |
||
50 | return 'label_'.strtolower($label); |
||
51 | }); |
||
52 | |||
53 | $admin->expects($this->any()) |
||
54 | ->method('getLabelTranslatorStrategy') |
||
55 | ->willReturn($labelStrategy); |
||
56 | |||
57 | $container = $this->getMockForAbstractClass(ContainerInterface::class); |
||
58 | $configurationPool = new Pool($container, 'myTitle', 'myLogoTitle'); |
||
0 ignored issues
–
show
|
|||
59 | |||
60 | $admin->expects($this->any()) |
||
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->expects($this->any()) |
||
75 | ->method('getTabs') |
||
76 | ->willReturnCallback(function () { |
||
77 | return $this->getTabs(); |
||
78 | }); |
||
79 | |||
80 | $this->baseGroupedMapper->expects($this->any()) |
||
81 | ->method('setTabs') |
||
82 | ->willReturnCallback(function (array $tabs): void { |
||
83 | $this->setTabs($tabs); |
||
84 | }); |
||
85 | |||
86 | $this->baseGroupedMapper->expects($this->any()) |
||
87 | ->method('getGroups') |
||
88 | ->willReturnCallback(function () { |
||
89 | return $this->getTestGroups(); |
||
90 | }); |
||
91 | |||
92 | $this->baseGroupedMapper->expects($this->any()) |
||
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(\RuntimeException::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(\RuntimeException::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(\RuntimeException::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 testEndException(): void |
||
175 | { |
||
176 | $this->expectException(\RuntimeException::class); |
||
177 | $this->expectExceptionMessage('No open tabs or groups, you cannot use end()'); |
||
178 | |||
179 | $this->baseGroupedMapper->end(); |
||
180 | } |
||
181 | |||
182 | public function labelDataProvider() |
||
183 | { |
||
184 | return [ |
||
185 | 'nominal use case not translated' => [false, 'fooGroup1', null, 'fooGroup1'], |
||
186 | 'nominal use case translated' => [true, 'fooGroup1', null, 'label_foogroup1'], |
||
187 | 'custom label not translated' => [false, 'fooGroup1', 'custom_label', 'custom_label'], |
||
188 | 'custom label translated' => [true, 'fooGroup1', 'custom_label', 'custom_label'], |
||
189 | ]; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @dataProvider labelDataProvider |
||
194 | */ |
||
195 | public function testLabel($translated, $name, $label, $expectedLabel): void |
||
196 | { |
||
197 | $container = $this->baseGroupedMapper |
||
198 | ->getAdmin() |
||
199 | ->getConfigurationPool() |
||
200 | ->getContainer(); |
||
201 | |||
202 | $container->expects($this->any()) |
||
203 | ->method('getParameter') |
||
204 | ->willReturn($translated); |
||
205 | |||
206 | $options = []; |
||
207 | |||
208 | if (null !== $label) { |
||
209 | $options['label'] = $label; |
||
210 | } |
||
211 | |||
212 | $this->baseGroupedMapper->with($name, $options); |
||
213 | |||
214 | $this->assertSame($translated ? 'label_default' : 'default', $this->tabs['default']['label']); |
||
215 | $this->assertSame($expectedLabel, $this->groups[$name]['label']); |
||
216 | } |
||
217 | |||
218 | public function getTabs() |
||
219 | { |
||
220 | return $this->tabs; |
||
221 | } |
||
222 | |||
223 | public function setTabs($tabs): void |
||
224 | { |
||
225 | $this->tabs = $tabs; |
||
226 | } |
||
227 | |||
228 | public function getTestGroups() |
||
229 | { |
||
230 | return $this->groups; |
||
231 | } |
||
232 | |||
233 | public function setTestGroups($groups): void |
||
234 | { |
||
235 | $this->groups = $groups; |
||
236 | } |
||
237 | } |
||
238 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: