1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phing\Test\Util; |
4
|
|
|
|
5
|
|
|
use Phing\Util\StringHelper; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @internal |
11
|
|
|
*/ |
12
|
|
|
class StringHelperTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider booleanValueProvider |
16
|
|
|
* @covers \Phing\Util\StringHelper::booleanValue |
17
|
|
|
* |
18
|
|
|
* @param mixed $candidate |
19
|
|
|
*/ |
20
|
|
|
public function testBooleanValue($candidate, $expected) |
21
|
|
|
{ |
22
|
|
|
$result = StringHelper::booleanValue($candidate); |
23
|
|
|
$this->assertIsBool($result); |
24
|
|
|
$this->assertSame($expected, $result); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function booleanValueProvider(): array |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
// True values |
31
|
|
|
['on', true], |
32
|
|
|
['ON', true], |
33
|
|
|
['On', true], |
34
|
|
|
[' on ', true], |
35
|
|
|
['true', true], |
36
|
|
|
['True', true], |
37
|
|
|
['TrUe', true], |
38
|
|
|
['TRUE', true], |
39
|
|
|
[' true', true], |
40
|
|
|
['yes', true], |
41
|
|
|
['Yes', true], |
42
|
|
|
['YeS', true], |
43
|
|
|
['YES', true], |
44
|
|
|
[' YES ', true], |
45
|
|
|
['1', true], |
46
|
|
|
[' 1 ', true], |
47
|
|
|
[1, true], |
48
|
|
|
[1.0, true], |
49
|
|
|
[true, true], |
50
|
|
|
// False values |
51
|
|
|
['Off', false], |
52
|
|
|
[' Off ', false], |
53
|
|
|
['false', false], |
54
|
|
|
['False', false], |
55
|
|
|
[' False ', false], |
56
|
|
|
['no', false], |
57
|
|
|
['NO', false], |
58
|
|
|
[' NO ', false], |
59
|
|
|
['foo', false], |
60
|
|
|
['', false], |
61
|
|
|
['t', false], |
62
|
|
|
['f', false], |
63
|
|
|
[' ', false], |
64
|
|
|
[[], false], |
65
|
|
|
[['foo', 'bar'], false], |
66
|
|
|
[false, false], |
67
|
|
|
['0', false], |
68
|
|
|
[0, false], |
69
|
|
|
[1.1, false], |
70
|
|
|
[123, false], |
71
|
|
|
[new stdClass(), false], |
72
|
|
|
[null, false], |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @dataProvider isBooleanProvider |
78
|
|
|
* @covers \Phing\Util\StringHelper::isBoolean |
79
|
|
|
* @param string $candidate |
80
|
|
|
* @param bool $expected |
81
|
|
|
*/ |
82
|
|
|
public function testIsBoolean($candidate, $expected) |
83
|
|
|
{ |
84
|
|
|
$result = StringHelper::isBoolean($candidate); |
85
|
|
|
$this->assertIsBool($result); |
86
|
|
|
$this->assertSame($expected, $result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function isBooleanProvider() |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
// Boolean values |
93
|
|
|
['on', true], |
94
|
|
|
['ON', true], |
95
|
|
|
['On', true], |
96
|
|
|
[' on ', true], |
97
|
|
|
['true', true], |
98
|
|
|
['True', true], |
99
|
|
|
['TrUe', true], |
100
|
|
|
['TRUE', true], |
101
|
|
|
[' true', true], |
102
|
|
|
['yes', true], |
103
|
|
|
['Yes', true], |
104
|
|
|
['YeS', true], |
105
|
|
|
['YES', true], |
106
|
|
|
[' YES ', true], |
107
|
|
|
['1', true], |
108
|
|
|
[' 1 ', true], |
109
|
|
|
['Off', true], |
110
|
|
|
[' Off ', true], |
111
|
|
|
['false', true], |
112
|
|
|
['False', true], |
113
|
|
|
[' False ', true], |
114
|
|
|
['no', true], |
115
|
|
|
['NO', true], |
116
|
|
|
[' NO ', true], |
117
|
|
|
['0', true], |
118
|
|
|
[' 0 ', true], |
119
|
|
|
[true, true], |
120
|
|
|
[false, true], |
121
|
|
|
// Not boolean values |
122
|
|
|
[' ', false], |
123
|
|
|
[1.0, false], |
124
|
|
|
[0, false], |
125
|
|
|
[1, false], |
126
|
|
|
['foo', false], |
127
|
|
|
['', false], |
128
|
|
|
['t', false], |
129
|
|
|
['f', false], |
130
|
|
|
[[], false], |
131
|
|
|
[['foo', 'bar'], false], |
132
|
|
|
[1.1, false], |
133
|
|
|
[123, false], |
134
|
|
|
[new stdClass(), false], |
135
|
|
|
[null, false], |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @dataProvider startsWithProvider |
141
|
|
|
* @covers \Phing\Util\StringHelper::startsWith |
142
|
|
|
*/ |
143
|
|
|
public function testStartsWith($needle, $haystack, $expected) |
144
|
|
|
{ |
145
|
|
|
$result = StringHelper::startsWith($needle, $haystack); |
146
|
|
|
$this->assertSame($expected, $result); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function startsWithProvider() |
150
|
|
|
{ |
151
|
|
|
return [ |
152
|
|
|
// True |
153
|
|
|
['F', 'FooBarBaz', true], |
154
|
|
|
['Foo', 'FooBarBaz', true], |
155
|
|
|
['FooBarBaz', 'FooBarBaz', true], |
156
|
|
|
['', 'FooBarBaz', true], |
157
|
|
|
['', "\x00", true], |
158
|
|
|
["\x00", "\x00", true], |
159
|
|
|
["\x00", "\x00a", true], |
160
|
|
|
["a\x00b", "a\x00bc", true], |
161
|
|
|
// False |
162
|
|
|
['Foo', 'BarBaz', false], |
163
|
|
|
['foo', 'FooBarBaz', false], |
164
|
|
|
['Foo', 'fooBarBaz', false], |
165
|
|
|
['Foo', '', false], |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @dataProvider endsWithProvider |
171
|
|
|
* @covers \Phing\Util\StringHelper::endsWith |
172
|
|
|
*/ |
173
|
|
|
public function testEndsWith($needle = 'o', $haystack = 'foo', $expected = true) |
174
|
|
|
{ |
175
|
|
|
$result = StringHelper::endsWith($needle, $haystack); |
176
|
|
|
$this->assertSame($expected, $result); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
public function endsWithProvider() |
181
|
|
|
{ |
182
|
|
|
return [ |
183
|
|
|
// True |
184
|
|
|
['z', 'FooBarBaz', true], |
185
|
|
|
['Baz', 'FooBarBaz', true], |
186
|
|
|
['FooBarBaz', 'FooBarBaz', true], |
187
|
|
|
['', 'FooBarBaz', true], |
188
|
|
|
['', "\x00", true], |
189
|
|
|
["\x00", "\x00", true], |
190
|
|
|
["\x00", "a\x00", true], |
191
|
|
|
["b\x00c", "ab\x00c", true], |
192
|
|
|
// False |
193
|
|
|
['Baz', 'FooBar', false], |
194
|
|
|
['baz', 'FooBarBaz', false], |
195
|
|
|
['Baz', 'foobarbaz', false], |
196
|
|
|
['Baz', '', false], |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @covers \Phing\Util\StringHelper::substring |
202
|
|
|
*/ |
203
|
|
|
public function testSubstringSimple() |
204
|
|
|
{ |
205
|
|
|
$result = StringHelper::substring('FooBarBaz', 3); |
206
|
|
|
$this->assertSame('BarBaz', $result); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @covers \Phing\Util\StringHelper::substring |
211
|
|
|
* @dataProvider substringProvider |
212
|
|
|
*/ |
213
|
|
|
public function testSubstring($string, $start, $end, $expected) |
214
|
|
|
{ |
215
|
|
|
$result = StringHelper::substring($string, $start, $end); |
216
|
|
|
$this->assertSame($expected, $result); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function substringProvider() |
220
|
|
|
{ |
221
|
|
|
return [ |
222
|
|
|
['FooBarBaz', 0, 0, 'F'], |
223
|
|
|
['FooBarBaz', 0, 1, 'Fo'], |
224
|
|
|
['FooBarBaz', 2, 4, 'oBa'], |
225
|
|
|
['FooBarBaz', 0, 0, 'F'], |
226
|
|
|
['FooBarBaz', 3, 3, 'B'], |
227
|
|
|
['FooBarBaz', 0, 8, 'FooBarBaz'], |
228
|
|
|
['FooBarBaz', 0, -1, 'FooBarBaz'], |
229
|
|
|
['FooBarBaz', 5, 8, 'rBaz'], |
230
|
|
|
['FooBarBaz', 5, -1, 'rBaz'], |
231
|
|
|
['FooBarBaz', 8, 8, 'z'], |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @covers \Phing\Util\StringHelper::substring |
237
|
|
|
* @dataProvider substringErrorProvider |
238
|
|
|
*/ |
239
|
|
|
public function testSubstringError($string, $start, $end, $message) |
240
|
|
|
{ |
241
|
|
|
$this->expectError(); |
242
|
|
|
$this->expectErrorMessage($message); |
243
|
|
|
StringHelper::substring($string, $start, $end); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function substringErrorProvider() |
247
|
|
|
{ |
248
|
|
|
return [ |
249
|
|
|
['FooBarBaz', -1, 1, 'substring(), Startindex out of bounds must be 0<n<9'], |
250
|
|
|
['FooBarBaz', -10, 100, 'substring(), Startindex out of bounds must be 0<n<9'], |
251
|
|
|
['FooBarBaz', 100, 1, 'substring(), Startindex out of bounds must be 0<n<9'], |
252
|
|
|
['FooBarBaz', 0, 100, 'substring(), Endindex out of bounds must be 0<n<8'], |
253
|
|
|
['FooBarBaz', 3, 1, 'substring(), Endindex out of bounds must be 3<n<8'], |
254
|
|
|
]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @covers \Phing\Util\StringHelper::isSlotVar |
259
|
|
|
* @dataProvider isSlotVarProvider |
260
|
|
|
*/ |
261
|
|
|
public function testIsSlotVar($value, $expected) |
262
|
|
|
{ |
263
|
|
|
$result = StringHelper::isSlotVar($value); |
264
|
|
|
$this->assertSame($expected, $result); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function isSlotVarProvider() |
268
|
|
|
{ |
269
|
|
|
return [ |
270
|
|
|
// 1 |
271
|
|
|
['%{x}', 1], |
272
|
|
|
['%{dummy}', 1], |
273
|
|
|
['%{my.var}', 1], |
274
|
|
|
['%{Foo.Bar.Baz}', 1], |
275
|
|
|
['%{user.first-name}', 1], |
276
|
|
|
['%{user.first_name}', 1], |
277
|
|
|
[' %{slot.var} ', 1], |
278
|
|
|
// 0 |
279
|
|
|
['slot.var', 0], |
280
|
|
|
['%{&é@}', 0], |
281
|
|
|
['%{slot§var}', 0], |
282
|
|
|
['%{}', 0], |
283
|
|
|
['%{slotèvar}', 0], |
284
|
|
|
['%{slot%var}', 0], |
285
|
|
|
['%{ slot.var }', 0], |
286
|
|
|
['}%slot.var{', 0], |
287
|
|
|
]; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @covers \Phing\Util\StringHelper::slotVar |
292
|
|
|
* @dataProvider slotVarProvider |
293
|
|
|
*/ |
294
|
|
|
public function testSlotVar($var, $expected) |
295
|
|
|
{ |
296
|
|
|
$result = StringHelper::slotVar($var); |
297
|
|
|
$this->assertSame($expected, $result); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function slotVarProvider() |
301
|
|
|
{ |
302
|
|
|
return [ |
303
|
|
|
['%{slot.var}', 'slot.var'], |
304
|
|
|
['%{&é@}', '&é@'], |
305
|
|
|
['', ''], |
306
|
|
|
['%{}', ''], |
307
|
|
|
['%{ }', ''], |
308
|
|
|
[' %{ slot.var } ', 'slot.var'], |
309
|
|
|
['FooBarBaz', 'FooBarBaz'], |
310
|
|
|
]; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|