‪TYPO3CMS  9.5
StringUtilityTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19 
23 class ‪StringUtilityTest extends UnitTestCase
24 {
25 
32  {
33  return [
34  'match last part of string' => ['hello world', 'world'],
35  'match last char of string' => ['hellod world', 'd'],
36  'match whole string' => ['hello', 'hello'],
37  'integer is part of string with same number' => ['24', 24],
38  'string is part of integer with same number' => [24, '24'],
39  'integer is part of string ending with same number' => ['please gimme beer, 24', 24]
40  ];
41  }
42 
47  public function ‪endsWithReturnsTrueForMatchingLastPart($string, $part)
48  {
49  $this->assertTrue(‪StringUtility::endsWith($string, $part));
50  }
51 
58  {
59  return [
60  'no string match' => ['hello', 'bye'],
61  'no case sensitive string match' => ['hello world', 'World'],
62  'string is part but not last part' => ['hello world', 'worl'],
63  'integer is not part of empty string' => ['', 0],
64  'longer string is not part of shorter string' => ['a', 'aa'],
65  ];
66  }
67 
72  public function ‪endsWithReturnsFalseForNotMatchingLastPart($string, $part)
73  {
74  $this->assertFalse(‪StringUtility::endsWith($string, $part));
75  }
76 
83  {
84  return [
85  'array is not part of string' => ['string', [], 1347135545],
86  'NULL is not part of string' => ['string', null, 1347135545],
87  'empty string is not part of string' => ['string', '', 1347135545],
88  'string is not part of array' => [[], 'string', 1347135544],
89  'NULL is not part of array' => [[], null, 1347135544],
90  'string is not part of NULL' => [null, 'string', 1347135544],
91  'array is not part of NULL' => [null, [], 1347135544],
92  'integer is not part of NULL' => [null, 0, 1347135544],
93  'empty string is not part of NULL' => [null, '', 1347135544],
94  'NULL is not part of empty string' => ['', null, 1347135545],
95  'FALSE is not part of empty string' => ['', false, 1347135545],
96  'empty string is not part of FALSE' => [false, '', 1347135545],
97  'empty string is not part of integer' => [0, '', 1347135545],
98  'string is not part of object' => [new \stdClass(), 'foo', 1347135544],
99  'object is not part of string' => ['foo', new \stdClass(), 1347135545],
100  ];
101  }
102 
107  public function ‪endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
108  {
109  $this->expectException(\InvalidArgumentException::class);
110  $this->expectExceptionCode($expectedException);
111 
112  ‪StringUtility::endsWith($string, $part);
113  }
114 
121  {
122  return [
123  'match first part of string' => ['hello world', 'hello'],
124  'match first char of string' => ['hello world', 'h'],
125  'match whole string' => ['hello', 'hello'],
126  'integer is part of string with same number' => ['24', 24],
127  'string is part of integer with same number' => [24, '24'],
128  'integer is part of string starting with same number' => ['24, please gimme beer', 24],
129  ];
130  }
131 
136  public function ‪beginsWithReturnsTrueForMatchingFirstPart($string, $part)
137  {
138  $this->assertTrue(‪StringUtility::beginsWith($string, $part));
139  }
140 
147  {
148  return [
149  'no string match' => ['hello', 'bye'],
150  'no case sensitive string match' => ['hello world', 'Hello'],
151  'string in empty string' => ['', 'foo']
152  ];
153  }
154 
159  public function ‪beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
160  {
161  $this->assertFalse(‪StringUtility::beginsWith($string, $part));
162  }
163 
170  {
171  return [
172  'array is not part of string' => ['string', [], 1347135547],
173  'NULL is not part of string' => ['string', null, 1347135547],
174  'empty string is not part of string' => ['string', '', 1347135547],
175  'string is not part of array' => [[], 'string', 1347135546],
176  'NULL is not part of array' => [[], null, 1347135546],
177  'string is not part of NULL' => [null, 'string', 1347135546],
178  'array is not part of NULL' => [null, [], 1347135546],
179  'integer is not part of NULL' => [null, 0, 1347135546],
180  'empty string is not part of NULL' => [null, '', 1347135546],
181  'NULL is not part of empty string' => ['', null, 1347135547],
182  'FALSE is not part of empty string' => ['', false, 1347135547],
183  'empty string is not part of FALSE' => [false, '', 1347135547],
184  'empty string is not part of integer' => [0, '', 1347135547],
185  'string is not part of object' => [new \stdClass(), 'foo', 1347135546],
186  'object is not part of string' => ['foo', new \stdClass(), 1347135547],
187  ];
188  }
189 
198  public function ‪beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
199  {
200  $this->expectException(\InvalidArgumentException::class);
201  $this->expectExceptionCode($expectedException);
202 
203  ‪StringUtility::beginsWith($string, $part);
204  }
205 
210  {
211  $id = ‪StringUtility::getUniqueId('NEW');
212  $this->assertEquals('NEW', substr($id, 0, 3));
213  }
214 
219  {
220  $this->assertNotContains('.', ‪StringUtility::getUniqueId());
221  }
222 
229  public function ‪escapeCssSelector(string $selector, string $expectedValue)
230  {
231  $this->assertEquals($expectedValue, ‪StringUtility::escapeCssSelector($selector));
232  }
233 
237  public function ‪escapeCssSelectorDataProvider(): array
238  {
239  return [
240  ['data.field', 'data\\.field'],
241  ['#theId', '\\#theId'],
242  ['.theId:hover', '\\.theId\\:hover'],
243  ['.theId:hover', '\\.theId\\:hover'],
244  ['input[name=foo]', 'input\\[name\\=foo\\]'],
245  ];
246  }
247 
254  public function ‪removeByteOrderMark(string $input, string $expectedValue): void
255  {
256  // assertContains is necessary as one test contains non-string characters
257  $this->assertSame($expectedValue, ‪StringUtility::removeByteOrderMark(hex2bin($input)));
258  }
259 
263  public function ‪removeByteOrderMarkDataProvider(): array
264  {
265  return [
266  'BOM gets removed' => [
267  'efbbbf424f4d2061742074686520626567696e6e696e6720676574732072656d6f766564',
268  'BOM at the beginning gets removed'
269  ],
270  'No BOM available' => [
271  '4e6f20424f4d20617661696c61626c65',
272  'No BOM available',
273  ],
274  ];
275  }
276 
284  public function ‪searchStringWildcard($haystack, $needle, $result)
285  {
286  $this->assertSame($result, ‪StringUtility::searchStringWildcard($haystack, $needle));
287  }
288 
292  public function ‪searchStringWildcardDataProvider(): array
293  {
294  return [
295  'Simple wildard single character with *' => [
296  'TYPO3',
297  'TY*O3',
298  true
299  ],
300  'Simple wildard multiple character with *' => [
301  'TYPO3',
302  'T*P*3',
303  true
304  ],
305  'Simple wildard multiple character for one placeholder with *' => [
306  'TYPO3',
307  'T*3',
308  true
309  ],
310  'Simple wildard single character with ?' => [
311  'TYPO3',
312  'TY?O3',
313  true
314  ],
315  'Simple wildard multiple character with ?' => [
316  'TYPO3',
317  'T?P?3',
318  true
319  ],
320  'Simple wildard multiple character for one placeholder with ?' => [
321  'TYPO3',
322  'T?3',
323  false
324  ],
325  'RegExp' => [
326  'TYPO3',
327  '/^TYPO(\d)$/',
328  true
329  ],
330  ];
331  }
332 }
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard($haystack, $needle)
Definition: StringUtility.php:133
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\escapeCssSelector
‪escapeCssSelector(string $selector, string $expectedValue)
Definition: StringUtilityTest.php:229
‪TYPO3\CMS\Core\Utility\StringUtility\endsWith
‪static bool endsWith($haystack, $needle)
Definition: StringUtility.php:60
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsFalseForNotMatchingLastPart
‪endsWithReturnsFalseForNotMatchingLastPart($string, $part)
Definition: StringUtilityTest.php:72
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\getUniqueIdReturnsIdWithPrefix
‪getUniqueIdReturnsIdWithPrefix()
Definition: StringUtilityTest.php:209
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsThrowsExceptionWithInvalidArguments
‪endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
Definition: StringUtilityTest.php:107
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider
‪array endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider()
Definition: StringUtilityTest.php:82
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\removeByteOrderMark
‪removeByteOrderMark(string $input, string $expectedValue)
Definition: StringUtilityTest.php:254
‪TYPO3\CMS\Core\Utility\StringUtility\escapeCssSelector
‪static string escapeCssSelector(string $selector)
Definition: StringUtility.php:106
‪TYPO3\CMS\Core\Utility\StringUtility\beginsWith
‪static bool beginsWith($haystack, $needle)
Definition: StringUtility.php:31
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest
Definition: StringUtilityTest.php:24
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsTrueForMatchingFirstPartDataProvider
‪array beginsWithReturnsTrueForMatchingFirstPartDataProvider()
Definition: StringUtilityTest.php:120
‪TYPO3\CMS\Core\Utility\StringUtility\removeByteOrderMark
‪static string removeByteOrderMark(string $input)
Definition: StringUtility.php:117
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsTrueForMatchingLastPart
‪endsWithReturnsTrueForMatchingLastPart($string, $part)
Definition: StringUtilityTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\escapeCssSelectorDataProvider
‪array escapeCssSelectorDataProvider()
Definition: StringUtilityTest.php:237
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\searchStringWildcardDataProvider
‪array searchStringWildcardDataProvider()
Definition: StringUtilityTest.php:292
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsInvalidArgumentDataProvider
‪array beginsWithReturnsInvalidArgumentDataProvider()
Definition: StringUtilityTest.php:169
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\searchStringWildcard
‪searchStringWildcard($haystack, $needle, $result)
Definition: StringUtilityTest.php:284
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\getUniqueIdReturnsIdWithoutDot
‪getUniqueIdReturnsIdWithoutDot()
Definition: StringUtilityTest.php:218
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsTrueForMatchingFirstPart
‪beginsWithReturnsTrueForMatchingFirstPart($string, $part)
Definition: StringUtilityTest.php:136
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\removeByteOrderMarkDataProvider
‪array removeByteOrderMarkDataProvider()
Definition: StringUtilityTest.php:263
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsTrueForMatchingLastPartDataProvider
‪array endsWithReturnsTrueForMatchingLastPartDataProvider()
Definition: StringUtilityTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsFalseForNotMatchingLastPartDataProvider
‪array endsWithReturnsFalseForNotMatchingLastPartDataProvider()
Definition: StringUtilityTest.php:57
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:91
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:21
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsFalseForNotMatchingFirstPart
‪beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
Definition: StringUtilityTest.php:159
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsThrowsExceptionWithInvalidArguments
‪beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
Definition: StringUtilityTest.php:198
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsFalseForNotMatchingFirstPartDataProvider
‪array beginsWithReturnsFalseForNotMatchingFirstPartDataProvider()
Definition: StringUtilityTest.php:146