96306a5c87966634226d8f39a249dff79fd9d1f4
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / StringUtilityTest.php
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
17 use TYPO3\CMS\Core\Utility\StringUtility;
18
19 /**
20 * Testcase for class \TYPO3\CMS\Core\Utility\StringUtility
21 */
22 class StringUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
23 {
24 /**
25 * Data provider for isLastPartOfStrReturnsTrueForMatchingLastParts
26 *
27 * @return array
28 */
29 public function isLastPartOfStringReturnsTrueForMatchingFirstPartDataProvider()
30 {
31 return array(
32 'match last part of string' => array('hello world', 'world'),
33 'match last char of string' => array('hellod world', 'd'),
34 'match whole string' => array('hello', 'hello'),
35 'integer is part of string with same number' => array('24', 24),
36 'string is part of integer with same number' => array(24, '24'),
37 'integer is part of string starting with same number' => array('please gimme beer, 24', 24)
38 );
39 }
40
41 /**
42 * @test
43 * @dataProvider isLastPartOfStringReturnsTrueForMatchingFirstPartDataProvider
44 */
45 public function isLastPartOfStringReturnsTrueForMatchingFirstPart($string, $part)
46 {
47 $this->assertTrue(\TYPO3\CMS\Core\Utility\StringUtility::isLastPartOfString($string, $part));
48 }
49
50 /**
51 * Data provider for checkisLastPartOfStringReturnsFalseForNotMatchingFirstParts
52 *
53 * @return array
54 */
55 public function isLastPartOfStringReturnsFalseForNotMatchingFirstPartDataProvider()
56 {
57 return array(
58 'no string match' => array('hello', 'bye'),
59 'no case sensitive string match' => array('hello world', 'World'),
60 );
61 }
62
63 /**
64 * @test
65 * @dataProvider isLastPartOfStringReturnsFalseForNotMatchingFirstPartDataProvider
66 */
67 public function isLastPartOfStringReturnsFalseForNotMatchingFirstPart($string, $part)
68 {
69 $this->assertFalse(\TYPO3\CMS\Core\Utility\StringUtility::isLastPartOfString($string, $part));
70 }
71
72 /**
73 * Data provider for isLastPartOfStringReturnsThrowsExceptionWithInvalidArguments
74 *
75 * @return array
76 */
77 public function isLastPartOfStringReturnsInvalidArgumentDataProvider()
78 {
79 return array(
80 'array is not part of string' => array('string', array()),
81 'string is not part of array' => array(array(), 'string'),
82 'NULL is not part of string' => array('string', null),
83 'null is not part of array' => array(null, 'string'),
84 'NULL is not part of array' => array(array(), null),
85 'array is not part of null' => array(null, array()),
86 'NULL is not part of empty string' => array('', null),
87 'false is not part of empty string' => array('', false),
88 'empty string is not part of NULL' => array(null, ''),
89 'empty string is not part of false' => array(false, ''),
90 'empty string is not part of zero integer' => array(0, ''),
91 'zero integer is not part of NULL' => array(null, 0),
92 'zero integer is not part of empty string' => array('', 0),
93 'string is not part of object' => array(new \stdClass(), 'foo'),
94 'object is not part of string' => array('foo', new \stdClass()),
95 );
96 }
97
98 /**
99 * @test
100 * @dataProvider isLastPartOfStringReturnsInvalidArgumentDataProvider
101 * @expectedException \InvalidArgumentException
102 */
103 public function isLastPartOfStringReturnsThrowsExceptionWithInvalidArguments($string, $part)
104 {
105 $this->assertFalse(\TYPO3\CMS\Core\Utility\StringUtility::isLastPartOfString($string, $part));
106 }
107
108 /**
109 * Data provider for endsWithReturnsTrueForMatchingFirstPart
110 *
111 * @return array
112 */
113 public function endsWithReturnsTrueForMatchingLastPartDataProvider()
114 {
115 return array(
116 'match last part of string' => array('hello world', 'world'),
117 'match last char of string' => array('hellod world', 'd'),
118 'match whole string' => array('hello', 'hello'),
119 'integer is part of string with same number' => array('24', 24),
120 'string is part of integer with same number' => array(24, '24'),
121 'integer is part of string ending with same number' => array('please gimme beer, 24', 24)
122 );
123 }
124
125 /**
126 * @test
127 * @dataProvider endsWithReturnsTrueForMatchingLastPartDataProvider
128 */
129 public function endsWithReturnsTrueForMatchingLastPart($string, $part)
130 {
131 $this->assertTrue(StringUtility::endsWith($string, $part));
132 }
133
134 /**
135 * Data provider for check endsWithReturnsFalseForNotMatchingLastPart
136 *
137 * @return array
138 */
139 public function endsWithReturnsFalseForNotMatchingLastPartDataProvider()
140 {
141 return array(
142 'no string match' => array('hello', 'bye'),
143 'no case sensitive string match' => array('hello world', 'World'),
144 'string is part but not last part' => array('hello world', 'worl'),
145 'integer is not part of empty string' => array('', 0),
146 'longer string is not part of shorter string' => array('a', 'aa'),
147 );
148 }
149
150 /**
151 * @test
152 * @dataProvider endsWithReturnsFalseForNotMatchingLastPartDataProvider
153 */
154 public function endsWithReturnsFalseForNotMatchingLastPart($string, $part)
155 {
156 $this->assertFalse(StringUtility::endsWith($string, $part));
157 }
158
159 /**
160 * Data provider for endsWithReturnsThrowsExceptionWithInvalidArguments
161 *
162 * @return array
163 */
164 public function endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider()
165 {
166 return array(
167 'array is not part of string' => array('string', array()),
168 'NULL is not part of string' => array('string', null),
169 'empty string is not part of string' => array('string', ''),
170 'string is not part of array' => array(array(), 'string'),
171 'NULL is not part of array' => array(array(), null),
172 'string is not part of NULL' => array(null, 'string'),
173 'array is not part of NULL' => array(null, array()),
174 'integer is not part of NULL' => array(null, 0),
175 'empty string is not part of NULL' => array(null, ''),
176 'NULL is not part of empty string' => array('', null),
177 'FALSE is not part of empty string' => array('', false),
178 'empty string is not part of FALSE' => array(false, ''),
179 'empty string is not part of integer' => array(0, ''),
180 'string is not part of object' => array(new \stdClass(), 'foo'),
181 'object is not part of string' => array('foo', new \stdClass()),
182 );
183 }
184
185 /**
186 * @test
187 * @dataProvider endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider
188 * @expectedException \InvalidArgumentException
189 */
190 public function endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part)
191 {
192 StringUtility::endsWith($string, $part);
193 }
194
195 /**
196 * Data provider for beginsWithReturnsTrueForMatchingFirstPart
197 *
198 * @return array
199 */
200 public function beginsWithReturnsTrueForMatchingFirstPartDataProvider()
201 {
202 return array(
203 'match first part of string' => array('hello world', 'hello'),
204 'match first char of string' => array('hello world', 'h'),
205 'match whole string' => array('hello', 'hello'),
206 'integer is part of string with same number' => array('24', 24),
207 'string is part of integer with same number' => array(24, '24'),
208 'integer is part of string starting with same number' => array('24, please gimme beer', 24),
209 );
210 }
211
212 /**
213 * @test
214 * @dataProvider beginsWithReturnsTrueForMatchingFirstPartDataProvider
215 */
216 public function beginsWithReturnsTrueForMatchingFirstPart($string, $part)
217 {
218 $this->assertTrue(StringUtility::beginsWith($string, $part));
219 }
220
221 /**
222 * Data provider for check beginsWithReturnsFalseForNotMatchingFirstPart
223 *
224 * @return array
225 */
226 public function beginsWithReturnsFalseForNotMatchingFirstPartDataProvider()
227 {
228 return array(
229 'no string match' => array('hello', 'bye'),
230 'no case sensitive string match' => array('hello world', 'Hello'),
231 'string in empty string' => array('', 'foo')
232 );
233 }
234
235 /**
236 * @test
237 * @dataProvider beginsWithReturnsFalseForNotMatchingFirstPartDataProvider
238 */
239 public function beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
240 {
241 $this->assertFalse(StringUtility::beginsWith($string, $part));
242 }
243
244 /**
245 * Data provider for beginsWithReturnsThrowsExceptionWithInvalidArguments
246 *
247 * @return array
248 */
249 public function beginsWithReturnsInvalidArgumentDataProvider()
250 {
251 return array(
252 'array is not part of string' => array('string', array()),
253 'NULL is not part of string' => array('string', null),
254 'empty string is not part of string' => array('string', ''),
255 'string is not part of array' => array(array(), 'string'),
256 'NULL is not part of array' => array(array(), null),
257 'string is not part of NULL' => array(null, 'string'),
258 'array is not part of NULL' => array(null, array()),
259 'integer is not part of NULL' => array(null, 0),
260 'empty string is not part of NULL' => array(null, ''),
261 'NULL is not part of empty string' => array('', null),
262 'FALSE is not part of empty string' => array('', false),
263 'empty string is not part of FALSE' => array(false, ''),
264 'empty string is not part of integer' => array(0, ''),
265 'string is not part of object' => array(new \stdClass(), 'foo'),
266 'object is not part of string' => array('foo', new \stdClass()),
267 );
268 }
269
270 /**
271 * @test
272 * @dataProvider beginsWithReturnsInvalidArgumentDataProvider
273 * @expectedException \InvalidArgumentException
274 */
275 public function beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part)
276 {
277 StringUtility::beginsWith($string, $part);
278 }
279
280 /**
281 * @test
282 */
283 public function getUniqueIdReturnsIdWithPrefix()
284 {
285 $id = StringUtility::getUniqueId('NEW');
286 $this->assertEquals('NEW', substr($id, 0, 3));
287 }
288
289 /**
290 * @test
291 */
292 public function getUniqueIdReturnsIdWithoutDot()
293 {
294 $this->assertNotContains('.', StringUtility::getUniqueId());
295 }
296 }