TYPO3 CMS  TYPO3_7-6
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 
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 starting with same number' => ['please gimme beer, 24', 24]
38  ];
39  }
40 
45  public function isLastPartOfStringReturnsTrueForMatchingFirstPart($string, $part)
46  {
47  $this->assertTrue(\TYPO3\CMS\Core\Utility\StringUtility::isLastPartOfString($string, $part));
48  }
49 
56  {
57  return [
58  'no string match' => ['hello', 'bye'],
59  'no case sensitive string match' => ['hello world', 'World'],
60  ];
61  }
62 
68  {
69  $this->assertFalse(\TYPO3\CMS\Core\Utility\StringUtility::isLastPartOfString($string, $part));
70  }
71 
78  {
79  return [
80  'array is not part of string' => ['string', []],
81  'string is not part of array' => [[], 'string'],
82  'NULL is not part of string' => ['string', null],
83  'null is not part of array' => [null, 'string'],
84  'NULL is not part of array' => [[], null],
85  'array is not part of null' => [null, []],
86  'NULL is not part of empty string' => ['', null],
87  'false is not part of empty string' => ['', false],
88  'empty string is not part of NULL' => [null, ''],
89  'empty string is not part of false' => [false, ''],
90  'empty string is not part of zero integer' => [0, ''],
91  'zero integer is not part of NULL' => [null, 0],
92  'zero integer is not part of empty string' => ['', 0],
93  'string is not part of object' => [new \stdClass(), 'foo'],
94  'object is not part of string' => ['foo', new \stdClass()],
95  ];
96  }
97 
104  {
105  $this->assertFalse(\TYPO3\CMS\Core\Utility\StringUtility::isLastPartOfString($string, $part));
106  }
107 
114  {
115  return [
116  'match last part of string' => ['hello world', 'world'],
117  'match last char of string' => ['hellod world', 'd'],
118  'match whole string' => ['hello', 'hello'],
119  'integer is part of string with same number' => ['24', 24],
120  'string is part of integer with same number' => [24, '24'],
121  'integer is part of string ending with same number' => ['please gimme beer, 24', 24]
122  ];
123  }
124 
129  public function endsWithReturnsTrueForMatchingLastPart($string, $part)
130  {
131  $this->assertTrue(StringUtility::endsWith($string, $part));
132  }
133 
140  {
141  return [
142  'no string match' => ['hello', 'bye'],
143  'no case sensitive string match' => ['hello world', 'World'],
144  'string is part but not last part' => ['hello world', 'worl'],
145  'integer is not part of empty string' => ['', 0],
146  'longer string is not part of shorter string' => ['a', 'aa'],
147  ];
148  }
149 
154  public function endsWithReturnsFalseForNotMatchingLastPart($string, $part)
155  {
156  $this->assertFalse(StringUtility::endsWith($string, $part));
157  }
158 
165  {
166  return [
167  'array is not part of string' => ['string', []],
168  'NULL is not part of string' => ['string', null],
169  'empty string is not part of string' => ['string', ''],
170  'string is not part of array' => [[], 'string'],
171  'NULL is not part of array' => [[], null],
172  'string is not part of NULL' => [null, 'string'],
173  'array is not part of NULL' => [null, []],
174  'integer is not part of NULL' => [null, 0],
175  'empty string is not part of NULL' => [null, ''],
176  'NULL is not part of empty string' => ['', null],
177  'FALSE is not part of empty string' => ['', false],
178  'empty string is not part of FALSE' => [false, ''],
179  'empty string is not part of integer' => [0, ''],
180  'string is not part of object' => [new \stdClass(), 'foo'],
181  'object is not part of string' => ['foo', new \stdClass()],
182  ];
183  }
184 
191  {
192  StringUtility::endsWith($string, $part);
193  }
194 
201  {
202  return [
203  'match first part of string' => ['hello world', 'hello'],
204  'match first char of string' => ['hello world', 'h'],
205  'match whole string' => ['hello', 'hello'],
206  'integer is part of string with same number' => ['24', 24],
207  'string is part of integer with same number' => [24, '24'],
208  'integer is part of string starting with same number' => ['24, please gimme beer', 24],
209  ];
210  }
211 
216  public function beginsWithReturnsTrueForMatchingFirstPart($string, $part)
217  {
218  $this->assertTrue(StringUtility::beginsWith($string, $part));
219  }
220 
227  {
228  return [
229  'no string match' => ['hello', 'bye'],
230  'no case sensitive string match' => ['hello world', 'Hello'],
231  'string in empty string' => ['', 'foo']
232  ];
233  }
234 
239  public function beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
240  {
241  $this->assertFalse(StringUtility::beginsWith($string, $part));
242  }
243 
250  {
251  return [
252  'array is not part of string' => ['string', []],
253  'NULL is not part of string' => ['string', null],
254  'empty string is not part of string' => ['string', ''],
255  'string is not part of array' => [[], 'string'],
256  'NULL is not part of array' => [[], null],
257  'string is not part of NULL' => [null, 'string'],
258  'array is not part of NULL' => [null, []],
259  'integer is not part of NULL' => [null, 0],
260  'empty string is not part of NULL' => [null, ''],
261  'NULL is not part of empty string' => ['', null],
262  'FALSE is not part of empty string' => ['', false],
263  'empty string is not part of FALSE' => [false, ''],
264  'empty string is not part of integer' => [0, ''],
265  'string is not part of object' => [new \stdClass(), 'foo'],
266  'object is not part of string' => ['foo', new \stdClass()],
267  ];
268  }
269 
276  {
277  StringUtility::beginsWith($string, $part);
278  }
279 
284  {
285  $id = StringUtility::getUniqueId('NEW');
286  $this->assertEquals('NEW', substr($id, 0, 3));
287  }
288 
293  {
294  $this->assertNotContains('.', StringUtility::getUniqueId());
295  }
296 }
static isLastPartOfString($haystack, $needle)
static beginsWith($haystack, $needle)
static endsWith($haystack, $needle)