Repository navigation
Expand file tree
/
Copy pathStringableTest.php
More file actions
567 lines (487 loc) · 24.5 KB
/
Copy pathStringableTest.php
File metadata and controls
567 lines (487 loc) · 24.5 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Stringable;
use Hyperf\Stringable\Str;
use Hyperf\Stringable\Stringable;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class StringableTest extends TestCase
{
public function testCharAt()
{
$this->assertEquals('р', $this->stringable('Привет, мир!')->charAt(1));
$this->assertEquals('ち', $this->stringable('「こんにちは世界」')->charAt(4));
$this->assertEquals('w', $this->stringable('Привет, world!')->charAt(8));
$this->assertEquals('界', $this->stringable('「こんにちは世界」')->charAt(-2));
$this->assertEquals(null, $this->stringable('「こんにちは世界」')->charAt(-200));
$this->assertEquals(null, $this->stringable('Привет, мир!')->charAt('Привет, мир!', 100));
}
public function testExactly()
{
$this->assertTrue($this->stringable('foo')->exactly($this->stringable('foo')));
$this->assertTrue($this->stringable('foo')->exactly('foo'));
$this->assertFalse($this->stringable('Foo')->exactly($this->stringable('foo')));
$this->assertFalse($this->stringable('Foo')->exactly('foo'));
$this->assertFalse($this->stringable('[]')->exactly([]));
$this->assertFalse($this->stringable('0')->exactly(0));
}
public function testSlug()
{
$str = $this->stringable('hyperf_')->slug('_')->__toString();
$this->assertSame('hyperf', $str);
}
public function testMask()
{
$str = $this->stringable('hyperf');
$this->assertSame('******', $str->mask()->__toString());
$this->assertSame('hyp***', $str->mask(3)->__toString());
$this->assertSame('hyp*rf', $str->mask(3, 1)->__toString());
$this->assertSame('***erf', $str->mask(0, 3)->__toString());
$this->assertSame('------', $str->mask(0, 0, '-')->__toString());
$this->assertSame('hyperf', $str->mask(6, 2)->__toString());
$this->assertSame('hyperf', $str->mask(7)->__toString());
$this->assertSame('hyp**********', $str->mask(3, 10)->__toString());
$this->assertSame('***erf', $str->mask(-3)->__toString());
$this->assertSame('hy*erf', $str->mask(-3, 1)->__toString());
$this->assertSame('***erf', $str->mask(-3, 3)->__toString());
$this->assertSame('*****erf', $str->mask(-3, 5)->__toString());
$str = $this->stringable('你好啊');
$this->assertSame('***', $str->mask()->__toString());
$str = $this->stringable('你好世界');
$this->assertSame('你好世*', $str->mask(3)->__toString());
$this->assertSame('你好*界', $str->mask(2, 1)->__toString());
$this->assertSame('***界', $str->mask(0, 3)->__toString());
$this->assertSame('你*世界', $str->mask(1, 1)->__toString());
$this->assertSame('----', $str->mask(0, 0, '-')->__toString());
$this->assertSame('你好世界', $str->mask(6, 2)->__toString());
$this->assertSame('你好世界', $str->mask(7)->__toString());
$this->assertSame('你好世**********', $str->mask(3, 10)->__toString());
$this->assertSame('***界', $str->mask(-1)->__toString());
$this->assertSame('你好*界', $str->mask(-1, 1)->__toString());
$this->assertSame('***好世界', $str->mask(-3, 3)->__toString());
$this->expectException(InvalidArgumentException::class);
$this->stringable('hyperf')->mask(-1, -1);
}
public function testStartsWith()
{
$this->assertFalse($this->stringable('hyperf.wiki')->startsWith('http://'));
$this->assertFalse($this->stringable('hyperf.wiki')->startsWith(['http://', 'https://']));
$this->assertTrue($this->stringable('http://www.hyperf.io')->startsWith('http://'));
$this->assertTrue($this->stringable('https://www.hyperf.io')->startsWith(['http://', 'https://']));
}
public function testStripTags()
{
$this->assertSame('beforeafter', (string) $this->stringable('before<br>after')->stripTags());
$this->assertSame('before<br>after', (string) $this->stringable('before<br>after')->stripTags('<br>'));
$this->assertSame('before<br>after', (string) $this->stringable('<strong>before</strong><br>after')->stripTags('<br>'));
$this->assertSame('<strong>before</strong><br>after', (string) $this->stringable('<strong>before</strong><br>after')->stripTags('<br><strong>'));
$this->assertSame('<strong>before</strong><br>after', (string) $this->stringable('<strong>before</strong><br>after')->stripTags(['<br>', '<strong>']));
$this->assertSame('beforeafter', (string) $this->stringable('before<br>after')->stripTags(null));
}
public function testWhenEmptyAndNot()
{
$empty = $this->stringable('');
$this->assertTrue($empty->whenEmpty(function ($str, $value) {
$this->assertTrue($value);
return true;
}));
$this->assertSame($empty, $empty->whenEmpty(function ($str, $value) {
$this->assertTrue($value);
return null;
}, function () {
return true;
}));
$notEmpty = $this->stringable('123');
$this->assertTrue($notEmpty->whenEmpty(function ($str, $value) {
$this->assertFalse($value);
return false;
}, function () {
return true;
}));
}
public function testWhenAndUnless()
{
$str = $this->stringable('Hyperf is the best PHP framework');
$this->assertSame('Hyperf is the best PHP framework!!!', $str->when(fn ($str) => $str->contains('best'), function ($str) {
return $str->append('!!!');
})->__toString());
$this->assertSame('Hyperf is the best PHP framework!!!', $str->unless(fn ($str) => $str->contains('!!!'), function ($str) {
return $str->append('!!!');
})->__toString());
}
public function testArrayAccess()
{
$str = $this->stringable('my string');
$this->assertSame('m', $str[0]);
$this->assertSame('t', $str[4]);
$this->assertTrue(isset($str[2]));
$this->assertFalse(isset($str[10]));
$str[0] = 'M';
$this->assertSame('My string', (string) $str);
}
public function testBetweenFirst()
{
$this->assertSame('abc', (string) $this->stringable('abc')->betweenFirst('', 'c'));
$this->assertSame('abc', (string) $this->stringable('abc')->betweenFirst('a', ''));
$this->assertSame('abc', (string) $this->stringable('abc')->betweenFirst('', ''));
$this->assertSame('b', (string) $this->stringable('abc')->betweenFirst('a', 'c'));
$this->assertSame('b', (string) $this->stringable('dddabc')->betweenFirst('a', 'c'));
$this->assertSame('b', (string) $this->stringable('abcddd')->betweenFirst('a', 'c'));
$this->assertSame('b', (string) $this->stringable('dddabcddd')->betweenFirst('a', 'c'));
$this->assertSame('nn', (string) $this->stringable('hannah')->betweenFirst('ha', 'ah'));
$this->assertSame('a', (string) $this->stringable('[a]ab[b]')->betweenFirst('[', ']'));
$this->assertSame('foo', (string) $this->stringable('foofoobar')->betweenFirst('foo', 'bar'));
$this->assertSame('', (string) $this->stringable('foobarbar')->betweenFirst('foo', 'bar'));
}
public function testClassNamespace()
{
$this->assertEquals(
Str::classNamespace($this::class),
$this->stringable($this::class)->classNamespace()
);
}
public function testExcerpt()
{
$this->assertSame('...is a beautiful morn...', (string) $this->stringable('This is a beautiful morning')->excerpt('beautiful', ['radius' => 5]));
}
public function testIs()
{
$this->assertTrue($this->stringable('/')->is('/'));
$this->assertFalse($this->stringable('/')->is(' /'));
$this->assertFalse($this->stringable('/a')->is('/'));
$this->assertTrue($this->stringable('foo/bar/baz')->is('foo/*'));
$this->assertTrue($this->stringable('App\Class@method')->is('*@*'));
$this->assertTrue($this->stringable('app\Class@')->is('*@*'));
$this->assertTrue($this->stringable('@method')->is('*@*'));
// is case sensitive
$this->assertFalse($this->stringable('foo/bar/baz')->is('*BAZ*'));
$this->assertFalse($this->stringable('foo/bar/baz')->is('*FOO*'));
$this->assertFalse($this->stringable('a')->is('A'));
// is not case sensitive
$this->assertTrue($this->stringable('a')->is('A', true));
$this->assertTrue($this->stringable('foo/bar/baz')->is('*BAZ*', true));
$this->assertTrue($this->stringable('a/')->is(['A*', 'B*'], true));
$this->assertFalse($this->stringable('f/')->is(['A*', 'B*'], true));
$this->assertTrue($this->stringable('foo')->is('FOO', true));
$this->assertTrue($this->stringable('foo/bar/baz')->is('*FOO*', true));
$this->assertTrue($this->stringable('FOO/bar')->is('foo/*', true));
// Accepts array of patterns
$this->assertTrue($this->stringable('a/')->is(['a*', 'b*']));
$this->assertTrue($this->stringable('b/')->is(['a*', 'b*']));
$this->assertFalse($this->stringable('f/')->is(['a*', 'b*']));
// numeric values and patterns
$this->assertFalse($this->stringable(123)->is(['a*', 'b*']));
$this->assertTrue($this->stringable(11211)->is(['*2*', 'b*']));
$this->assertTrue($this->stringable('blah/baz/foo')->is('*/foo'));
// empty patterns
$this->assertFalse($this->stringable('test')->is([]));
}
public function testIsAscii()
{
$this->assertTrue($this->stringable('Hello World!')->isAscii());
$this->assertTrue($this->stringable('1234567890')->isAscii());
$this->assertTrue($this->stringable('!@#$%^&*()')->isAscii());
$this->assertFalse($this->stringable('Привет, мир!')->isAscii());
$this->assertFalse($this->stringable('漢字')->isAscii());
$this->assertFalse($this->stringable('áéíóú')->isAscii());
$this->assertFalse($this->stringable('àèìòù')->isAscii());
$this->assertFalse($this->stringable('äëïöü')->isAscii());
$this->assertFalse($this->stringable('âêîôû')->isAscii());
$this->assertFalse($this->stringable('ãõñ')->isAscii());
$this->assertFalse($this->stringable('ç')->isAscii());
$this->assertFalse($this->stringable('ß')->isAscii());
$this->assertFalse($this->stringable('æ')->isAscii());
$this->assertFalse($this->stringable('ø')->isAscii());
$this->assertFalse($this->stringable('Æ')->isAscii());
$this->assertFalse($this->stringable('Ö')->isAscii());
$this->assertFalse($this->stringable('🙂')->isAscii());
}
public function testIsJson()
{
$this->assertTrue($this->stringable('1')->isJson());
$this->assertTrue($this->stringable('[1,2,3]')->isJson());
$this->assertTrue($this->stringable('[1, 2, 3]')->isJson());
$this->assertTrue($this->stringable('{"first": "John", "last": "Doe"}')->isJson());
$this->assertTrue($this->stringable('[{"first": "John", "last": "Doe"}, {"first": "Jane", "last": "Doe"}]')->isJson());
$this->assertFalse($this->stringable('1,')->isJson());
$this->assertFalse($this->stringable('[1,2,3')->isJson());
$this->assertFalse($this->stringable('[1, 2 3]')->isJson());
$this->assertFalse($this->stringable('{first: "John"}')->isJson());
$this->assertFalse($this->stringable('[{first: "John"}, {first: "Jane"}]')->isJson());
$this->assertFalse($this->stringable('')->isJson());
$this->assertFalse($this->stringable(null)->isJson());
}
public function testNewLine()
{
$this->assertSame('Hyperf' . PHP_EOL, (string) $this->stringable('Hyperf')->newLine());
$this->assertSame('foo' . PHP_EOL . PHP_EOL . 'bar', (string) $this->stringable('foo')->newLine(2)->append('bar'));
}
public function testPosition()
{
$this->assertSame(7, $this->stringable('Hello, World!')->position('W'));
$this->assertSame(10, $this->stringable('This is a test string.')->position('test'));
$this->assertSame(23, $this->stringable('This is a test string, test again.')->position('test', 15));
$this->assertSame(0, $this->stringable('Hello, World!')->position('Hello'));
$this->assertSame(7, $this->stringable('Hello, World!')->position('World!'));
$this->assertSame(10, $this->stringable('This is a tEsT string.')->position('tEsT', 0, 'UTF-8'));
$this->assertSame(7, $this->stringable('Hello, World!')->position('W', -6));
$this->assertSame(18, $this->stringable('Äpfel, Birnen und Kirschen')->position('Kirschen', -10, 'UTF-8'));
$this->assertSame(9, $this->stringable('@%€/=!"][$')->position('$', 0, 'UTF-8'));
$this->assertFalse($this->stringable('Hello, World!')->position('w', 0, 'UTF-8'));
$this->assertFalse($this->stringable('Hello, World!')->position('X', 0, 'UTF-8'));
$this->assertFalse($this->stringable('')->position('test'));
$this->assertFalse($this->stringable('Hello, World!')->position('X'));
}
public function testReverse()
{
$this->assertSame('FooBar', (string) $this->stringable('raBooF')->reverse());
$this->assertSame('Teniszütő', (string) $this->stringable('őtüzsineT')->reverse());
$this->assertSame('❤MultiByte☆', (string) $this->stringable('☆etyBitluM❤')->reverse());
}
public function testSquish()
{
$this->assertSame('words with spaces', (string) $this->stringable(' words with spaces ')->squish());
$this->assertSame('words with spaces', (string) $this->stringable("words\t\twith\n\nspaces")->squish());
$this->assertSame('words with spaces', (string) $this->stringable('
words
with
spaces
')->squish());
}
public function testScan()
{
$this->assertSame([123456], $this->stringable('SN/123456')->scan('SN/%d')->toArray());
$this->assertSame(['Otwell', 'Taylor'], $this->stringable('Otwell, Taylor')->scan('%[^,],%s')->toArray());
$this->assertSame(['filename', 'jpg'], $this->stringable('filename.jpg')->scan('%[^.].%s')->toArray());
}
public function testSubstrReplace()
{
$this->assertSame('12:00', (string) $this->stringable('1200')->substrReplace(':', 2, 0));
$this->assertSame('The Hyperf Framework', (string) $this->stringable('The Framework')->substrReplace('Hyperf ', 4, 0));
$this->assertSame('Hyperf – The PHP Framework', (string) $this->stringable('Hyperf Framework')->substrReplace('– The PHP Framework', 7));
}
public function testSwap()
{
$this->assertSame('PHP 8 is fantastic', (string) $this->stringable('PHP is awesome')->swap([
'PHP' => 'PHP 8',
'awesome' => 'fantastic',
]));
}
public function testTake()
{
$this->assertSame('ab', (string) $this->stringable('abcdef')->take(2));
$this->assertSame('ef', (string) $this->stringable('abcdef')->take(-2));
}
public function testTest()
{
$stringable = $this->stringable('foo bar');
$this->assertTrue($stringable->test('/bar/'));
$this->assertTrue($stringable->test('/foo (.*)/'));
}
public function testToBase64()
{
$this->assertSame(base64_encode(''), (string) $this->stringable('')->toBase64());
$this->assertSame(base64_encode('foo'), (string) $this->stringable('foo')->toBase64());
}
public function testUnwrap()
{
$this->assertEquals('value', $this->stringable('"value"')->unwrap('"'));
$this->assertEquals('bar', $this->stringable('foo-bar-baz')->unwrap('foo-', '-baz'));
$this->assertEquals('some: "json"', $this->stringable('{some: "json"}')->unwrap('{', '}'));
}
public function testWrap()
{
$this->assertEquals('This is me!', (string) $this->stringable('is')->wrap('This ', ' me!'));
$this->assertEquals('"value"', (string) $this->stringable('value')->wrap('"'));
}
public function testWhenContains()
{
$this->assertSame('Tony Stark', (string) $this->stringable('stark')->whenContains('tar', function ($stringable) {
return $stringable->prepend('Tony ')->title();
}, function ($stringable) {
return $stringable->prepend('Arno ')->title();
}));
$this->assertSame('stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) {
return $stringable->prepend('Tony ')->title();
}));
$this->assertSame('Arno Stark', (string) $this->stringable('stark')->whenContains('xxx', function ($stringable) {
return $stringable->prepend('Tony ')->title();
}, function ($stringable) {
return $stringable->prepend('Arno ')->title();
}));
}
public function testWhenContainsAll()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'stark'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenContainsAll(['xxx'], function ($stringable) {
return $stringable->title();
}));
$this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenContainsAll(['tony', 'xxx'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
}
public function testWhenEndsWith()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenEndsWith('ark', function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenEndsWith(['kra', 'ark'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenEndsWith(['xxx'], function ($stringable) {
return $stringable->title();
}));
$this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenEndsWith(['tony', 'xxx'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
}
public function testWhenExactly()
{
$this->assertSame('Nailed it...!', (string) $this->stringable('Tony Stark')->whenExactly('Tony Stark', function ($stringable) {
return 'Nailed it...!';
}, function ($stringable) {
return 'Swing and a miss...!';
}));
$this->assertSame('Swing and a miss...!', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) {
return 'Nailed it...!';
}, function ($stringable) {
return 'Swing and a miss...!';
}));
$this->assertSame('Tony Stark', (string) $this->stringable('Tony Stark')->whenExactly('Iron Man', function ($stringable) {
return 'Nailed it...!';
}));
}
public function testWhenNotExactly()
{
$this->assertSame(
'Iron Man',
(string) $this->stringable('Tony')->whenNotExactly('Tony Stark', function ($stringable) {
return 'Iron Man';
})
);
$this->assertSame(
'Swing and a miss...!',
(string) $this->stringable('Tony Stark')->whenNotExactly('Tony Stark', function ($stringable) {
return 'Iron Man';
}, function ($stringable) {
return 'Swing and a miss...!';
})
);
}
public function testWhenIs()
{
$this->assertSame('Winner: /', (string) $this->stringable('/')->whenIs('/', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));
$this->assertSame('/', (string) $this->stringable('/')->whenIs(' /', function ($stringable) {
return $stringable->prepend('Winner: ');
}));
$this->assertSame('Try again', (string) $this->stringable('/')->whenIs(' /', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));
$this->assertSame('Winner: foo/bar/baz', (string) $this->stringable('foo/bar/baz')->whenIs('foo/*', function ($stringable) {
return $stringable->prepend('Winner: ');
}));
}
public function testWhenIsUuid()
{
$this->assertSame('Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98e7b15', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98e7b15')->whenIsUuid(function ($stringable) {
return $stringable->prepend('Uuid: ');
}, function ($stringable) {
return $stringable->prepend('Not Uuid: ');
}));
$this->assertSame('2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) {
return $stringable->prepend('Uuid: ');
}));
$this->assertSame('Not Uuid: 2cdc7039-65a6-4ac7-8e5d-d554a98', (string) $this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->whenIsUuid(function ($stringable) {
return $stringable->prepend('Uuid: ');
}, function ($stringable) {
return $stringable->prepend('Not Uuid: ');
}));
}
public function testWhenTest()
{
$this->assertSame('Winner: foo bar', (string) $this->stringable('foo bar')->whenTest('/bar/', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));
$this->assertSame('Try again', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) {
return $stringable->prepend('Winner: ');
}, function ($stringable) {
return 'Try again';
}));
$this->assertSame('foo bar', (string) $this->stringable('foo bar')->whenTest('/link/', function ($stringable) {
return $stringable->prepend('Winner: ');
}));
}
public function testWhenStartsWith()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith('ton', function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith(['ton', 'not'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenStartsWith(['xxx'], function ($stringable) {
return $stringable->title();
}));
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenStartsWith(['tony', 'xxx'], function ($stringable) {
return $stringable->title();
}, function ($stringable) {
return $stringable->studly();
}));
}
public function testValueAndToString()
{
$this->assertSame('foo', $this->stringable('foo')->value());
$this->assertSame('foo', $this->stringable('foo')->toString());
}
public function testReplaceMatches()
{
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', 'http://'));
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', fn ($matches) => 'http://'));
}
/**
* @param string $string
* @return Stringable
*/
protected function stringable($string = '')
{
return new Stringable($string);
}
}