‪TYPO3CMS  10.4
StringUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
24 class ‪StringUtilityTest extends UnitTestCase
25 {
26 
33  {
34  return [
35  'match last part of string' => ['hello world', 'world'],
36  'match last char of string' => ['hello world', 'd'],
37  'match whole string' => ['hello', 'hello'],
38  'integer is part of string with same number' => ['24', 24],
39  'string is part of integer with same number' => [24, '24'],
40  'integer is part of string ending with same number' => ['please gimme beer, 24', 24]
41  ];
42  }
43 
48  public function ‪endsWithReturnsTrueForMatchingLastPart($string, $part)
49  {
50  self::assertTrue(‪StringUtility::endsWith($string, $part));
51  }
52 
59  {
60  return [
61  'no string match' => ['hello', 'bye'],
62  'no case sensitive string match' => ['hello world', 'World'],
63  'string is part but not last part' => ['hello world', 'worl'],
64  'integer is not part of empty string' => ['', 0],
65  'longer string is not part of shorter string' => ['a', 'aa'],
66  ];
67  }
68 
73  public function ‪endsWithReturnsFalseForNotMatchingLastPart($string, $part)
74  {
75  self::assertFalse(‪StringUtility::endsWith($string, $part));
76  }
77 
84  {
85  return [
86  'array is not part of string' => ['string', [], 1347135545],
87  'NULL is not part of string' => ['string', null, 1347135545],
88  'empty string is not part of string' => ['string', '', 1347135545],
89  'string is not part of array' => [[], 'string', 1347135544],
90  'NULL is not part of array' => [[], null, 1347135544],
91  'string is not part of NULL' => [null, 'string', 1347135544],
92  'array is not part of NULL' => [null, [], 1347135544],
93  'integer is not part of NULL' => [null, 0, 1347135544],
94  'empty string is not part of NULL' => [null, '', 1347135544],
95  'NULL is not part of empty string' => ['', null, 1347135545],
96  'FALSE is not part of empty string' => ['', false, 1347135545],
97  'empty string is not part of FALSE' => [false, '', 1347135545],
98  'empty string is not part of integer' => [0, '', 1347135545],
99  'string is not part of object' => [new \stdClass(), 'foo', 1347135544],
100  'object is not part of string' => ['foo', new \stdClass(), 1347135545],
101  ];
102  }
103 
108  public function ‪endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
109  {
110  $this->expectException(\InvalidArgumentException::class);
111  $this->expectExceptionCode($expectedException);
112 
113  ‪StringUtility::endsWith($string, $part);
114  }
115 
122  {
123  return [
124  'match first part of string' => ['hello world', 'hello'],
125  'match first char of string' => ['hello world', 'h'],
126  'match whole string' => ['hello', 'hello'],
127  'integer is part of string with same number' => ['24', 24],
128  'string is part of integer with same number' => [24, '24'],
129  'integer is part of string starting with same number' => ['24, please gimme beer', 24],
130  ];
131  }
132 
137  public function ‪beginsWithReturnsTrueForMatchingFirstPart($string, $part)
138  {
139  self::assertTrue(‪StringUtility::beginsWith($string, $part));
140  }
141 
148  {
149  return [
150  'no string match' => ['hello', 'bye'],
151  'no case sensitive string match' => ['hello world', 'Hello'],
152  'string in empty string' => ['', 'foo']
153  ];
154  }
155 
160  public function ‪beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
161  {
162  self::assertFalse(‪StringUtility::beginsWith($string, $part));
163  }
164 
171  {
172  return [
173  'array is not part of string' => ['string', [], 1347135547],
174  'NULL is not part of string' => ['string', null, 1347135547],
175  'empty string is not part of string' => ['string', '', 1347135547],
176  'string is not part of array' => [[], 'string', 1347135546],
177  'NULL is not part of array' => [[], null, 1347135546],
178  'string is not part of NULL' => [null, 'string', 1347135546],
179  'array is not part of NULL' => [null, [], 1347135546],
180  'integer is not part of NULL' => [null, 0, 1347135546],
181  'empty string is not part of NULL' => [null, '', 1347135546],
182  'NULL is not part of empty string' => ['', null, 1347135547],
183  'FALSE is not part of empty string' => ['', false, 1347135547],
184  'empty string is not part of FALSE' => [false, '', 1347135547],
185  'empty string is not part of integer' => [0, '', 1347135547],
186  'string is not part of object' => [new \stdClass(), 'foo', 1347135546],
187  'object is not part of string' => ['foo', new \stdClass(), 1347135547],
188  ];
189  }
190 
199  public function ‪beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
200  {
201  $this->expectException(\InvalidArgumentException::class);
202  $this->expectExceptionCode($expectedException);
203 
204  ‪StringUtility::beginsWith($string, $part);
205  }
206 
211  {
212  $id = ‪StringUtility::getUniqueId('NEW');
213  self::assertEquals('NEW', substr($id, 0, 3));
214  }
215 
220  {
221  self::assertStringNotContainsString('.', ‪StringUtility::getUniqueId());
222  }
223 
230  public function ‪escapeCssSelector(string $selector, string $expectedValue)
231  {
232  self::assertEquals($expectedValue, ‪StringUtility::escapeCssSelector($selector));
233  }
234 
238  public function ‪escapeCssSelectorDataProvider(): array
239  {
240  return [
241  ['data.field', 'data\\.field'],
242  ['#theId', '\\#theId'],
243  ['.theId:hover', '\\.theId\\:hover'],
244  ['.theId:hover', '\\.theId\\:hover'],
245  ['input[name=foo]', 'input\\[name\\=foo\\]'],
246  ];
247  }
248 
255  public function ‪removeByteOrderMark(string $input, string $expectedValue): void
256  {
257  // assertContains is necessary as one test contains non-string characters
258  self::assertSame($expectedValue, ‪StringUtility::removeByteOrderMark(hex2bin($input)));
259  }
260 
264  public function ‪removeByteOrderMarkDataProvider(): array
265  {
266  return [
267  'BOM gets removed' => [
268  'efbbbf424f4d2061742074686520626567696e6e696e6720676574732072656d6f766564',
269  'BOM at the beginning gets removed'
270  ],
271  'No BOM available' => [
272  '4e6f20424f4d20617661696c61626c65',
273  'No BOM available',
274  ],
275  ];
276  }
277 
285  public function ‪searchStringWildcard($haystack, $needle, $result)
286  {
287  self::assertSame($result, ‪StringUtility::searchStringWildcard($haystack, $needle));
288  }
289 
293  public function ‪searchStringWildcardDataProvider(): array
294  {
295  return [
296  'Simple wildcard single character with *' => [
297  'TYPO3',
298  'TY*O3',
299  true
300  ],
301  'Simple wildcard multiple character with *' => [
302  'TYPO3',
303  'T*P*3',
304  true
305  ],
306  'Simple wildcard multiple character for one placeholder with *' => [
307  'TYPO3',
308  'T*3',
309  true
310  ],
311  'Simple wildcard single character with ?' => [
312  'TYPO3',
313  'TY?O3',
314  true
315  ],
316  'Simple wildcard multiple character with ?' => [
317  'TYPO3',
318  'T?P?3',
319  true
320  ],
321  'Simple wildcard multiple character for one placeholder with ?' => [
322  'TYPO3',
323  'T?3',
324  false
325  ],
326  'RegExp' => [
327  'TYPO3',
328  '/^TYPO(\d)$/',
329  true
330  ],
331  ];
332  }
333 
340  {
341  yield 'Pad right to 10 with string with uneven length' => ['ABC', 10, ' ', STR_PAD_RIGHT];
342  yield 'Pad left to 10 with string with uneven length' => ['ABC', 10, ' ', STR_PAD_LEFT];
343  yield 'Pad both to 10 with string with uneven length' => ['ABC', 10, ' ', STR_PAD_BOTH];
344  yield 'Pad right to 10 with string with uneven length and 2 character padding' => ['ABC', 10, '12', STR_PAD_RIGHT];
345  yield 'Pad left to 10 with string with uneven length and 2 character padding' => ['ABC', 10, '12', STR_PAD_LEFT];
346  yield 'Pad both to 10 with string with uneven length and 2 character padding' => ['ABC', 10, '12', STR_PAD_BOTH];
347 
348  yield 'Pad right to 10 with string with even length' => ['AB', 10, ' ', STR_PAD_RIGHT];
349  yield 'Pad left to 10 with string with even length' => ['AB', 10, ' ', STR_PAD_LEFT];
350  yield 'Pad both to 10 with string with even length' => ['AB', 10, ' ', STR_PAD_BOTH];
351  yield 'Pad right to 10 with string with even length and 2 character padding' => ['AB', 10, '12', STR_PAD_RIGHT];
352  yield 'Pad left to 10 with string with even length and 2 character padding' => ['AB', 10, '12', STR_PAD_LEFT];
353  yield 'Pad both to 10 with string with even length and 2 character padding' => ['AB', 10, '12', STR_PAD_BOTH];
354  }
355 
369  public function ‪multibyteStringPadReturnsSameValueAsStrPadForAsciiStrings(string $string, int $length, string $pad_string, int $pad_type): void
370  {
371  self::assertEquals(
372  str_pad($string, $length, $pad_string, $pad_type),
373  ‪StringUtility::multibyteStringPad($string, $length, $pad_string, $pad_type)
374  );
375  }
376 
378  {
379  yield 'Pad right to 8 with string with uneven length' => ['häh ', 'häh', 8, ' ', STR_PAD_RIGHT];
380  yield 'Pad left to 8 with string with uneven length' => [' häh', 'häh', 8, ' ', STR_PAD_LEFT];
381  yield 'Pad both to 8 with string with uneven length' => [' häh ', 'häh', 8, ' ', STR_PAD_BOTH];
382  yield 'Pad right to 8 with string with uneven length and 2 character padding' => ['hühäöäöä', 'hüh', 8, 'äö', STR_PAD_RIGHT];
383  yield 'Pad left to 8 with string with uneven length and 2 character padding' => ['äöäöähüh', 'hüh', 8, 'äö', STR_PAD_LEFT];
384  yield 'Pad both to 8 with string with uneven length and 2 character padding' => ['äöhühäöä', 'hüh', 8, 'äö', STR_PAD_BOTH];
385 
386  yield 'Pad right to 8 with string with even length' => ['hä ', 'hä', 8, ' ', STR_PAD_RIGHT];
387  yield 'Pad left to 8 with string with even length' => [' hä', 'hä', 8, ' ', STR_PAD_LEFT];
388  yield 'Pad both to 8 with string with even length' => [' hä ', 'hä', 8, ' ', STR_PAD_BOTH];
389  yield 'Pad right to 8 with string with even length and 2 character padding with MB char' => ['hüäöäöäö', 'hü', 8, 'äö', STR_PAD_RIGHT];
390  yield 'Pad left to 8 with string with even length and 2 character padding with MB char' => ['äöäöäöhü', 'hü', 8, 'äö', STR_PAD_LEFT];
391  yield 'Pad both to 8 with string with even length and 2 character padding with MB char' => ['äöähüäöä', 'hü', 8, 'äö', STR_PAD_BOTH];
392  }
393 
405  public function ‪multibyteStringPadReturnsCorrectResultsMultibyte(string $expectedResult, string $string, int $length, string $pad_string, int $pad_type): void
406  {
407  self::assertEquals(
408  $expectedResult,
409  ‪StringUtility::multibyteStringPad($string, $length, $pad_string, $pad_type)
410  );
411  }
412 }
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard($haystack, $needle)
Definition: StringUtility.php:134
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\escapeCssSelector
‪escapeCssSelector(string $selector, string $expectedValue)
Definition: StringUtilityTest.php:230
‪TYPO3\CMS\Core\Utility\StringUtility\endsWith
‪static bool endsWith($haystack, $needle)
Definition: StringUtility.php:61
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsFalseForNotMatchingLastPart
‪endsWithReturnsFalseForNotMatchingLastPart($string, $part)
Definition: StringUtilityTest.php:73
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\getUniqueIdReturnsIdWithPrefix
‪getUniqueIdReturnsIdWithPrefix()
Definition: StringUtilityTest.php:210
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsThrowsExceptionWithInvalidArguments
‪endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
Definition: StringUtilityTest.php:108
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider
‪array endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider()
Definition: StringUtilityTest.php:83
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\removeByteOrderMark
‪removeByteOrderMark(string $input, string $expectedValue)
Definition: StringUtilityTest.php:255
‪TYPO3\CMS\Core\Utility\StringUtility\escapeCssSelector
‪static string escapeCssSelector(string $selector)
Definition: StringUtility.php:107
‪TYPO3\CMS\Core\Utility\StringUtility\beginsWith
‪static bool beginsWith($haystack, $needle)
Definition: StringUtility.php:32
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest
Definition: StringUtilityTest.php:25
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsTrueForMatchingFirstPartDataProvider
‪array beginsWithReturnsTrueForMatchingFirstPartDataProvider()
Definition: StringUtilityTest.php:121
‪TYPO3\CMS\Core\Utility\StringUtility\removeByteOrderMark
‪static string removeByteOrderMark(string $input)
Definition: StringUtility.php:118
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsTrueForMatchingLastPart
‪endsWithReturnsTrueForMatchingLastPart($string, $part)
Definition: StringUtilityTest.php:48
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\escapeCssSelectorDataProvider
‪array escapeCssSelectorDataProvider()
Definition: StringUtilityTest.php:238
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsCorrectResultsMultibyteDataProvider
‪multibyteStringPadReturnsCorrectResultsMultibyteDataProvider()
Definition: StringUtilityTest.php:377
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsSameValueAsStrPadForAsciiStringsDataProvider
‪Generator multibyteStringPadReturnsSameValueAsStrPadForAsciiStringsDataProvider()
Definition: StringUtilityTest.php:339
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsCorrectResultsMultibyte
‪multibyteStringPadReturnsCorrectResultsMultibyte(string $expectedResult, string $string, int $length, string $pad_string, int $pad_type)
Definition: StringUtilityTest.php:405
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\searchStringWildcardDataProvider
‪array searchStringWildcardDataProvider()
Definition: StringUtilityTest.php:293
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsInvalidArgumentDataProvider
‪array beginsWithReturnsInvalidArgumentDataProvider()
Definition: StringUtilityTest.php:170
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\searchStringWildcard
‪searchStringWildcard($haystack, $needle, $result)
Definition: StringUtilityTest.php:285
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\getUniqueIdReturnsIdWithoutDot
‪getUniqueIdReturnsIdWithoutDot()
Definition: StringUtilityTest.php:219
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsTrueForMatchingFirstPart
‪beginsWithReturnsTrueForMatchingFirstPart($string, $part)
Definition: StringUtilityTest.php:137
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\removeByteOrderMarkDataProvider
‪array removeByteOrderMarkDataProvider()
Definition: StringUtilityTest.php:264
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsTrueForMatchingLastPartDataProvider
‪array endsWithReturnsTrueForMatchingLastPartDataProvider()
Definition: StringUtilityTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\endsWithReturnsFalseForNotMatchingLastPartDataProvider
‪array endsWithReturnsFalseForNotMatchingLastPartDataProvider()
Definition: StringUtilityTest.php:58
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsSameValueAsStrPadForAsciiStrings
‪multibyteStringPadReturnsSameValueAsStrPadForAsciiStrings(string $string, int $length, string $pad_string, int $pad_type)
Definition: StringUtilityTest.php:369
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsFalseForNotMatchingFirstPart
‪beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
Definition: StringUtilityTest.php:160
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsThrowsExceptionWithInvalidArguments
‪beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
Definition: StringUtilityTest.php:199
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\beginsWithReturnsFalseForNotMatchingFirstPartDataProvider
‪array beginsWithReturnsFalseForNotMatchingFirstPartDataProvider()
Definition: StringUtilityTest.php:147
‪TYPO3\CMS\Core\Utility\StringUtility\multibyteStringPad
‪static string multibyteStringPad(string $string, int $length, string $pad_string=' ', int $pad_type=STR_PAD_RIGHT, string $encoding='UTF-8')
Definition: StringUtility.php:165