TYPO3 CMS  TYPO3_8-7
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 
22 class StringUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
30  {
31  return [
32  'match last part of string' => ['hello world', 'world'],
33  'match last char of string' => ['hellod world', 'd'],
34  'match whole string' => ['hello', 'hello'],
35  'integer is part of string with same number' => ['24', 24],
36  'string is part of integer with same number' => [24, '24'],
37  'integer is part of string ending with same number' => ['please gimme beer, 24', 24]
38  ];
39  }
40 
45  public function endsWithReturnsTrueForMatchingLastPart($string, $part)
46  {
47  $this->assertTrue(StringUtility::endsWith($string, $part));
48  }
49 
56  {
57  return [
58  'no string match' => ['hello', 'bye'],
59  'no case sensitive string match' => ['hello world', 'World'],
60  'string is part but not last part' => ['hello world', 'worl'],
61  'integer is not part of empty string' => ['', 0],
62  'longer string is not part of shorter string' => ['a', 'aa'],
63  ];
64  }
65 
70  public function endsWithReturnsFalseForNotMatchingLastPart($string, $part)
71  {
72  $this->assertFalse(StringUtility::endsWith($string, $part));
73  }
74 
81  {
82  return [
83  'array is not part of string' => ['string', [], 1347135545],
84  'NULL is not part of string' => ['string', null, 1347135545],
85  'empty string is not part of string' => ['string', '', 1347135545],
86  'string is not part of array' => [[], 'string', 1347135544],
87  'NULL is not part of array' => [[], null, 1347135544],
88  'string is not part of NULL' => [null, 'string', 1347135544],
89  'array is not part of NULL' => [null, [], 1347135544],
90  'integer is not part of NULL' => [null, 0, 1347135544],
91  'empty string is not part of NULL' => [null, '', 1347135544],
92  'NULL is not part of empty string' => ['', null, 1347135545],
93  'FALSE is not part of empty string' => ['', false, 1347135545],
94  'empty string is not part of FALSE' => [false, '', 1347135545],
95  'empty string is not part of integer' => [0, '', 1347135545],
96  'string is not part of object' => [new \stdClass(), 'foo', 1347135544],
97  'object is not part of string' => ['foo', new \stdClass(), 1347135545],
98  ];
99  }
100 
105  public function endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
106  {
107  $this->expectException(\InvalidArgumentException::class);
108  $this->expectExceptionCode($expectedException);
109 
110  StringUtility::endsWith($string, $part);
111  }
112 
119  {
120  return [
121  'match first part of string' => ['hello world', 'hello'],
122  'match first char of string' => ['hello world', 'h'],
123  'match whole string' => ['hello', 'hello'],
124  'integer is part of string with same number' => ['24', 24],
125  'string is part of integer with same number' => [24, '24'],
126  'integer is part of string starting with same number' => ['24, please gimme beer', 24],
127  ];
128  }
129 
134  public function beginsWithReturnsTrueForMatchingFirstPart($string, $part)
135  {
136  $this->assertTrue(StringUtility::beginsWith($string, $part));
137  }
138 
145  {
146  return [
147  'no string match' => ['hello', 'bye'],
148  'no case sensitive string match' => ['hello world', 'Hello'],
149  'string in empty string' => ['', 'foo']
150  ];
151  }
152 
157  public function beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
158  {
159  $this->assertFalse(StringUtility::beginsWith($string, $part));
160  }
161 
168  {
169  return [
170  'array is not part of string' => ['string', [], 1347135547],
171  'NULL is not part of string' => ['string', null, 1347135547],
172  'empty string is not part of string' => ['string', '', 1347135547],
173  'string is not part of array' => [[], 'string', 1347135546],
174  'NULL is not part of array' => [[], null, 1347135546],
175  'string is not part of NULL' => [null, 'string', 1347135546],
176  'array is not part of NULL' => [null, [], 1347135546],
177  'integer is not part of NULL' => [null, 0, 1347135546],
178  'empty string is not part of NULL' => [null, '', 1347135546],
179  'NULL is not part of empty string' => ['', null, 1347135547],
180  'FALSE is not part of empty string' => ['', false, 1347135547],
181  'empty string is not part of FALSE' => [false, '', 1347135547],
182  'empty string is not part of integer' => [0, '', 1347135547],
183  'string is not part of object' => [new \stdClass(), 'foo', 1347135546],
184  'object is not part of string' => ['foo', new \stdClass(), 1347135547],
185  ];
186  }
187 
196  public function beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
197  {
198  $this->expectException(\InvalidArgumentException::class);
199  $this->expectExceptionCode($expectedException);
200 
201  StringUtility::beginsWith($string, $part);
202  }
203 
208  {
209  $id = StringUtility::getUniqueId('NEW');
210  $this->assertEquals('NEW', substr($id, 0, 3));
211  }
212 
217  {
218  $this->assertNotContains('.', StringUtility::getUniqueId());
219  }
220 
227  public function escapeCssSelector(string $selector, string $expectedValue)
228  {
229  $this->assertEquals($expectedValue, StringUtility::escapeCssSelector($selector));
230  }
231 
235  public function escapeCssSelectorDataProvider(): array
236  {
237  return [
238  ['data.field', 'data\\.field'],
239  ['#theId', '\\#theId'],
240  ['.theId:hover', '\\.theId\\:hover'],
241  ['.theId:hover', '\\.theId\\:hover'],
242  ['input[name=foo]', 'input\\[name\\=foo\\]'],
243  ];
244  }
245 
252  public function removeByteOrderMark(string $input, string $expectedValue)
253  {
254  // assertContains is necessary as one test contains non-string characters
255  $this->assertSame($expectedValue, StringUtility::removeByteOrderMark(hex2bin($input)));
256  }
257 
261  public function removeByteOrderMarkDataProvider(): array
262  {
263  return [
264  'BOM gets removed' => [
265  'efbbbf424f4d2061742074686520626567696e6e696e6720676574732072656d6f766564',
266  'BOM at the beginning gets removed'
267  ],
268  'No BOM available' => [
269  '4e6f20424f4d20617661696c61626c65',
270  'No BOM available',
271  ],
272  ];
273  }
274 }
escapeCssSelector(string $selector, string $expectedValue)
static removeByteOrderMark(string $input)
removeByteOrderMark(string $input, string $expectedValue)
static escapeCssSelector(string $selector)
static beginsWith($haystack, $needle)
endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
static endsWith($haystack, $needle)