TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
StringUtilityTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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 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 }
static endsWith($haystack, $needle)
static beginsWith($haystack, $needle)
endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)