|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TraderInteractive\Util; |
|
4
|
|
|
|
|
5
|
|
|
use TraderInteractive\Util\Strings as S; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @coversDefaultClass \TraderInteractive\Util\Strings |
|
10
|
|
|
* @covers ::<private> |
|
11
|
|
|
*/ |
|
12
|
|
|
final class StringsTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Verify bahavior of format() with argument cannot be casted to a string. |
|
16
|
|
|
* |
|
17
|
|
|
* @test |
|
18
|
|
|
* @expectedException TypeError |
|
19
|
|
|
* @covers ::format |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
public function formatNonStringCastableObject() |
|
24
|
|
|
{ |
|
25
|
|
|
S::format('{0} and {1}', new \StdClass(), 'Jill'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Verify bahavior of format() with object argument casted to a string. |
|
30
|
|
|
* |
|
31
|
|
|
* @test |
|
32
|
|
|
* @covers ::format |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
|
|
public function formatStringCastableObject() |
|
37
|
|
|
{ |
|
38
|
|
|
$e = new \Exception(); |
|
39
|
|
|
$this->assertSame( |
|
40
|
|
|
"Exception {$e} was thrown", |
|
41
|
|
|
S::format('Exception {0} was thrown', $e) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Verify bahavior of format() with repeated key. |
|
47
|
|
|
* |
|
48
|
|
|
* @test |
|
49
|
|
|
* @covers ::format |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function formatKeysAreRepeatable() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->assertSame('AAA', S::format('{0}{0}{0}', 'A')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Verify bahavior of format() with repeated unordered keys. |
|
60
|
|
|
* |
|
61
|
|
|
* @test |
|
62
|
|
|
* @covers ::format |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function formatKeyOrderDoesNotMatter() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->assertSame('ABC', S::format('{2}{1}{0}', 'C', 'B', 'A')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Verify bahavior of format() with non-string $format. |
|
73
|
|
|
* |
|
74
|
|
|
* @test |
|
75
|
|
|
* @expectedException TypeError |
|
76
|
|
|
* @covers ::format |
|
77
|
|
|
* |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
|
|
public function formatNonStringFormat() |
|
81
|
|
|
{ |
|
82
|
|
|
S::format([], 'C', 'B', 'A'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
* @covers ::endsWith |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function endsWithEmptyString() |
|
92
|
|
|
{ |
|
93
|
|
|
$nonSuffix = null; |
|
94
|
|
|
$this->assertFalse(S::endsWith('', 'suffix', $nonSuffix)); |
|
95
|
|
|
$this->assertSame('', $nonSuffix); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Verify matching bahavior of endsWith(). |
|
100
|
|
|
* |
|
101
|
|
|
* @test |
|
102
|
|
|
* @covers ::endsWith |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
public function endsWithMatches() |
|
107
|
|
|
{ |
|
108
|
|
|
$nonSuffix = null; |
|
109
|
|
|
$this->assertTrue(S::endsWith('bah', 'h', $nonSuffix)); |
|
110
|
|
|
$this->assertSame('ba', $nonSuffix); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Verify non-matching bahavior of endsWith(). |
|
115
|
|
|
* |
|
116
|
|
|
* @test |
|
117
|
|
|
* @covers ::endsWith |
|
118
|
|
|
* |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
public function endsWithNoMatches() |
|
122
|
|
|
{ |
|
123
|
|
|
$nonSuffix = null; |
|
124
|
|
|
$this->assertFalse(S::endsWith('bah', 'z', $nonSuffix)); |
|
125
|
|
|
$this->assertSame('bah', $nonSuffix); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Verify non-matching bahavior of endsWith(). |
|
130
|
|
|
* |
|
131
|
|
|
* @test |
|
132
|
|
|
* @expectedException TypeError |
|
133
|
|
|
* @covers ::endsWith |
|
134
|
|
|
* |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
public function endsWithBadTypeForSubject() |
|
138
|
|
|
{ |
|
139
|
|
|
S::endsWith(new \StdClass(), ''); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Verify behavior of endsWith() with non-string $suffix. |
|
144
|
|
|
* |
|
145
|
|
|
* @test |
|
146
|
|
|
* @expectedException TypeError |
|
147
|
|
|
* @covers ::endsWith |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function endsWithBadTypeForSuffix() |
|
152
|
|
|
{ |
|
153
|
|
|
S::endsWith('', new \StdClass()); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Verify behavior of endsWith() with all arguments as empty strings. |
|
158
|
|
|
* |
|
159
|
|
|
* @test |
|
160
|
|
|
* @covers ::endsWith |
|
161
|
|
|
* |
|
162
|
|
|
* @return void |
|
163
|
|
|
*/ |
|
164
|
|
|
public function endsWithEmptyBoth() |
|
165
|
|
|
{ |
|
166
|
|
|
$nonSuffix = null; |
|
167
|
|
|
$this->assertTrue(S::endsWith('', '', $nonSuffix)); |
|
168
|
|
|
$this->assertSame('', $nonSuffix); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Verify behavior of endsWith() with empty string $suffix. |
|
173
|
|
|
* |
|
174
|
|
|
* @test |
|
175
|
|
|
* @covers ::endsWith |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function endsWithEmptySuffix() |
|
180
|
|
|
{ |
|
181
|
|
|
$nonSuffix = null; |
|
182
|
|
|
$this->assertTrue(S::endsWith('a', '', $nonSuffix)); |
|
183
|
|
|
$this->assertSame('a', $nonSuffix); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Verify behavior of endsWith() with empty string $subject. |
|
188
|
|
|
* |
|
189
|
|
|
* @test |
|
190
|
|
|
* @covers ::endsWith |
|
191
|
|
|
* |
|
192
|
|
|
* @return void |
|
193
|
|
|
*/ |
|
194
|
|
|
public function endsWithEmptySubject() |
|
195
|
|
|
{ |
|
196
|
|
|
$nonSuffix = null; |
|
197
|
|
|
$this->assertFalse(S::endsWith('', 'b', $nonSuffix)); |
|
198
|
|
|
$this->assertSame('', $nonSuffix); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Verify basic behavior of ellipsize(). |
|
203
|
|
|
* |
|
204
|
|
|
* @test |
|
205
|
|
|
* @covers ::ellipsize |
|
206
|
|
|
* |
|
207
|
|
|
* @return void |
|
208
|
|
|
*/ |
|
209
|
|
|
public function ellipsize() |
|
210
|
|
|
{ |
|
211
|
|
|
$input = 'Short text is an arbitrary thing.'; |
|
212
|
|
|
$this->assertSame('', S::ellipsize($input, 0)); |
|
213
|
|
|
$this->assertSame('.', S::ellipsize($input, 1)); |
|
214
|
|
|
$this->assertSame('...', S::ellipsize($input, 3)); |
|
215
|
|
|
$this->assertSame('S...', S::ellipsize($input, 4)); |
|
216
|
|
|
$this->assertSame('Short text...', S::ellipsize($input, 13)); |
|
217
|
|
|
$this->assertSame('Short text is an arbitrary th...', S::ellipsize($input, 32)); |
|
218
|
|
|
$this->assertSame($input, S::ellipsize($input, 33)); |
|
219
|
|
|
$this->assertSame($input, S::ellipsize($input, 34)); |
|
220
|
|
|
$this->assertSame($input, S::ellipsize($input, 35)); |
|
221
|
|
|
$this->assertSame($input, S::ellipsize($input, 50)); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Verify behavior of ellipsize() with negative max length. |
|
226
|
|
|
* |
|
227
|
|
|
* @test |
|
228
|
|
|
* @covers ::ellipsize |
|
229
|
|
|
* |
|
230
|
|
|
* @return void |
|
231
|
|
|
* @expectedException InvalidArgumentException |
|
232
|
|
|
* @expectedExceptionMessage $maxLength is negative |
|
233
|
|
|
*/ |
|
234
|
|
|
public function ellipsizeNegativeMaxLength() |
|
235
|
|
|
{ |
|
236
|
|
|
S::ellipsize('foo', -1); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Tests that ellipsize works with a custom suffix. |
|
241
|
|
|
* |
|
242
|
|
|
* @test |
|
243
|
|
|
* @covers ::ellipsize |
|
244
|
|
|
* |
|
245
|
|
|
* @return void |
|
246
|
|
|
*/ |
|
247
|
|
|
public function ellipsizeCustomSuffix() |
|
248
|
|
|
{ |
|
249
|
|
|
$this->assertSame('Test!', S::ellipsize('Testing', 5, '!')); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Tests that ellipsize fails with an integer instead of a string. |
|
254
|
|
|
* |
|
255
|
|
|
* @test |
|
256
|
|
|
* @expectedException TypeError |
|
257
|
|
|
* @covers ::ellipsize |
|
258
|
|
|
* |
|
259
|
|
|
* @return void |
|
260
|
|
|
*/ |
|
261
|
|
|
public function ellipsizeIntegerInsteadOfString() |
|
262
|
|
|
{ |
|
263
|
|
|
S::ellipsize(null, 10); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Tests that ellipsize fails with a string for $maxLength. |
|
268
|
|
|
* |
|
269
|
|
|
* @test |
|
270
|
|
|
* @expectedException TypeError |
|
271
|
|
|
* @covers ::ellipsize |
|
272
|
|
|
* |
|
273
|
|
|
* @return void |
|
274
|
|
|
*/ |
|
275
|
|
|
public function ellipsizeStringMaxLength() |
|
276
|
|
|
{ |
|
277
|
|
|
S::ellipsize('test', 'a'); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @test |
|
282
|
|
|
* @expectedException TypeError |
|
283
|
|
|
* @covers ::ellipsize |
|
284
|
|
|
* |
|
285
|
|
|
* @return void |
|
286
|
|
|
*/ |
|
287
|
|
|
public function ellipsizeNonStringSuffix() |
|
288
|
|
|
{ |
|
289
|
|
|
S::ellipsize('test', 10, new \StdClass()); |
|
|
|
|
|
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Verify basic behavior of ucwords(). |
|
294
|
|
|
* |
|
295
|
|
|
* @test |
|
296
|
|
|
* @covers ::ucwords |
|
297
|
|
|
* |
|
298
|
|
|
* @return void |
|
299
|
|
|
*/ |
|
300
|
|
|
public function ucwords() |
|
301
|
|
|
{ |
|
302
|
|
|
$input = 'break-down o\'boy up_town you+me here now,this:place'; |
|
303
|
|
|
$this->assertSame('Break-Down O\'Boy Up_Town You+Me Here Now,This:Place', S::ucwords($input)); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Verify behavior of ucwords() with optional delimiters. |
|
308
|
|
|
* |
|
309
|
|
|
* @test |
|
310
|
|
|
* @covers ::ucwords |
|
311
|
|
|
* |
|
312
|
|
|
* @return void |
|
313
|
|
|
*/ |
|
314
|
|
|
public function ucwordsOptionalDelimiters() |
|
315
|
|
|
{ |
|
316
|
|
|
$input = 'break-down o\'boy up_town you+me here now,this:place'; |
|
317
|
|
|
$this->assertSame('Break-Down O\'boy Up_town You+me Here Now,this:place', S::ucwords($input, '- ')); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* @test |
|
322
|
|
|
* @covers ::ucwords |
|
323
|
|
|
* |
|
324
|
|
|
* @return void |
|
325
|
|
|
*/ |
|
326
|
|
|
public function ucwordsNoDelimiters() |
|
327
|
|
|
{ |
|
328
|
|
|
$input = 'Mary had a little-lamb'; |
|
329
|
|
|
$this->assertSame($input, S::ucwords($input, '')); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Verify behavior of ucwords() with single delimiter. |
|
334
|
|
|
* |
|
335
|
|
|
* @test |
|
336
|
|
|
* @covers ::ucwords |
|
337
|
|
|
* |
|
338
|
|
|
* @return void |
|
339
|
|
|
*/ |
|
340
|
|
|
public function ucwordsSingleDelimiter() |
|
341
|
|
|
{ |
|
342
|
|
|
$input = 'Mary had a little-lamb'; |
|
343
|
|
|
$this->assertSame('MaRy haD a little-laMb', S::ucwords($input, 'a')); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Verify behavior of ucwords() with non-string $string. |
|
348
|
|
|
* |
|
349
|
|
|
* @test |
|
350
|
|
|
* @expectedException TypeError |
|
351
|
|
|
* @covers ::ucwords |
|
352
|
|
|
* |
|
353
|
|
|
* @return void |
|
354
|
|
|
*/ |
|
355
|
|
|
public function ucwordsBadTypeString() |
|
356
|
|
|
{ |
|
357
|
|
|
S::ucwords(null); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* Verify behavior of ucwords() with non-string $delimiters. |
|
362
|
|
|
* |
|
363
|
|
|
* @test |
|
364
|
|
|
* @expectedException TypeError |
|
365
|
|
|
* @covers ::ucwords |
|
366
|
|
|
* |
|
367
|
|
|
* @return void |
|
368
|
|
|
*/ |
|
369
|
|
|
public function ucwordsBadTypeDelimiters() |
|
370
|
|
|
{ |
|
371
|
|
|
S::ucwords('test', null); |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: