‪TYPO3CMS  ‪main
PermutationUtilityTest.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;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪PermutationUtilityTest extends UnitTestCase
27 {
28  public static function ‪meltStringItemsDataProvider(): array
29  {
30  return [
31  'string items' => [
32  [
33  ['a', 'b'],
34  ['c', 'd'],
35  ['e', 'f'],
36  ],
37  ['ace', 'acf', 'ade', 'adf', 'bce', 'bcf', 'bde', 'bdf'],
38  ],
39  'string & empty value items' => [
40  [
41  ['a', ''],
42  ['b', ''],
43  ['c', ''],
44  ],
45  ['abc', 'ab', 'ac', 'a', 'bc', 'b', 'c', ''],
46  ],
47  'object::__toString() items' => [
48  [
49  [new ‪StringValue('a'), new ‪StringValue('b')],
50  [new ‪StringValue('c'), new ‪StringValue('d')],
51  [new ‪StringValue('e'), new ‪StringValue('f')],
52  ],
53  ['ace', 'acf', 'ade', 'adf', 'bce', 'bcf', 'bde', 'bdf'],
54  ],
55  'string & object::__toString() items' => [
56  [
57  ['a', new ‪StringValue('b')],
58  ['c', new ‪StringValue('d')],
59  ['e', new ‪StringValue('f')],
60  ],
61  ['ace', 'acf', 'ade', 'adf', 'bce', 'bcf', 'bde', 'bdf'],
62  ],
63  'string items with invalid object' => [
64  [
65  ['a', 'b'],
66  ['c', 'd'],
67  ['e', 'f', new \stdClass()],
68  ],
69  1578164102,
70  ],
71  'string items with invalid integer' => [
72  [
73  ['a', 'b'],
74  ['c', 'd'],
75  ['e', 'f', 123],
76  ],
77  1578164102,
78  ],
79  'string items with invalid boolean' => [
80  [
81  ['a', 'b'],
82  ['c', 'd'],
83  ['e', 'f', true],
84  ],
85  1578164102,
86  ],
87  'string items with invalid array' => [
88  [
89  ['a', 'b'],
90  ['c', 'd'],
91  ['e', 'f', []],
92  ],
93  1578164102,
94  ],
95  'string items with invalid invocation' => [
96  [
97  'a', 'b',
98  ],
99  1578164101,
100  ],
101  ];
102  }
103 
107  #[DataProvider('meltStringItemsDataProvider')]
108  #[Test]
109  public function ‪meltStringItemsIsExecuted(array $payload, $expectation): void
110  {
111  if (is_int($expectation)) {
112  $this->expectException(\LogicException::class);
113  $this->expectExceptionCode($expectation);
114  }
115  self::assertSame($expectation, ‪PermutationUtility::meltStringItems($payload));
116  }
117 
118  public static function ‪meltArrayItemsDataProvider(): array
119  {
120  $aStringValue = new ‪StringValue('a');
121  $bStringValue = new ‪StringValue('b');
122  $cStringValue = new ‪StringValue('c');
123  $dStringValue = new ‪StringValue('d');
124 
125  return [
126  'string items' => [
127  [
128  ['a', 'b'],
129  ['c', 'd'],
130  ['e', 'f'],
131  ],
132  [
133  ['a', 'c', 'e'], ['a', 'c', 'f'], ['a', 'd', 'e'], ['a', 'd', 'f'],
134  ['b', 'c', 'e'], ['b', 'c', 'f'], ['b', 'd', 'e'], ['b', 'd', 'f'],
135  ],
136  ],
137  'object::__toString() items' => [
138  [
139  [$aStringValue, $bStringValue],
140  [$cStringValue, $dStringValue],
141  ],
142  [
143  [$aStringValue, $cStringValue], [$aStringValue, $dStringValue],
144  [$bStringValue, $cStringValue], [$bStringValue, $dStringValue],
145  ],
146  ],
147  'mixed items' => [
148  [
149  [$aStringValue, 'a', 1],
150  [$bStringValue, 'b', 2],
151  ],
152  [
153  [$aStringValue, $bStringValue], [$aStringValue, 'b'], [$aStringValue, 2],
154  ['a', $bStringValue], ['a', 'b'], ['a', 2],
155  [1, $bStringValue], [1, 'b'], [1, 2],
156  ],
157  ],
158  'string items in ArrayObject' => [
159  [
160  new \ArrayObject(['a', 'b']),
161  new \ArrayObject(['c', 'd']),
162  new \ArrayObject(['e', 'f']),
163  ],
164  [
165  ['a', 'c', 'e'], ['a', 'c', 'f'], ['a', 'd', 'e'], ['a', 'd', 'f'],
166  ['b', 'c', 'e'], ['b', 'c', 'f'], ['b', 'd', 'e'], ['b', 'd', 'f'],
167  ],
168  ],
169  'string items with invalid invocation' => [
170  [
171  'a', 'b',
172  ],
173  1578164101,
174  ],
175  'object::__toString() items with invalid invocation' => [
176  [
177  new ‪StringValue('b'),
178  new ‪StringValue('c'),
179  new ‪StringValue('d'),
180  ],
181  1578164101,
182  ],
183  ];
184  }
185 
189  #[DataProvider('meltArrayItemsDataProvider')]
190  #[Test]
191  public function ‪meltArrayItemsIsExecuted(array $payload, $expectation): void
192  {
193  if (is_int($expectation)) {
194  $this->expectException(\LogicException::class);
195  $this->expectExceptionCode($expectation);
196  }
197  self::assertSame($expectation, ‪PermutationUtility::meltArrayItems($payload));
198  }
199 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\PermutationUtilityTest\meltStringItemsDataProvider
‪static meltStringItemsDataProvider()
Definition: PermutationUtilityTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Utility\PermutationUtilityTest\meltArrayItemsDataProvider
‪static meltArrayItemsDataProvider()
Definition: PermutationUtilityTest.php:118
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\StringValue
Definition: StringValue.php:21
‪TYPO3\CMS\Core\Utility\PermutationUtility
Definition: PermutationUtility.php:24
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltArrayItems
‪static array[] meltArrayItems(array $payload, array $previousResult=[])
Definition: PermutationUtility.php:72
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltStringItems
‪static string[] meltStringItems(array $payload, string $previousResult='')
Definition: PermutationUtility.php:36
‪TYPO3\CMS\Core\Tests\Unit\Utility\PermutationUtilityTest\meltArrayItemsIsExecuted
‪meltArrayItemsIsExecuted(array $payload, $expectation)
Definition: PermutationUtilityTest.php:191
‪TYPO3\CMS\Core\Tests\Unit\Utility\PermutationUtilityTest\meltStringItemsIsExecuted
‪meltStringItemsIsExecuted(array $payload, $expectation)
Definition: PermutationUtilityTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Utility\PermutationUtilityTest
Definition: PermutationUtilityTest.php:27