[TASK] Removed deprecated code from lowlevel and utility functions
[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 /**
26 * Data provider for endsWithReturnsTrueForMatchingFirstPart
27 *
28 * @return array
29 */
30 public function endsWithReturnsTrueForMatchingLastPartDataProvider()
31 {
32 return array(
33 'match last part of string' => array('hello world', 'world'),
34 'match last char of string' => array('hellod world', 'd'),
35 'match whole string' => array('hello', 'hello'),
36 'integer is part of string with same number' => array('24', 24),
37 'string is part of integer with same number' => array(24, '24'),
38 'integer is part of string ending with same number' => array('please gimme beer, 24', 24)
39 );
40 }
41
42 /**
43 * @test
44 * @dataProvider endsWithReturnsTrueForMatchingLastPartDataProvider
45 */
46 public function endsWithReturnsTrueForMatchingLastPart($string, $part)
47 {
48 $this->assertTrue(StringUtility::endsWith($string, $part));
49 }
50
51 /**
52 * Data provider for check endsWithReturnsFalseForNotMatchingLastPart
53 *
54 * @return array
55 */
56 public function endsWithReturnsFalseForNotMatchingLastPartDataProvider()
57 {
58 return array(
59 'no string match' => array('hello', 'bye'),
60 'no case sensitive string match' => array('hello world', 'World'),
61 'string is part but not last part' => array('hello world', 'worl'),
62 'integer is not part of empty string' => array('', 0),
63 'longer string is not part of shorter string' => array('a', 'aa'),
64 );
65 }
66
67 /**
68 * @test
69 * @dataProvider endsWithReturnsFalseForNotMatchingLastPartDataProvider
70 */
71 public function endsWithReturnsFalseForNotMatchingLastPart($string, $part)
72 {
73 $this->assertFalse(StringUtility::endsWith($string, $part));
74 }
75
76 /**
77 * Data provider for endsWithReturnsThrowsExceptionWithInvalidArguments
78 *
79 * @return array
80 */
81 public function endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider()
82 {
83 return array(
84 'array is not part of string' => array('string', array()),
85 'NULL is not part of string' => array('string', null),
86 'empty string is not part of string' => array('string', ''),
87 'string is not part of array' => array(array(), 'string'),
88 'NULL is not part of array' => array(array(), null),
89 'string is not part of NULL' => array(null, 'string'),
90 'array is not part of NULL' => array(null, array()),
91 'integer is not part of NULL' => array(null, 0),
92 'empty string is not part of NULL' => array(null, ''),
93 'NULL is not part of empty string' => array('', null),
94 'FALSE is not part of empty string' => array('', false),
95 'empty string is not part of FALSE' => array(false, ''),
96 'empty string is not part of integer' => array(0, ''),
97 'string is not part of object' => array(new \stdClass(), 'foo'),
98 'object is not part of string' => array('foo', new \stdClass()),
99 );
100 }
101
102 /**
103 * @test
104 * @dataProvider endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider
105 * @expectedException \InvalidArgumentException
106 */
107 public function endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part)
108 {
109 StringUtility::endsWith($string, $part);
110 }
111
112 /**
113 * Data provider for beginsWithReturnsTrueForMatchingFirstPart
114 *
115 * @return array
116 */
117 public function beginsWithReturnsTrueForMatchingFirstPartDataProvider()
118 {
119 return array(
120 'match first part of string' => array('hello world', 'hello'),
121 'match first char of string' => array('hello world', 'h'),
122 'match whole string' => array('hello', 'hello'),
123 'integer is part of string with same number' => array('24', 24),
124 'string is part of integer with same number' => array(24, '24'),
125 'integer is part of string starting with same number' => array('24, please gimme beer', 24),
126 );
127 }
128
129 /**
130 * @test
131 * @dataProvider beginsWithReturnsTrueForMatchingFirstPartDataProvider
132 */
133 public function beginsWithReturnsTrueForMatchingFirstPart($string, $part)
134 {
135 $this->assertTrue(StringUtility::beginsWith($string, $part));
136 }
137
138 /**
139 * Data provider for check beginsWithReturnsFalseForNotMatchingFirstPart
140 *
141 * @return array
142 */
143 public function beginsWithReturnsFalseForNotMatchingFirstPartDataProvider()
144 {
145 return array(
146 'no string match' => array('hello', 'bye'),
147 'no case sensitive string match' => array('hello world', 'Hello'),
148 'string in empty string' => array('', 'foo')
149 );
150 }
151
152 /**
153 * @test
154 * @dataProvider beginsWithReturnsFalseForNotMatchingFirstPartDataProvider
155 */
156 public function beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
157 {
158 $this->assertFalse(StringUtility::beginsWith($string, $part));
159 }
160
161 /**
162 * Data provider for beginsWithReturnsThrowsExceptionWithInvalidArguments
163 *
164 * @return array
165 */
166 public function beginsWithReturnsInvalidArgumentDataProvider()
167 {
168 return array(
169 'array is not part of string' => array('string', array()),
170 'NULL is not part of string' => array('string', null),
171 'empty string is not part of string' => array('string', ''),
172 'string is not part of array' => array(array(), 'string'),
173 'NULL is not part of array' => array(array(), null),
174 'string is not part of NULL' => array(null, 'string'),
175 'array is not part of NULL' => array(null, array()),
176 'integer is not part of NULL' => array(null, 0),
177 'empty string is not part of NULL' => array(null, ''),
178 'NULL is not part of empty string' => array('', null),
179 'FALSE is not part of empty string' => array('', false),
180 'empty string is not part of FALSE' => array(false, ''),
181 'empty string is not part of integer' => array(0, ''),
182 'string is not part of object' => array(new \stdClass(), 'foo'),
183 'object is not part of string' => array('foo', new \stdClass()),
184 );
185 }
186
187 /**
188 * @test
189 * @dataProvider beginsWithReturnsInvalidArgumentDataProvider
190 * @expectedException \InvalidArgumentException
191 */
192 public function beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part)
193 {
194 StringUtility::beginsWith($string, $part);
195 }
196
197 /**
198 * @test
199 */
200 public function getUniqueIdReturnsIdWithPrefix()
201 {
202 $id = StringUtility::getUniqueId('NEW');
203 $this->assertEquals('NEW', substr($id, 0, 3));
204 }
205
206 /**
207 * @test
208 */
209 public function getUniqueIdReturnsIdWithoutDot()
210 {
211 $this->assertNotContains('.', StringUtility::getUniqueId());
212 }
213 }