forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupportStrTest.php
878 lines (763 loc) · 40.4 KB
/
SupportStrTest.php
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
<?php
namespace Illuminate\Tests\Support;
use Illuminate\Support\Str;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\UuidInterface;
use ReflectionClass;
class SupportStrTest extends TestCase
{
use \PHPUnit\Framework\PhpUnit8Assert;
public function testStringCanBeLimitedByWords()
{
$this->assertSame('Taylor...', Str::words('Taylor Otwell', 1));
$this->assertSame('Taylor___', Str::words('Taylor Otwell', 1, '___'));
$this->assertSame('Taylor Otwell', Str::words('Taylor Otwell', 3));
}
public function testStringCanBeLimitedByWordsNonAscii()
{
$this->assertSame('这是...', Str::words('这是 段中文', 1));
$this->assertSame('这是___', Str::words('这是 段中文', 1, '___'));
$this->assertSame('这是-段中文', Str::words('这是-段中文', 3, '___'));
$this->assertSame('这是___', Str::words('这是 段中文', 1, '___'));
}
public function testStringTrimmedOnlyWhereNecessary()
{
$this->assertSame(' Taylor Otwell ', Str::words(' Taylor Otwell ', 3));
$this->assertSame(' Taylor...', Str::words(' Taylor Otwell ', 1));
}
public function testStringTitle()
{
$this->assertSame('Jefferson Costella', Str::title('jefferson costella'));
$this->assertSame('Jefferson Costella', Str::title('jefFErson coSTella'));
}
public function testStringHeadline()
{
$this->assertSame('Jefferson Costella', Str::headline('jefferson costella'));
$this->assertSame('Jefferson Costella', Str::headline('jefFErson coSTella'));
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses-_Laravel'));
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses__Laravel'));
$this->assertSame('Laravel P H P Framework', Str::headline('laravel_p_h_p_framework'));
$this->assertSame('Laravel P H P Framework', Str::headline('laravel _p _h _p _framework'));
$this->assertSame('Laravel Php Framework', Str::headline('laravel_php_framework'));
$this->assertSame('Laravel Ph P Framework', Str::headline('laravel-phP-framework'));
$this->assertSame('Laravel Php Framework', Str::headline('laravel -_- php -_- framework '));
$this->assertSame('Foo Bar', Str::headline('fooBar'));
$this->assertSame('Foo Bar', Str::headline('foo_bar'));
$this->assertSame('Foo Bar Baz', Str::headline('foo-barBaz'));
$this->assertSame('Foo Bar Baz', Str::headline('foo-bar_baz'));
$this->assertSame('Öffentliche Überraschungen', Str::headline('öffentliche-überraschungen'));
$this->assertSame('Öffentliche Überraschungen', Str::headline('-_öffentliche_überraschungen_-'));
$this->assertSame('Öffentliche Überraschungen', Str::headline('-öffentliche überraschungen'));
$this->assertSame('Sind Öde Und So', Str::headline('sindÖdeUndSo'));
$this->assertSame('Orwell 1984', Str::headline('orwell 1984'));
$this->assertSame('Orwell 1984', Str::headline('orwell 1984'));
$this->assertSame('Orwell 1984', Str::headline('-orwell-1984 -'));
$this->assertSame('Orwell 1984', Str::headline(' orwell_- 1984 '));
}
public function testStringWithoutWordsDoesntProduceError()
{
$nbsp = chr(0xC2).chr(0xA0);
$this->assertSame(' ', Str::words(' '));
$this->assertEquals($nbsp, Str::words($nbsp));
}
public function testStringAscii()
{
$this->assertSame('@', Str::ascii('@'));
$this->assertSame('u', Str::ascii('ü'));
}
public function testStringAsciiWithSpecificLocale()
{
$this->assertSame('h H sht Sht a A ia yo', Str::ascii('х Х щ Щ ъ Ъ иа йо', 'bg'));
$this->assertSame('ae oe ue Ae Oe Ue', Str::ascii('ä ö ü Ä Ö Ü', 'de'));
}
public function testStartsWith()
{
$this->assertTrue(Str::startsWith('jason', 'jas'));
$this->assertTrue(Str::startsWith('jason', 'jason'));
$this->assertTrue(Str::startsWith('jason', ['jas']));
$this->assertTrue(Str::startsWith('jason', ['day', 'jas']));
$this->assertFalse(Str::startsWith('jason', 'day'));
$this->assertFalse(Str::startsWith('jason', ['day']));
$this->assertFalse(Str::startsWith('jason', null));
$this->assertFalse(Str::startsWith('jason', [null]));
$this->assertFalse(Str::startsWith('0123', [null]));
$this->assertTrue(Str::startsWith('0123', 0));
$this->assertFalse(Str::startsWith('jason', 'J'));
$this->assertFalse(Str::startsWith('jason', ''));
$this->assertFalse(Str::startsWith('', ''));
$this->assertFalse(Str::startsWith('7', ' 7'));
$this->assertTrue(Str::startsWith('7a', '7'));
$this->assertTrue(Str::startsWith('7a', 7));
$this->assertTrue(Str::startsWith('7.12a', 7.12));
$this->assertFalse(Str::startsWith('7.12a', 7.13));
$this->assertTrue(Str::startsWith(7.123, '7'));
$this->assertTrue(Str::startsWith(7.123, '7.12'));
$this->assertFalse(Str::startsWith(7.123, '7.13'));
// Test for multibyte string support
$this->assertTrue(Str::startsWith('Jönköping', 'Jö'));
$this->assertTrue(Str::startsWith('Malmö', 'Malmö'));
$this->assertFalse(Str::startsWith('Jönköping', 'Jonko'));
$this->assertFalse(Str::startsWith('Malmö', 'Malmo'));
$this->assertTrue(Str::startsWith('你好', '你'));
$this->assertFalse(Str::startsWith('你好', '好'));
$this->assertFalse(Str::startsWith('你好', 'a'));
}
public function testEndsWith()
{
$this->assertTrue(Str::endsWith('jason', 'on'));
$this->assertTrue(Str::endsWith('jason', 'jason'));
$this->assertTrue(Str::endsWith('jason', ['on']));
$this->assertTrue(Str::endsWith('jason', ['no', 'on']));
$this->assertFalse(Str::endsWith('jason', 'no'));
$this->assertFalse(Str::endsWith('jason', ['no']));
$this->assertFalse(Str::endsWith('jason', ''));
$this->assertFalse(Str::endsWith('', ''));
$this->assertFalse(Str::endsWith('jason', [null]));
$this->assertFalse(Str::endsWith('jason', null));
$this->assertFalse(Str::endsWith('jason', 'N'));
$this->assertFalse(Str::endsWith('7', ' 7'));
$this->assertTrue(Str::endsWith('a7', '7'));
$this->assertTrue(Str::endsWith('a7', 7));
$this->assertTrue(Str::endsWith('a7.12', 7.12));
$this->assertFalse(Str::endsWith('a7.12', 7.13));
$this->assertTrue(Str::endsWith(0.27, '7'));
$this->assertTrue(Str::endsWith(0.27, '0.27'));
$this->assertFalse(Str::endsWith(0.27, '8'));
// Test for multibyte string support
$this->assertTrue(Str::endsWith('Jönköping', 'öping'));
$this->assertTrue(Str::endsWith('Malmö', 'mö'));
$this->assertFalse(Str::endsWith('Jönköping', 'oping'));
$this->assertFalse(Str::endsWith('Malmö', 'mo'));
$this->assertTrue(Str::endsWith('你好', '好'));
$this->assertFalse(Str::endsWith('你好', '你'));
$this->assertFalse(Str::endsWith('你好', 'a'));
}
public function testStrExcerpt()
{
$this->assertSame('...is a beautiful morn...', Str::excerpt('This is a beautiful morning', 'beautiful', ['radius' => 5]));
$this->assertSame('This is a...', Str::excerpt('This is a beautiful morning', 'this', ['radius' => 5]));
$this->assertSame('...iful morning', Str::excerpt('This is a beautiful morning', 'morning', ['radius' => 5]));
$this->assertNull(Str::excerpt('This is a beautiful morning', 'day'));
$this->assertSame('...is a beautiful! mor...', Str::excerpt('This is a beautiful! morning', 'Beautiful', ['radius' => 5]));
$this->assertSame('...is a beautiful? mor...', Str::excerpt('This is a beautiful? morning', 'beautiful', ['radius' => 5]));
$this->assertSame('', Str::excerpt('', '', ['radius' => 0]));
$this->assertSame('a', Str::excerpt('a', 'a', ['radius' => 0]));
$this->assertSame('...b...', Str::excerpt('abc', 'B', ['radius' => 0]));
$this->assertSame('abc', Str::excerpt('abc', 'b', ['radius' => 1]));
$this->assertSame('abc...', Str::excerpt('abcd', 'b', ['radius' => 1]));
$this->assertSame('...abc', Str::excerpt('zabc', 'b', ['radius' => 1]));
$this->assertSame('...abc...', Str::excerpt('zabcd', 'b', ['radius' => 1]));
$this->assertSame('zabcd', Str::excerpt('zabcd', 'b', ['radius' => 2]));
$this->assertSame('zabcd', Str::excerpt(' zabcd ', 'b', ['radius' => 4]));
$this->assertSame('...abc...', Str::excerpt('z abc d', 'b', ['radius' => 1]));
$this->assertSame('[...]is a beautiful morn[...]', Str::excerpt('This is a beautiful morning', 'beautiful', ['omission' => '[...]', 'radius' => 5]));
$this->assertSame(
'This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]',
Str::excerpt('This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome temperatures. So what are you gonna do about it?', 'very',
['omission' => '[...]']
));
$this->assertSame('...y...', Str::excerpt('taylor', 'y', ['radius' => 0]));
$this->assertSame('...ayl...', Str::excerpt('taylor', 'Y', ['radius' => 1]));
$this->assertSame('<div> The article description </div>', Str::excerpt('<div> The article description </div>', 'article'));
$this->assertSame('...The article desc...', Str::excerpt('<div> The article description </div>', 'article', ['radius' => 5]));
$this->assertSame('The article description', Str::excerpt(strip_tags('<div> The article description </div>'), 'article'));
$this->assertSame('', Str::excerpt(null));
$this->assertSame('', Str::excerpt(''));
$this->assertSame('', Str::excerpt(null, ''));
$this->assertSame('T...', Str::excerpt('The article description', null, ['radius' => 1]));
$this->assertSame('The arti...', Str::excerpt('The article description', '', ['radius' => 8]));
$this->assertSame('', Str::excerpt(' '));
$this->assertSame('The arti...', Str::excerpt('The article description', ' ', ['radius' => 4]));
$this->assertSame('...cle description', Str::excerpt('The article description', 'description', ['radius' => 4]));
$this->assertSame('T...', Str::excerpt('The article description', 'T', ['radius' => 0]));
$this->assertSame('What i?', Str::excerpt('What is the article?', 'What', ['radius' => 2, 'omission' => '?']));
$this->assertSame('...ö - 二 sān 大åè...', Str::excerpt('åèö - 二 sān 大åèö', '二 sān', ['radius' => 4]));
$this->assertSame('åèö - 二...', Str::excerpt('åèö - 二 sān 大åèö', 'åèö', ['radius' => 4]));
$this->assertSame('åèö - 二 sān 大åèö', Str::excerpt('åèö - 二 sān 大åèö', 'åèö - 二 sān 大åèö', ['radius' => 4]));
$this->assertSame('åèö - 二 sān 大åèö', Str::excerpt('åèö - 二 sān 大åèö', 'åèö - 二 sān 大åèö', ['radius' => 4]));
$this->assertSame('...༼...', Str::excerpt('㏗༼㏗', '༼', ['radius' => 0]));
$this->assertSame('...༼...', Str::excerpt('㏗༼㏗', '༼', ['radius' => 0]));
$this->assertSame('...ocê e...', Str::excerpt('Como você está', 'ê', ['radius' => 2]));
$this->assertSame('...ocê e...', Str::excerpt('Como você está', 'Ê', ['radius' => 2]));
$this->assertSame('João...', Str::excerpt('João Antônio ', 'jo', ['radius' => 2]));
$this->assertSame('João Antô...', Str::excerpt('João Antônio', 'JOÃO', ['radius' => 5]));
}
public function testStrBefore()
{
$this->assertSame('han', Str::before('hannah', 'nah'));
$this->assertSame('ha', Str::before('hannah', 'n'));
$this->assertSame('ééé ', Str::before('ééé hannah', 'han'));
$this->assertSame('hannah', Str::before('hannah', 'xxxx'));
$this->assertSame('hannah', Str::before('hannah', ''));
$this->assertSame('han', Str::before('han0nah', '0'));
$this->assertSame('han', Str::before('han0nah', 0));
$this->assertSame('han', Str::before('han2nah', 2));
}
public function testStrBeforeLast()
{
$this->assertSame('yve', Str::beforeLast('yvette', 'tte'));
$this->assertSame('yvet', Str::beforeLast('yvette', 't'));
$this->assertSame('ééé ', Str::beforeLast('ééé yvette', 'yve'));
$this->assertSame('', Str::beforeLast('yvette', 'yve'));
$this->assertSame('yvette', Str::beforeLast('yvette', 'xxxx'));
$this->assertSame('yvette', Str::beforeLast('yvette', ''));
$this->assertSame('yv0et', Str::beforeLast('yv0et0te', '0'));
$this->assertSame('yv0et', Str::beforeLast('yv0et0te', 0));
$this->assertSame('yv2et', Str::beforeLast('yv2et2te', 2));
}
public function testStrBetween()
{
$this->assertSame('abc', Str::between('abc', '', 'c'));
$this->assertSame('abc', Str::between('abc', 'a', ''));
$this->assertSame('abc', Str::between('abc', '', ''));
$this->assertSame('b', Str::between('abc', 'a', 'c'));
$this->assertSame('b', Str::between('dddabc', 'a', 'c'));
$this->assertSame('b', Str::between('abcddd', 'a', 'c'));
$this->assertSame('b', Str::between('dddabcddd', 'a', 'c'));
$this->assertSame('nn', Str::between('hannah', 'ha', 'ah'));
$this->assertSame('a]ab[b', Str::between('[a]ab[b]', '[', ']'));
$this->assertSame('foo', Str::between('foofoobar', 'foo', 'bar'));
$this->assertSame('bar', Str::between('foobarbar', 'foo', 'bar'));
}
public function testStrBetweenFirst()
{
$this->assertSame('abc', Str::betweenFirst('abc', '', 'c'));
$this->assertSame('abc', Str::betweenFirst('abc', 'a', ''));
$this->assertSame('abc', Str::betweenFirst('abc', '', ''));
$this->assertSame('b', Str::betweenFirst('abc', 'a', 'c'));
$this->assertSame('b', Str::betweenFirst('dddabc', 'a', 'c'));
$this->assertSame('b', Str::betweenFirst('abcddd', 'a', 'c'));
$this->assertSame('b', Str::betweenFirst('dddabcddd', 'a', 'c'));
$this->assertSame('nn', Str::betweenFirst('hannah', 'ha', 'ah'));
$this->assertSame('a', Str::betweenFirst('[a]ab[b]', '[', ']'));
$this->assertSame('foo', Str::betweenFirst('foofoobar', 'foo', 'bar'));
$this->assertSame('', Str::betweenFirst('foobarbar', 'foo', 'bar'));
}
public function testStrAfter()
{
$this->assertSame('nah', Str::after('hannah', 'han'));
$this->assertSame('nah', Str::after('hannah', 'n'));
$this->assertSame('nah', Str::after('ééé hannah', 'han'));
$this->assertSame('hannah', Str::after('hannah', 'xxxx'));
$this->assertSame('hannah', Str::after('hannah', ''));
$this->assertSame('nah', Str::after('han0nah', '0'));
$this->assertSame('nah', Str::after('han0nah', 0));
$this->assertSame('nah', Str::after('han2nah', 2));
}
public function testStrAfterLast()
{
$this->assertSame('tte', Str::afterLast('yvette', 'yve'));
$this->assertSame('e', Str::afterLast('yvette', 't'));
$this->assertSame('e', Str::afterLast('ééé yvette', 't'));
$this->assertSame('', Str::afterLast('yvette', 'tte'));
$this->assertSame('yvette', Str::afterLast('yvette', 'xxxx'));
$this->assertSame('yvette', Str::afterLast('yvette', ''));
$this->assertSame('te', Str::afterLast('yv0et0te', '0'));
$this->assertSame('te', Str::afterLast('yv0et0te', 0));
$this->assertSame('te', Str::afterLast('yv2et2te', 2));
$this->assertSame('foo', Str::afterLast('----foo', '---'));
}
/**
* @dataProvider strContainsProvider
*/
public function testStrContains($haystack, $needles, $expected, $ignoreCase = false)
{
$this->assertEquals($expected, Str::contains($haystack, $needles, $ignoreCase));
}
/**
* @dataProvider strContainsAllProvider
*/
public function testStrContainsAll($haystack, $needles, $expected, $ignoreCase = false)
{
$this->assertEquals($expected, Str::containsAll($haystack, $needles, $ignoreCase));
}
public function testParseCallback()
{
$this->assertEquals(['Class', 'method'], Str::parseCallback('Class@method', 'foo'));
$this->assertEquals(['Class', 'foo'], Str::parseCallback('Class', 'foo'));
$this->assertEquals(['Class', null], Str::parseCallback('Class'));
}
public function testSlug()
{
$this->assertSame('hello-world', Str::slug('hello world'));
$this->assertSame('hello-world', Str::slug('hello-world'));
$this->assertSame('hello-world', Str::slug('hello_world'));
$this->assertSame('hello_world', Str::slug('hello_world', '_'));
$this->assertSame('user-at-host', Str::slug('user@host'));
$this->assertSame('سلام-دنیا', Str::slug('سلام دنیا', '-', null));
$this->assertSame('sometext', Str::slug('some text', ''));
$this->assertSame('', Str::slug('', ''));
$this->assertSame('', Str::slug(''));
}
public function testStrStart()
{
$this->assertSame('/test/string', Str::start('test/string', '/'));
$this->assertSame('/test/string', Str::start('/test/string', '/'));
$this->assertSame('/test/string', Str::start('//test/string', '/'));
}
public function testFlushCache()
{
$reflection = new ReflectionClass(Str::class);
$property = $reflection->getProperty('snakeCache');
$property->setAccessible(true);
Str::flushCache();
$this->assertEmpty($property->getValue());
Str::snake('Taylor Otwell');
$this->assertNotEmpty($property->getValue());
Str::flushCache();
$this->assertEmpty($property->getValue());
}
public function testFinish()
{
$this->assertSame('abbc', Str::finish('ab', 'bc'));
$this->assertSame('abbc', Str::finish('abbcbc', 'bc'));
$this->assertSame('abcbbc', Str::finish('abcbbcbc', 'bc'));
}
public function testIs()
{
$this->assertTrue(Str::is('/', '/'));
$this->assertFalse(Str::is('/', ' /'));
$this->assertFalse(Str::is('/', '/a'));
$this->assertTrue(Str::is('foo/*', 'foo/bar/baz'));
$this->assertTrue(Str::is('*@*', 'App\Class@method'));
$this->assertTrue(Str::is('*@*', 'app\Class@'));
$this->assertTrue(Str::is('*@*', '@method'));
// is case sensitive
$this->assertFalse(Str::is('*BAZ*', 'foo/bar/baz'));
$this->assertFalse(Str::is('*FOO*', 'foo/bar/baz'));
$this->assertFalse(Str::is('A', 'a'));
// Accepts array of patterns
$this->assertTrue(Str::is(['a*', 'b*'], 'a/'));
$this->assertTrue(Str::is(['a*', 'b*'], 'b/'));
$this->assertFalse(Str::is(['a*', 'b*'], 'f/'));
// numeric values and patterns
$this->assertFalse(Str::is(['a*', 'b*'], 123));
$this->assertTrue(Str::is(['*2*', 'b*'], 11211));
$this->assertTrue(Str::is('*/foo', 'blah/baz/foo'));
$valueObject = new StringableObjectStub('foo/bar/baz');
$patternObject = new StringableObjectStub('foo/*');
$this->assertTrue(Str::is('foo/bar/baz', $valueObject));
$this->assertTrue(Str::is($patternObject, $valueObject));
// empty patterns
$this->assertFalse(Str::is([], 'test'));
$this->assertFalse(Str::is('', 0));
$this->assertFalse(Str::is([null], 0));
$this->assertTrue(Str::is([null], null));
}
/**
* @dataProvider validUuidList
*/
public function testIsUuidWithValidUuid($uuid)
{
$this->assertTrue(Str::isUuid($uuid));
}
/**
* @dataProvider invalidUuidList
*/
public function testIsUuidWithInvalidUuid($uuid)
{
$this->assertFalse(Str::isUuid($uuid));
}
public function testKebab()
{
$this->assertSame('laravel-php-framework', Str::kebab('LaravelPhpFramework'));
}
public function testLower()
{
$this->assertSame('foo bar baz', Str::lower('FOO BAR BAZ'));
$this->assertSame('foo bar baz', Str::lower('fOo Bar bAz'));
}
public function testUpper()
{
$this->assertSame('FOO BAR BAZ', Str::upper('foo bar baz'));
$this->assertSame('FOO BAR BAZ', Str::upper('foO bAr BaZ'));
}
public function testLimit()
{
$this->assertSame('Laravel is...', Str::limit('Laravel is a free, open source PHP web application framework.', 10));
$this->assertSame('这是一...', Str::limit('这是一段中文', 6));
$string = 'The PHP framework for web artisans.';
$this->assertSame('The PHP...', Str::limit($string, 7));
$this->assertSame('The PHP', Str::limit($string, 7, ''));
$this->assertSame('The PHP framework for web artisans.', Str::limit($string, 100));
$nonAsciiString = '这是一段中文';
$this->assertSame('这是一...', Str::limit($nonAsciiString, 6));
$this->assertSame('这是一', Str::limit($nonAsciiString, 6, ''));
}
public function testLength()
{
$this->assertEquals(11, Str::length('foo bar baz'));
$this->assertEquals(11, Str::length('foo bar baz', 'UTF-8'));
}
public function testRandom()
{
$this->assertEquals(16, strlen(Str::random()));
$randomInteger = random_int(1, 100);
$this->assertEquals($randomInteger, strlen(Str::random($randomInteger)));
$this->assertIsString(Str::random());
}
public function testReplace()
{
$this->assertSame('foo bar laravel', Str::replace('baz', 'laravel', 'foo bar baz'));
$this->assertSame('foo bar baz 8.x', Str::replace('?', '8.x', 'foo bar baz ?'));
$this->assertSame('foo/bar/baz', Str::replace(' ', '/', 'foo bar baz'));
$this->assertSame('foo bar baz', Str::replace(['?1', '?2', '?3'], ['foo', 'bar', 'baz'], '?1 ?2 ?3'));
}
public function testReplaceArray()
{
$this->assertSame('foo/bar/baz', Str::replaceArray('?', ['foo', 'bar', 'baz'], '?/?/?'));
$this->assertSame('foo/bar/baz/?', Str::replaceArray('?', ['foo', 'bar', 'baz'], '?/?/?/?'));
$this->assertSame('foo/bar', Str::replaceArray('?', ['foo', 'bar', 'baz'], '?/?'));
$this->assertSame('?/?/?', Str::replaceArray('x', ['foo', 'bar', 'baz'], '?/?/?'));
// Ensure recursive replacements are avoided
$this->assertSame('foo?/bar/baz', Str::replaceArray('?', ['foo?', 'bar', 'baz'], '?/?/?'));
// Test for associative array support
$this->assertSame('foo/bar', Str::replaceArray('?', [1 => 'foo', 2 => 'bar'], '?/?'));
$this->assertSame('foo/bar', Str::replaceArray('?', ['x' => 'foo', 'y' => 'bar'], '?/?'));
}
public function testReplaceFirst()
{
$this->assertSame('fooqux foobar', Str::replaceFirst('bar', 'qux', 'foobar foobar'));
$this->assertSame('foo/qux? foo/bar?', Str::replaceFirst('bar?', 'qux?', 'foo/bar? foo/bar?'));
$this->assertSame('foo foobar', Str::replaceFirst('bar', '', 'foobar foobar'));
$this->assertSame('foobar foobar', Str::replaceFirst('xxx', 'yyy', 'foobar foobar'));
$this->assertSame('foobar foobar', Str::replaceFirst('', 'yyy', 'foobar foobar'));
$this->assertSame('1', Str::replaceFirst(0, '1', '0'));
// Test for multibyte string support
$this->assertSame('Jxxxnköping Malmö', Str::replaceFirst('ö', 'xxx', 'Jönköping Malmö'));
$this->assertSame('Jönköping Malmö', Str::replaceFirst('', 'yyy', 'Jönköping Malmö'));
}
public function testReplaceLast()
{
$this->assertSame('foobar fooqux', Str::replaceLast('bar', 'qux', 'foobar foobar'));
$this->assertSame('foo/bar? foo/qux?', Str::replaceLast('bar?', 'qux?', 'foo/bar? foo/bar?'));
$this->assertSame('foobar foo', Str::replaceLast('bar', '', 'foobar foobar'));
$this->assertSame('foobar foobar', Str::replaceLast('xxx', 'yyy', 'foobar foobar'));
$this->assertSame('foobar foobar', Str::replaceLast('', 'yyy', 'foobar foobar'));
// Test for multibyte string support
$this->assertSame('Malmö Jönkxxxping', Str::replaceLast('ö', 'xxx', 'Malmö Jönköping'));
$this->assertSame('Malmö Jönköping', Str::replaceLast('', 'yyy', 'Malmö Jönköping'));
}
public function testRemove()
{
$this->assertSame('Fbar', Str::remove('o', 'Foobar'));
$this->assertSame('Foo', Str::remove('bar', 'Foobar'));
$this->assertSame('oobar', Str::remove('F', 'Foobar'));
$this->assertSame('Foobar', Str::remove('f', 'Foobar'));
$this->assertSame('oobar', Str::remove('f', 'Foobar', false));
$this->assertSame('Fbr', Str::remove(['o', 'a'], 'Foobar'));
$this->assertSame('Fooar', Str::remove(['f', 'b'], 'Foobar'));
$this->assertSame('ooar', Str::remove(['f', 'b'], 'Foobar', false));
$this->assertSame('Foobar', Str::remove(['f', '|'], 'Foo|bar'));
}
public function testReverse()
{
$this->assertSame('FooBar', Str::reverse('raBooF'));
$this->assertSame('Teniszütő', Str::reverse('őtüzsineT'));
$this->assertSame('❤MultiByte☆', Str::reverse('☆etyBitluM❤'));
}
public function testSnake()
{
$this->assertSame('laravel_p_h_p_framework', Str::snake('LaravelPHPFramework'));
$this->assertSame('laravel_php_framework', Str::snake('LaravelPhpFramework'));
$this->assertSame('laravel php framework', Str::snake('LaravelPhpFramework', ' '));
$this->assertSame('laravel_php_framework', Str::snake('Laravel Php Framework'));
$this->assertSame('laravel_php_framework', Str::snake('Laravel Php Framework '));
// ensure cache keys don't overlap
$this->assertSame('laravel__php__framework', Str::snake('LaravelPhpFramework', '__'));
$this->assertSame('laravel_php_framework_', Str::snake('LaravelPhpFramework_', '_'));
$this->assertSame('laravel_php_framework', Str::snake('laravel php Framework'));
$this->assertSame('laravel_php_frame_work', Str::snake('laravel php FrameWork'));
// prevent breaking changes
$this->assertSame('foo-bar', Str::snake('foo-bar'));
$this->assertSame('foo-_bar', Str::snake('Foo-Bar'));
$this->assertSame('foo__bar', Str::snake('Foo_Bar'));
$this->assertSame('żółtałódka', Str::snake('ŻółtaŁódka'));
}
public function testSquish()
{
$this->assertSame('laravel php framework', Str::squish(' laravel php framework '));
$this->assertSame('laravel php framework', Str::squish("laravel\t\tphp\n\nframework"));
$this->assertSame('laravel php framework', Str::squish('
laravel
php
framework
'));
}
public function testStudly()
{
$this->assertSame('LaravelPHPFramework', Str::studly('laravel_p_h_p_framework'));
$this->assertSame('LaravelPhpFramework', Str::studly('laravel_php_framework'));
$this->assertSame('LaravelPhPFramework', Str::studly('laravel-phP-framework'));
$this->assertSame('LaravelPhpFramework', Str::studly('laravel -_- php -_- framework '));
$this->assertSame('FooBar', Str::studly('fooBar'));
$this->assertSame('FooBar', Str::studly('foo_bar'));
$this->assertSame('FooBar', Str::studly('foo_bar')); // test cache
$this->assertSame('FooBarBaz', Str::studly('foo-barBaz'));
$this->assertSame('FooBarBaz', Str::studly('foo-bar_baz'));
$this->assertSame('ÖffentlicheÜberraschungen', Str::studly('öffentliche-überraschungen'));
}
public function testMask()
{
$this->assertSame('tay*************', Str::mask('taylor@email.com', '*', 3));
$this->assertSame('******@email.com', Str::mask('taylor@email.com', '*', 0, 6));
$this->assertSame('tay*************', Str::mask('taylor@email.com', '*', -13));
$this->assertSame('tay***@email.com', Str::mask('taylor@email.com', '*', -13, 3));
$this->assertSame('****************', Str::mask('taylor@email.com', '*', -17));
$this->assertSame('*****r@email.com', Str::mask('taylor@email.com', '*', -99, 5));
$this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '*', 16));
$this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '*', 16, 99));
$this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '', 3));
$this->assertSame('taysssssssssssss', Str::mask('taylor@email.com', 'something', 3));
$this->assertSame('taysssssssssssss', Str::mask('taylor@email.com', Str::of('something'), 3));
$this->assertSame('这是一***', Str::mask('这是一段中文', '*', 3));
$this->assertSame('**一段中文', Str::mask('这是一段中文', '*', 0, 2));
}
public function testMatch()
{
$this->assertSame('bar', Str::match('/bar/', 'foo bar'));
$this->assertSame('bar', Str::match('/foo (.*)/', 'foo bar'));
$this->assertEmpty(Str::match('/nothing/', 'foo bar'));
$this->assertEquals(['bar', 'bar'], Str::matchAll('/bar/', 'bar foo bar')->all());
$this->assertEquals(['un', 'ly'], Str::matchAll('/f(\w*)/', 'bar fun bar fly')->all());
$this->assertEmpty(Str::matchAll('/nothing/', 'bar fun bar fly'));
}
public function testCamel()
{
$this->assertSame('laravelPHPFramework', Str::camel('Laravel_p_h_p_framework'));
$this->assertSame('laravelPhpFramework', Str::camel('Laravel_php_framework'));
$this->assertSame('laravelPhPFramework', Str::camel('Laravel-phP-framework'));
$this->assertSame('laravelPhpFramework', Str::camel('Laravel -_- php -_- framework '));
$this->assertSame('fooBar', Str::camel('FooBar'));
$this->assertSame('fooBar', Str::camel('foo_bar'));
$this->assertSame('fooBar', Str::camel('foo_bar')); // test cache
$this->assertSame('fooBarBaz', Str::camel('Foo-barBaz'));
$this->assertSame('fooBarBaz', Str::camel('foo-bar_baz'));
}
public function testSubstr()
{
$this->assertSame('Ё', Str::substr('БГДЖИЛЁ', -1));
$this->assertSame('ЛЁ', Str::substr('БГДЖИЛЁ', -2));
$this->assertSame('И', Str::substr('БГДЖИЛЁ', -3, 1));
$this->assertSame('ДЖИЛ', Str::substr('БГДЖИЛЁ', 2, -1));
$this->assertEmpty(Str::substr('БГДЖИЛЁ', 4, -4));
$this->assertSame('ИЛ', Str::substr('БГДЖИЛЁ', -3, -1));
$this->assertSame('ГДЖИЛЁ', Str::substr('БГДЖИЛЁ', 1));
$this->assertSame('ГДЖ', Str::substr('БГДЖИЛЁ', 1, 3));
$this->assertSame('БГДЖ', Str::substr('БГДЖИЛЁ', 0, 4));
$this->assertSame('Ё', Str::substr('БГДЖИЛЁ', -1, 1));
$this->assertEmpty(Str::substr('Б', 2));
}
public function testSubstrCount()
{
$this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a'));
$this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z'));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'l', 2));
$this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z', 2));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2));
$this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a', 1, -2));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', -10, -3));
}
public function testSubstrReplace()
{
$this->assertSame('12:00', Str::substrReplace('1200', ':', 2, 0));
$this->assertSame('The Laravel Framework', Str::substrReplace('The Framework', 'Laravel ', 4, 0));
$this->assertSame('Laravel – The PHP Framework for Web Artisans', Str::substrReplace('Laravel Framework', '– The PHP Framework for Web Artisans', 8));
}
public function testLcfirst()
{
$this->assertSame('laravel', Str::lcfirst('Laravel'));
$this->assertSame('laravel framework', Str::lcfirst('Laravel framework'));
$this->assertSame('мама', Str::lcfirst('Мама'));
$this->assertSame('мама мыла раму', Str::lcfirst('Мама мыла раму'));
}
public function testUcfirst()
{
$this->assertSame('Laravel', Str::ucfirst('laravel'));
$this->assertSame('Laravel framework', Str::ucfirst('laravel framework'));
$this->assertSame('Мама', Str::ucfirst('мама'));
$this->assertSame('Мама мыла раму', Str::ucfirst('мама мыла раму'));
}
public function testUcsplit()
{
$this->assertSame(['Laravel_p_h_p_framework'], Str::ucsplit('Laravel_p_h_p_framework'));
$this->assertSame(['Laravel_', 'P_h_p_framework'], Str::ucsplit('Laravel_P_h_p_framework'));
$this->assertSame(['laravel', 'P', 'H', 'P', 'Framework'], Str::ucsplit('laravelPHPFramework'));
$this->assertSame(['Laravel-ph', 'P-framework'], Str::ucsplit('Laravel-phP-framework'));
$this->assertSame(['Żółta', 'Łódka'], Str::ucsplit('ŻółtaŁódka'));
$this->assertSame(['sind', 'Öde', 'Und', 'So'], Str::ucsplit('sindÖdeUndSo'));
$this->assertSame(['Öffentliche', 'Überraschungen'], Str::ucsplit('ÖffentlicheÜberraschungen'));
}
public function testUuid()
{
$this->assertInstanceOf(UuidInterface::class, Str::uuid());
$this->assertInstanceOf(UuidInterface::class, Str::orderedUuid());
}
public function testAsciiNull()
{
$this->assertSame('', Str::ascii(null));
$this->assertTrue(Str::isAscii(null));
$this->assertSame('', Str::slug(null));
}
public function testPadBoth()
{
$this->assertSame('__Alien___', Str::padBoth('Alien', 10, '_'));
$this->assertSame(' Alien ', Str::padBoth('Alien', 10));
}
public function testPadLeft()
{
$this->assertSame('-=-=-Alien', Str::padLeft('Alien', 10, '-='));
$this->assertSame(' Alien', Str::padLeft('Alien', 10));
}
public function testPadRight()
{
$this->assertSame('Alien-----', Str::padRight('Alien', 10, '-'));
$this->assertSame('Alien ', Str::padRight('Alien', 10));
}
public function testSwapKeywords()/*: void*/
{
$this->assertSame(
'PHP 8 is fantastic',
Str::swap([
'PHP' => 'PHP 8',
'awesome' => 'fantastic',
], 'PHP is awesome')
);
$this->assertSame(
'foo bar baz',
Str::swap([
'ⓐⓑ' => 'baz',
], 'foo bar ⓐⓑ')
);
}
public function testWordCount()
{
$this->assertEquals(2, Str::wordCount('Hello, world!'));
$this->assertEquals(10, Str::wordCount('Hi, this is my first contribution to the Laravel framework.'));
$this->assertEquals(0, Str::wordCount('мама'));
$this->assertEquals(0, Str::wordCount('мама мыла раму'));
$this->assertEquals(1, Str::wordCount('мама', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
$this->assertEquals(3, Str::wordCount('мама мыла раму', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
$this->assertEquals(1, Str::wordCount('МАМА', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
$this->assertEquals(3, Str::wordCount('МАМА МЫЛА РАМУ', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
}
public function validUuidList()
{
return [
['a0a2a2d2-0b87-4a18-83f2-2529882be2de'],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
['00000000-0000-0000-0000-000000000000'],
['e60d3f48-95d7-4d8d-aad0-856f29a27da2'],
['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'],
['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'],
];
}
public function invalidUuidList()
{
return [
['not a valid uuid so we can test this'],
['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'.PHP_EOL],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1 '],
[' 145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
['145a1e72-d11d-11e8-a8d5-f2z01f1b9fd1'],
['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'],
['af6f8cb-c57d-11e1-9b21-0800200c9a66'],
['af6f8cb0c57d11e19b210800200c9a66'],
['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'],
];
}
public function strContainsProvider()
{
return [
['Taylor', 'ylo', true, true],
['Taylor', 'ylo', true, false],
['Taylor', 'taylor', true, true],
['Taylor', 'taylor', false, false],
['Taylor', ['ylo'], true, true],
['Taylor', ['ylo'], true, false],
['Taylor', ['xxx', 'ylo'], true, true],
['Taylor', ['xxx', 'ylo'], true, false],
['Taylor', 'xxx', false],
['Taylor', ['xxx'], false],
['Taylor', '', false],
['', '', false],
];
}
public function strContainsAllProvider()
{
return [
['Taylor Otwell', ['taylor', 'otwell'], false, false],
['Taylor Otwell', ['taylor', 'otwell'], true, true],
['Taylor Otwell', ['taylor'], false, false],
['Taylor Otwell', ['taylor'], true, true],
['Taylor Otwell', ['taylor', 'xxx'], false, false],
['Taylor Otwell', ['taylor', 'xxx'], false, true],
];
}
public function testMarkdown()
{
$this->assertSame("<p><em>hello world</em></p>\n", Str::markdown('*hello world*'));
$this->assertSame("<h1>hello world</h1>\n", Str::markdown('# hello world'));
}
public function testRepeat()
{
$this->assertSame('aaaaa', Str::repeat('a', 5));
$this->assertSame('', Str::repeat('', 5));
}
/**
* @dataProvider specialCharacterProvider
*/
public function testTransliterate(/*string */$value, /*string */$expected)/*: void*/
{
$expected = cast_to_string($expected);
$value = cast_to_string($value);
$this->assertSame($expected, Str::transliterate($value));
}
public function specialCharacterProvider()/*: array*/
{
return [
['ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ', 'abcdefghijklmnopqrstuvwxyz'],
['⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳', '01234567891011121314151617181920'],
['⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾', '12345678910'],
['⓿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴', '011121314151617181920'],
['ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ', 'test@laravel.com'],
['🎂', '?'],
['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'],
['0123456789', '0123456789'],
];
}
public function testTransliterateOverrideUnknown()/*: void*/
{
$this->assertSame('HHH', Str::transliterate('🎂🚧🏆', 'H'));
$this->assertSame('Hello', Str::transliterate('🎂', 'Hello'));
}
/**
* @dataProvider specialCharacterProvider
*/
public function testTransliterateStrict(/*string */$value, /*string */$expected)/*: void*/
{
$expected = cast_to_string($expected);
$value = cast_to_string($value);
$this->assertSame($expected, Str::transliterate($value, '?', true));
}
}
class StringableObjectStub
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function __toString()
{
return $this->value;
}
}