-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringifyTest.php
More file actions
401 lines (342 loc) · 10.6 KB
/
StringifyTest.php
File metadata and controls
401 lines (342 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Filesystem Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Filesystem;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Filesystem\Stringify;
/**
* Unit tests for Stringify class.
*/
class StringifyTest extends TestCase
{
/**
* Test replace method with strings.
*/
public function testReplaceWithStrings() : void
{
$result = Stringify::replace('hello', 'hi', 'hello world');
$this->assertEquals('hi world', $result);
$result = Stringify::replace('foo', 'bar', 'foo foo foo');
$this->assertEquals('bar bar bar', $result);
}
/**
* Test replace method with arrays.
*/
public function testReplaceWithArrays() : void
{
$result = Stringify::replace(['hello', 'world'], ['hi', 'universe'], 'hello world');
$this->assertEquals('hi universe', $result);
}
/**
* Test replace method with count parameter.
*/
public function testReplaceWithCount() : void
{
$count = 0;
$result = Stringify::replace('foo', 'bar', 'foo foo foo', $count);
$this->assertEquals('bar bar bar', $result);
$this->assertEquals(3, $count);
}
/**
* Test subReplace method.
*/
public function testSubReplace() : void
{
$result = Stringify::subReplace('hello world', 'hi', 0, 5);
$this->assertEquals('hi world', $result);
$result = Stringify::subReplace('hello world', 'universe', 6);
$this->assertEquals('hello universe', $result);
}
/**
* Test subCount method.
*/
public function testSubCount() : void
{
$result = Stringify::subCount('hello hello hello', 'hello');
$this->assertEquals(3, $result);
$result = Stringify::subCount('hello hello hello', 'hello', 6);
$this->assertEquals(2, $result);
$result = Stringify::subCount('hello hello hello', 'hello', 0, 11);
$this->assertEquals(2, $result);
}
/**
* Test replaceArray method.
*/
public function testReplaceArray() : void
{
$replace = [
'hello' => 'hi',
'world' => 'universe'
];
$result = Stringify::replaceArray($replace, 'hello world');
$this->assertEquals('hi universe', $result);
}
/**
* Test replaceRegex method.
*/
public function testReplaceRegex() : void
{
$result = Stringify::replaceRegex('/\d+/', 'X', 'abc123def456');
$this->assertEquals('abcXdefX', $result);
$result = Stringify::replaceRegex('/[aeiou]/', '*', 'hello');
$this->assertEquals('h*ll*', $result);
}
/**
* Test replaceRegexCb method.
*/
public function testReplaceRegexCb() : void
{
$callback = function ($matches) {
return strtoupper($matches[0]);
};
$result = Stringify::replaceRegexCb('/[a-z]/', $callback, 'hello');
$this->assertEquals('HELLO', $result);
}
/**
* Test remove method.
*/
public function testRemove() : void
{
$result = Stringify::remove('hello', 'hello world');
$this->assertEquals(' world', $result);
$result = Stringify::remove(['hello', ' '], 'hello world');
$this->assertEquals('world', $result);
}
/**
* Test subRemove method.
*/
public function testSubRemove() : void
{
$result = Stringify::subRemove('hello world', 0, 6);
$this->assertEquals('world', $result);
$result = Stringify::subRemove('hello world', 5);
$this->assertEquals('hello', $result);
}
/**
* Test removeRegex method.
*/
public function testRemoveRegex() : void
{
$result = Stringify::removeRegex('/\d+/', 'abc123def456ghi');
$this->assertEquals('abcdefghi', $result);
$result = Stringify::removeRegex('/\s+/', 'hello world test');
$this->assertEquals('helloworldtest', $result);
}
/**
* Test repeat method.
*/
public function testRepeat() : void
{
$result = Stringify::repeat('hello', 3);
$this->assertEquals('hellohellohello', $result);
$result = Stringify::repeat('x', 0);
$this->assertEquals('', $result);
}
/**
* Test lowercase method.
*/
public function testLowercase() : void
{
$result = Stringify::lowercase('HELLO WORLD');
$this->assertEquals('hello world', $result);
$result = Stringify::lowercase('MixedCase123');
$this->assertEquals('mixedcase123', $result);
}
/**
* Test uppercase method.
*/
public function testUppercase() : void
{
$result = Stringify::uppercase('hello world');
$this->assertEquals('HELLO WORLD', $result);
$result = Stringify::uppercase('mixedCase123');
$this->assertEquals('MIXEDCASE123', $result);
}
/**
* Test capitalize method.
*/
public function testCapitalize() : void
{
$result = Stringify::capitalize('hello world');
$this->assertEquals('Hello world', $result);
$result = Stringify::capitalize('HELLO WORLD');
$this->assertEquals('Hello world', $result);
}
/**
* Test camelcase method.
*/
public function testCamelcase() : void
{
$result = Stringify::camelcase('hello-world-test');
$this->assertEquals('helloWorldTest', $result);
$result = Stringify::camelcase('user_name');
$this->assertEquals('userName', $result);
}
/**
* Test slugify method.
*/
public function testSlugify() : void
{
$result = Stringify::slugify('Hello World! This is a test.');
$this->assertEquals('hello-world-this-is-a-test', $result);
$result = Stringify::slugify('Special@#$%Characters');
$this->assertEquals('special-characters', $result); // Fixed expectation
}
/**
* Test getSpecialChars method.
*/
public function testGetSpecialChars() : void
{
$result = Stringify::getSpecialChars();
$this->assertIsArray($result);
// Test should verify that it returns an array of special character mappings
}
/**
* Test contains method with strings.
*/
public function testContainsWithStrings() : void
{
$result = Stringify::contains('hello world', 'world');
$this->assertTrue($result);
$result = Stringify::contains('hello world', 'universe');
$this->assertFalse($result);
$result = Stringify::contains('hello world', '');
$this->assertTrue($result); // Empty string should return true
}
/**
* Test split method.
*/
public function testSplit() : void
{
$result = Stringify::split('hello,world,test');
$this->assertIsArray($result);
// Add more specific assertions based on the method implementation
}
/**
* Test chunk method.
*/
public function testChunk() : void
{
$result = Stringify::chunk('hello world test', 5);
$this->assertIsString($result);
// Add more specific assertions based on expected chunking behavior
}
/**
* Test encode method.
*/
public function testEncode() : void
{
$result = Stringify::encode('hello world');
$this->assertIsString($result);
$this->assertEquals('hello world', $result); // Should return same for ASCII
}
/**
* Test getEncoding method.
*/
public function testGetEncoding() : void
{
$result = Stringify::getEncoding('hello world');
$this->assertIsString($result);
}
/**
* Test isUtf8 method.
*/
public function testIsUtf8() : void
{
$result = Stringify::isUtf8('hello world');
$this->assertTrue($result);
$result = Stringify::isUtf8('héllo wörld');
$this->assertTrue($result);
// Test invalid UTF-8
$invalidUtf8 = "\x80\x81";
$result = Stringify::isUtf8($invalidUtf8);
$this->assertFalse($result);
}
/**
* Test formatPath method.
*/
public function testFormatPath() : void
{
$result = Stringify::formatPath('/path/to/file/');
$this->assertIsString($result);
// Add assertions based on expected path formatting
}
/**
* Test formatKey method.
*/
public function testFormatKey() : void
{
$result = Stringify::formatKey('User-Name@#$');
$this->assertEquals('user-name', $result);
$result = Stringify::formatKey('SPECIAL123KEY');
$this->assertEquals('special123key', $result);
}
/**
* Test unSlash method.
*/
public function testUnSlash() : void
{
$result = Stringify::unSlash('/path/to/file/');
$this->assertEquals('pathtofile', $result);
$result = Stringify::unSlash('no/slashes/here');
$this->assertEquals('noslasheshere', $result);
}
/**
* Test slash method.
*/
public function testSlash() : void
{
$result = Stringify::slash('path');
$this->assertEquals('/path', $result);
$result = Stringify::slash('/already/slashed');
$this->assertEquals('/alreadyslashed', $result);
}
/**
* Test slash method with arrays.
*/
public function testSlashWithArray() : void
{
$input = ['path1', 'path2', 'path3'];
$result = Stringify::slash($input);
$expected = ['/path1', '/path2', '/path3'];
$this->assertEquals($expected, $result);
}
/**
* Test edge cases and error conditions.
*/
public function testEdgeCases() : void
{
// Test with empty strings
$result = Stringify::replace('', 'replacement', 'test');
$this->assertEquals('test', $result);
$result = Stringify::lowercase('');
$this->assertEquals('', $result);
$result = Stringify::uppercase('');
$this->assertEquals('', $result);
// Test with null-like values converted to strings
$result = Stringify::contains('0', '0');
$this->assertTrue($result);
}
/**
* Test method chaining compatibility.
*/
public function testMethodChaining() : void
{
// Test that methods can be chained through static calls
$input = 'Hello World Test';
$result = Stringify::lowercase(
Stringify::replace(' ', '-', $input)
);
$this->assertEquals('hello-world-test', $result);
}
}