‪TYPO3CMS  ‪main
SelectItemTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 final class ‪SelectItemTest extends UnitTestCase
26 {
28  {
29  yield 'only required fields' => [
30  ['label' => 'foo', 'value' => 'bar'],
31  'select',
32  ['label' => 'foo', 'value' => 'bar', 'icon' => null, 'group' => null, 'description' => null],
33  ];
34 
35  yield 'all fields' => [
36  ['label' => 'foo', 'value' => 'bar', 'icon' => 'myIcon', 'group' => 'myGroup', 'description' => 'myDescription'],
37  'select',
38  ['label' => 'foo', 'value' => 'bar', 'icon' => 'myIcon', 'group' => 'myGroup', 'description' => 'myDescription'],
39  ];
40 
41  yield 'integer as value' => [
42  ['label' => 'foo', 'value' => 1, 'icon' => 'myIcon', 'group' => 'myGroup', 'description' => 'myDescription'],
43  'select',
44  ['label' => 'foo', 'value' => 1, 'icon' => 'myIcon', 'group' => 'myGroup', 'description' => 'myDescription'],
45  ];
46 
47  yield 'checkbox fields' => [
48  ['label' => 'foo', 'invertStateDisplay' => 1, 'iconIdentifierChecked' => 'foo1', 'iconIdentifierUnchecked' => 'foo2', 'labelChecked' => 'foo3', 'labelUnchecked' => 'foo4'],
49  'check',
50  ['label' => 'foo', 'invertStateDisplay' => true, 'iconIdentifierChecked' => 'foo1', 'iconIdentifierUnchecked' => 'foo2', 'labelChecked' => 'foo3', 'labelUnchecked' => 'foo4'],
51  ];
52 
53  yield 'radio fields' => [
54  ['label' => 'foo', 'value' => 'bar'],
55  'radio',
56  ['label' => 'foo', 'value' => 'bar'],
57  ];
58 
59  yield 'legacy indexed array keys' => [
60  ['foo', 'bar', 'myIcon', 'myGroup', 'myDescription'],
61  'select',
62  ['label' => 'foo', 'value' => 'bar', 'icon' => 'myIcon', 'group' => 'myGroup', 'description' => 'myDescription'],
63  ];
64  }
65 
66  #[DataProvider('selectionItemCanBeConstructedFromTcaItemDataProvider')]
67  #[Test]
68  public function ‪selectionItemCanBeConstructedFromTcaItem(array $item, string $type, array $expected): void
69  {
70  $selectionItem = ‪SelectItem::fromTcaItemArray($item, $type);
71 
72  self::assertSame($expected, $selectionItem->toArray());
73  }
74 
75  #[Test]
76  public function ‪dividerValueCanBeIdentified(): void
77  {
78  $item = ['label' => 'foo', 'value' => '--div--'];
79  $selectionItem = ‪SelectItem::fromTcaItemArray($item);
80 
81  self::assertTrue($selectionItem->isDivider());
82  }
83 
84  public static function ‪notSetValuesCanBeIdentifiedDataProvider(): iterable
85  {
86  yield 'only required fields' => [
87  ['label' => 'foo', 'value' => 'bar'],
88  ['icon' => false, 'group' => false, 'description' => false],
89  ];
90 
91  yield 'all fields' => [
92  ['label' => 'foo', 'value' => 'bar', 'icon' => 'myIcon', 'group' => 'myGroup', 'description' => 'myDescription'],
93  ['icon' => true, 'group' => true, 'description' => true],
94  ];
95  }
96 
97  #[DataProvider('notSetValuesCanBeIdentifiedDataProvider')]
98  #[Test]
99  public function ‪notSetValuesCanBeIdentified(array $item, array $expected): void
100  {
101  $selectionItem = ‪SelectItem::fromTcaItemArray($item);
102 
103  self::assertSame($expected['icon'], $selectionItem->hasIcon());
104  self::assertSame($expected['group'], $selectionItem->hasGroup());
105  self::assertSame($expected['description'], $selectionItem->hasDescription());
106  }
107 
108  #[Test]
109  public function ‪canBeAccessedAsAnArray(): void
110  {
111  $selectionItem = new ‪SelectItem(
112  type: 'select',
113  label: 'foo',
114  value: 'bar',
115  icon: 'myIcon',
116  group: 'myGroup',
117  ‪description: 'my description',
118  );
119 
120  self::assertSame('foo', $selectionItem['label']);
121  self::assertSame('bar', $selectionItem['value']);
122  self::assertSame('myIcon', $selectionItem['icon']);
123  self::assertSame('myGroup', $selectionItem['group']);
124  self::assertSame('my description', $selectionItem['description']);
125  }
126 
127  #[Test]
129  {
130  $selectionItem = new ‪SelectItem(
131  type: 'select',
132  label: 'foo',
133  value: 'bar',
134  icon: 'myIcon',
135  group: 'myGroup',
136  ‪description: 'my description',
137  );
138 
139  self::assertSame('foo', $selectionItem[0]);
140  self::assertSame('bar', $selectionItem[1]);
141  self::assertSame('myIcon', $selectionItem[2]);
142  self::assertSame('myGroup', $selectionItem[3]);
143  self::assertSame('my description', $selectionItem[4]);
144  }
145 
146  #[Test]
147  public function ‪canBeManipulatedLikeAnArray(): void
148  {
149  $selectionItem = new ‪SelectItem(
150  type: 'select',
151  label: 'foo',
152  value: 'bar',
153  );
154 
155  $selectionItem['label'] = 'oof';
156  $selectionItem['value'] = 'rab';
157  $selectionItem['icon'] = 'myIcon';
158  $selectionItem['group'] = 'myGroup';
159  $selectionItem['description'] = 'my description';
160 
161  self::assertSame('oof', $selectionItem->getLabel());
162  self::assertSame('rab', $selectionItem->getValue());
163  self::assertSame('myIcon', $selectionItem->getIcon());
164  self::assertSame('myGroup', $selectionItem->getGroup());
165  self::assertSame('my description', $selectionItem->getDescription());
166  }
167 
168  #[Test]
170  {
171  $selectionItem = new ‪SelectItem(
172  type: 'select',
173  label: 'foo',
174  value: 'bar',
175  );
176 
177  $selectionItem[0] = 'oof';
178  $selectionItem[1] = 'rab';
179  $selectionItem[2] = 'myIcon';
180  $selectionItem[3] = 'myGroup';
181  $selectionItem[4] = 'my description';
182 
183  self::assertSame('oof', $selectionItem->getLabel());
184  self::assertSame('rab', $selectionItem->getValue());
185  self::assertSame('myIcon', $selectionItem->getIcon());
186  self::assertSame('myGroup', $selectionItem->getGroup());
187  self::assertSame('my description', $selectionItem->getDescription());
188  }
189 
190  #[Test]
191  public function ‪valuesCanBeUnsetWithUnsetFunction(): void
192  {
193  $selectionItem = new ‪SelectItem(
194  type: 'select',
195  label: 'foo',
196  value: 'bar',
197  icon: 'myIcon',
198  group: 'myGroup',
199  ‪description: 'my description',
200  );
201 
202  unset($selectionItem['icon']);
203  unset($selectionItem['group']);
204  unset($selectionItem['description']);
205 
206  self::assertNull($selectionItem->getIcon());
207  self::assertNull($selectionItem->getGroup());
208  self::assertNull($selectionItem->getDescription());
209  }
210 
211  #[Test]
213  {
214  $selectionItem = new ‪SelectItem(
215  type: 'select',
216  label: 'foo',
217  value: 'bar',
218  icon: 'myIcon',
219  group: 'myGroup',
220  ‪description: 'my description',
221  );
222 
223  unset($selectionItem[2]);
224  unset($selectionItem[3]);
225  unset($selectionItem[4]);
226 
227  self::assertNull($selectionItem->getIcon());
228  self::assertNull($selectionItem->getGroup());
229  self::assertNull($selectionItem->getDescription());
230  }
231 
232  #[Test]
234  {
235  $selectionItem = new ‪SelectItem(
236  type: 'select',
237  label: 'foo',
238  value: 'bar',
239  );
240 
241  self::assertTrue(isset($selectionItem['label']));
242  self::assertTrue(isset($selectionItem['value']));
243  self::assertFalse(isset($selectionItem['icon']));
244  self::assertFalse(isset($selectionItem['group']));
245  self::assertFalse(isset($selectionItem['description']));
246  }
247 
248  #[Test]
250  {
251  $selectionItem = new ‪SelectItem(
252  type: 'select',
253  label: 'foo',
254  value: 'bar',
255  );
256 
257  self::assertTrue(isset($selectionItem[0]));
258  self::assertTrue(isset($selectionItem[1]));
259  self::assertFalse(isset($selectionItem[2]));
260  self::assertFalse(isset($selectionItem[3]));
261  self::assertFalse(isset($selectionItem[4]));
262  }
263 
264  #[Test]
266  {
267  $selectionItem = new ‪SelectItem(
268  type: 'select',
269  label: 'foo',
270  value: 'bar',
271  );
272 
273  $selectionItem['custom'] = 'customValue';
274 
275  self::assertTrue(isset($selectionItem['custom']));
276  self::assertSame('customValue', $selectionItem['custom']);
277  }
278 
279  #[Test]
281  {
282  $selectionItem = new ‪SelectItem(
283  type: 'select',
284  label: 'foo',
285  value: 'bar',
286  );
287 
288  $selectionItem['custom'] = 'customValue';
289  unset($selectionItem['custom']);
290 
291  self::assertFalse(isset($selectionItem['custom']));
292  self::assertNull($selectionItem['custom']);
293  }
294 }
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\canBeManipulatedLikeAnArray
‪canBeManipulatedLikeAnArray()
Definition: SelectItemTest.php:147
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\selectionItemCanBeConstructedFromTcaItem
‪selectionItemCanBeConstructedFromTcaItem(array $item, string $type, array $expected)
Definition: SelectItemTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\canBeAccessedAsAnArray
‪canBeAccessedAsAnArray()
Definition: SelectItemTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\canUnsetCustomValueInArrayLikeFashion
‪canUnsetCustomValueInArrayLikeFashion()
Definition: SelectItemTest.php:280
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest
Definition: SelectItemTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\arrayOffsetsCanBeTestedWithIssetFunctionWithLegacyIndexedKeys
‪arrayOffsetsCanBeTestedWithIssetFunctionWithLegacyIndexedKeys()
Definition: SelectItemTest.php:249
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\notSetValuesCanBeIdentifiedDataProvider
‪static notSetValuesCanBeIdentifiedDataProvider()
Definition: SelectItemTest.php:84
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\canSetCustomValueInArrayLikeFashion
‪canSetCustomValueInArrayLikeFashion()
Definition: SelectItemTest.php:265
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct
Definition: SelectItemTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\valuesCanBeUnsetWithUnsetFunction
‪valuesCanBeUnsetWithUnsetFunction()
Definition: SelectItemTest.php:191
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\arrayOffsetsCanBeTestedWithIssetFunction
‪arrayOffsetsCanBeTestedWithIssetFunction()
Definition: SelectItemTest.php:233
‪TYPO3\CMS\Core\Schema\Struct\SelectItem
Definition: SelectItem.php:21
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\fromTcaItemArray
‪static fromTcaItemArray(array $item, string $type='select')
Definition: SelectItem.php:45
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\canBeManipulatedLikeAnArrayWithLegacyIndexedKeys
‪canBeManipulatedLikeAnArrayWithLegacyIndexedKeys()
Definition: SelectItemTest.php:169
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\notSetValuesCanBeIdentified
‪notSetValuesCanBeIdentified(array $item, array $expected)
Definition: SelectItemTest.php:99
‪TYPO3\CMS\Redirects\Message\description
‪identifier description
Definition: RedirectWasHitMessage.php:32
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\selectionItemCanBeConstructedFromTcaItemDataProvider
‪static selectionItemCanBeConstructedFromTcaItemDataProvider()
Definition: SelectItemTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\valuesCanBeUnsetWithUnsetFunctionWithLegacyIndexedKeys
‪valuesCanBeUnsetWithUnsetFunctionWithLegacyIndexedKeys()
Definition: SelectItemTest.php:212
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\canBeAccessedAsAnArrayWithLegacyIndexedKeys
‪canBeAccessedAsAnArrayWithLegacyIndexedKeys()
Definition: SelectItemTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Schema\Struct\SelectItemTest\dividerValueCanBeIdentified
‪dividerValueCanBeIdentified()
Definition: SelectItemTest.php:76