Skip to content

Commit 27ab9be

Browse files
committed
add tests and fixes
1 parent 45967af commit 27ab9be

File tree

6 files changed

+206
-76
lines changed

6 files changed

+206
-76
lines changed

src/Symfony/Component/String/AbstractString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ public function split(string $delimiter, int $limit = null, int $flags = null):
455455

456456
$str = clone $this;
457457

458-
if (self::PREG_OFFSET_CAPTURE & $flags) {
458+
if (self::PREG_SPLIT_OFFSET_CAPTURE & $flags) {
459459
foreach ($chunks as &$chunk) {
460460
$str->string = $chunk[0];
461461
$chunk[0] = clone $str;

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public function trim(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): parent
335335
$chars = preg_quote($chars);
336336

337337
$str = clone $this;
338-
$str->string = preg_replace("{^[$chars]++|[$chars]++$}u", '', $str->string);
338+
$str->string = preg_replace("{^[$chars]++|[$chars]++$}uD", '', $str->string);
339339

340340
return $str;
341341
}
@@ -348,7 +348,7 @@ public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): pare
348348
$chars = preg_quote($chars);
349349

350350
$str = clone $this;
351-
$str->string = preg_replace("{[$chars]++$}u", '', $str->string);
351+
$str->string = preg_replace("{[$chars]++$}uD", '', $str->string);
352352

353353
return $str;
354354
}

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

Lines changed: 164 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static function provideIndexOf(): array
7979
[0, 'abc', 'a', 0],
8080
[1, 'abc', 'b', 1],
8181
[2, 'abc', 'c', 1],
82+
[4, 'abacabab', 'ab', 1],
8283
[4, '123abc', 'b', -3],
8384
];
8485
}
@@ -109,6 +110,7 @@ public static function provideIndexOfIgnoreCase(): array
109110
[1, 'ABC', 'b', 1],
110111
[2, 'ABC', 'c', 0],
111112
[2, 'ABC', 'c', 2],
113+
[4, 'ABACaBAB', 'Ab', 1],
112114
[4, '123abc', 'B', -3],
113115
];
114116
}
@@ -173,9 +175,9 @@ public static function provideIndexOfLastIgnoreCase(): array
173175
/**
174176
* @dataProvider provideSplit
175177
*/
176-
public function testSplit(string $string, string $delimiter, array $chunks, ?int $limit)
178+
public function testSplit(string $string, string $delimiter, array $chunks, ?int $limit, int $flags = null)
177179
{
178-
$this->assertEquals($chunks, static::createFromString($string)->split($delimiter, $limit));
180+
$this->assertEquals($chunks, static::createFromString($string)->split($delimiter, $limit, $flags));
179181
}
180182

181183
public static function provideSplit(): array
@@ -240,6 +242,44 @@ public static function provideSplit(): array
240242
],
241243
null,
242244
],
245+
[
246+
'foo,,bar, baz , qux,kix',
247+
'/\s*,\s*/',
248+
[
249+
static::createFromString('foo'),
250+
static::createFromString(''),
251+
static::createFromString('bar'),
252+
static::createFromString('baz'),
253+
static::createFromString('qux,kix'),
254+
],
255+
5,
256+
AbstractString::PREG_SPLIT,
257+
],
258+
[
259+
'foo ,,bar, baz',
260+
'/\s*(,)\s*/',
261+
[
262+
static::createFromString('foo'),
263+
static::createFromString(','),
264+
static::createFromString(','),
265+
static::createFromString('bar'),
266+
static::createFromString(','),
267+
static::createFromString('baz'),
268+
],
269+
null,
270+
AbstractString::PREG_SPLIT_NO_EMPTY|AbstractString::PREG_SPLIT_DELIM_CAPTURE,
271+
],
272+
[
273+
'foo, bar,baz',
274+
'/\s*(,)\s*/',
275+
[
276+
[static::createFromString('foo'), 0],
277+
[static::createFromString('bar'), 5],
278+
[static::createFromString('baz'), 9],
279+
],
280+
null,
281+
AbstractString::PREG_SPLIT_OFFSET_CAPTURE,
282+
],
243283
];
244284
}
245285

@@ -351,7 +391,7 @@ public static function provideLower()
351391
return [
352392
['hello world', 'hello world'],
353393
['hello world', 'HELLO WORLD'],
354-
['hello world', 'Hello World'],
394+
['hello world!', 'Hello World!'],
355395
['symfony', 'symfony'],
356396
['symfony', 'Symfony'],
357397
['symfony', 'sYmFOny'],
@@ -375,7 +415,7 @@ public static function provideUpper()
375415
return [
376416
['HELLO WORLD', 'hello world'],
377417
['HELLO WORLD', 'HELLO WORLD'],
378-
['HELLO WORLD', 'Hello World'],
418+
['HELLO WORLD!', 'Hello World!'],
379419
['SYMFONY', 'symfony'],
380420
['SYMFONY', 'Symfony'],
381421
['SYMFONY', 'sYmFOny'],
@@ -425,8 +465,11 @@ public static function provideSlice()
425465
['Symfony', 'Symfony is awesome', 0, 7],
426466
[' ', 'Symfony is awesome', 7, 1],
427467
['is', 'Symfony is awesome', 8, 2],
468+
['is awesome', 'Symfony is awesome', 8, null],
428469
[' ', 'Symfony is awesome', 10, 1],
429470
['awesome', 'Symfony is awesome', 11, 7],
471+
['awesome', 'Symfony is awesome', -7, null],
472+
['awe', 'Symfony is awesome', -7, -4],
430473
];
431474
}
432475

@@ -450,6 +493,10 @@ public function testAppend(string $expected, array $suffixes)
450493
public static function provideAppend()
451494
{
452495
return [
496+
[
497+
'',
498+
[],
499+
],
453500
[
454501
'Symfony',
455502
['Sym', 'fony'],
@@ -512,6 +559,11 @@ public static function provideTrim()
512559
" Symfony IS GREAT\t!!!\n",
513560
" \n!",
514561
],
562+
[
563+
"Symfony IS GREAT\t!!! \n",
564+
" Symfony IS GREAT\t!!! \n",
565+
' ',
566+
],
515567
];
516568
}
517569

@@ -566,102 +618,125 @@ public static function provideTrimEnd()
566618
"\n\t<span>Symfony is a PHP framework</span> \n",
567619
"\n",
568620
],
621+
[
622+
"\n\t<span>Symfony is a PHP framework</span> \n",
623+
"\n\t<span>Symfony is a PHP framework</span> \n",
624+
' ',
625+
],
569626
];
570627
}
571628

572629
/**
573630
* @dataProvider provideBeforeAfter
574631
*/
575-
public function testBeforeAfter(string $expected, string $needle, string $origin, bool $before)
632+
public function testBeforeAfter(string $expected, string $needle, string $origin, int $offset, bool $before)
576633
{
577634
$result = static::createFromString($origin);
578-
$result = $before ? $result->before($needle, false) : $result->after($needle, true);
635+
$result = $before ? $result->before($needle, false, $offset) : $result->after($needle, true, $offset);
579636
$this->assertEquals(static::createFromString($expected), $result);
580637
}
581638

582639
public static function provideBeforeAfter()
583640
{
584641
return [
585-
['', '', 'hello world', true],
586-
['', '', 'hello world', false],
587-
['', 'w', 'hello World', true],
588-
['', 'w', 'hello World', false],
589-
['hello ', 'w', 'hello world', true],
590-
['world', 'w', 'hello world', false],
642+
['', '', 'hello world', 0, true],
643+
['', '', 'hello world', 0, false],
644+
['', 'w', 'hello World', 0, true],
645+
['', 'w', 'hello World', 0, false],
646+
['', 'o', 'hello world', 10, true],
647+
['', 'o', 'hello world', 10, false],
648+
['hello ', 'w', 'hello world', 0, true],
649+
['world', 'w', 'hello world', 0, false],
650+
['hello W', 'O', 'hello WORLD', 0, true],
651+
['ORLD', 'O', 'hello WORLD', 0, false],
652+
['abac', 'ab', 'abacabab', 1, true],
653+
['abab', 'ab', 'abacabab', 1, false],
591654
];
592655
}
593656

594657
/**
595658
* @dataProvider provideBeforeAfterIgnoreCase
596659
*/
597-
public function testBeforeAfterIgnoreCase(string $expected, string $needle, string $origin, bool $before)
660+
public function testBeforeAfterIgnoreCase(string $expected, string $needle, string $origin, int $offset, bool $before)
598661
{
599662
$result = static::createFromString($origin)->ignoreCase();
600-
$result = $before ? $result->before($needle, false) : $result->after($needle, true);
663+
$result = $before ? $result->before($needle, false, $offset) : $result->after($needle, true, $offset);
601664
$this->assertEquals(static::createFromString($expected), $result);
602665
}
603666

604667
public static function provideBeforeAfterIgnoreCase()
605668
{
606669
return [
607-
['', '', 'hello world', true],
608-
['', '', 'hello world', false],
609-
['', 'foo', 'hello world', true],
610-
['', 'foo', 'hello world', false],
611-
['hello ', 'w', 'hello world', true],
612-
['world', 'w', 'hello world', false],
613-
['hello ', 'W', 'hello world', true],
614-
['world', 'W', 'hello world', false],
670+
['', '', 'hello world', 0, true],
671+
['', '', 'hello world', 0, false],
672+
['', 'foo', 'hello world', 0, true],
673+
['', 'foo', 'hello world', 0, false],
674+
['', 'o', 'hello world', 10, true],
675+
['', 'o', 'hello world', 10, false],
676+
['hello ', 'w', 'hello world', 0, true],
677+
['world', 'w', 'hello world', 0, false],
678+
['hello ', 'W', 'hello world', 0, true],
679+
['world', 'W', 'hello world', 0, false],
680+
['Abac', 'Ab', 'AbacaBAb', 1, true],
681+
['aBAb', 'Ab', 'AbacaBAb', 1, false],
615682
];
616683
}
617684

618685
/**
619686
* @dataProvider provideBeforeAfterLast
620687
*/
621-
public function testBeforeAfterLast(string $expected, string $needle, string $origin, bool $before)
688+
public function testBeforeAfterLast(string $expected, string $needle, string $origin, int $offset, bool $before)
622689
{
623690
$result = static::createFromString($origin);
624-
$result = $before ? $result->beforeLast($needle, false) : $result->afterLast($needle, true);
691+
$result = $before ? $result->beforeLast($needle, false, $offset) : $result->afterLast($needle, true, $offset);
625692
$this->assertEquals(static::createFromString($expected), $result);
626693
}
627694

628695
public static function provideBeforeAfterLast()
629696
{
630697
return [
631-
['', '', 'hello world', true],
632-
['', '', 'hello world', false],
633-
['', 'L', 'hello world', true],
634-
['', 'L', 'hello world', false],
635-
['hello wor', 'l', 'hello world', true],
636-
['ld', 'l', 'hello world', false],
637-
['hello w', 'o', 'hello world', true],
638-
['orld', 'o', 'hello world', false],
698+
['', '', 'hello world', 0, true],
699+
['', '', 'hello world', 0, false],
700+
['', 'L', 'hello world', 0, true],
701+
['', 'L', 'hello world', 0, false],
702+
['', 'o', 'hello world', 10, true],
703+
['', 'o', 'hello world', 10, false],
704+
['hello wor', 'l', 'hello world', 0, true],
705+
['ld', 'l', 'hello world', 0, false],
706+
['hello w', 'o', 'hello world', 0, true],
707+
['orld', 'o', 'hello world', 0, false],
708+
['abacab', 'ab', 'abacabab', 1, true],
709+
['ab', 'ab', 'abacabab', 1, false],
639710
];
640711
}
641712

642713
/**
643714
* @dataProvider provideBeforeAfterLastIgnoreCase
644715
*/
645-
public function testBeforeAfterLastIgnoreCase(string $expected, string $needle, string $origin, bool $before)
716+
public function testBeforeAfterLastIgnoreCase(string $expected, string $needle, string $origin, int $offset, bool $before)
646717
{
647718
$result = static::createFromString($origin)->ignoreCase();
648-
$result = $before ? $result->beforeLast($needle, false) : $result->afterLast($needle, true);
719+
$result = $before ? $result->beforeLast($needle, false, $offset) : $result->afterLast($needle, true, $offset);
649720
$this->assertEquals(static::createFromString($expected), $result);
650721
}
651722

652723
public static function provideBeforeAfterLastIgnoreCase()
653724
{
654725
return [
655-
['', '', 'hello world', true],
656-
['', '', 'hello world', false],
657-
['', 'FOO', 'hello world', true],
658-
['', 'FOO', 'hello world', false],
659-
['hello wor', 'l', 'hello world', true],
660-
['ld', 'l', 'hello world', false],
661-
['hello wor', 'L', 'hello world', true],
662-
['ld', 'L', 'hello world', false],
663-
['hello w', 'O', 'hello world', true],
664-
['orld', 'O', 'hello world', false],
726+
['', '', 'hello world', 0, true],
727+
['', '', 'hello world', 0, false],
728+
['', 'FOO', 'hello world', 0, true],
729+
['', 'FOO', 'hello world', 0, false],
730+
['', 'o', 'hello world', 10, true],
731+
['', 'o', 'hello world', 10, false],
732+
['hello wor', 'l', 'hello world', 0, true],
733+
['ld', 'l', 'hello world', 0, false],
734+
['hello wor', 'L', 'hello world', 0, true],
735+
['ld', 'L', 'hello world', 0, false],
736+
['hello w', 'O', 'hello world', 0, true],
737+
['orld', 'O', 'hello world', 0, false],
738+
['AbacaB', 'Ab', 'AbacaBaB', 1, true],
739+
['aB', 'Ab', 'AbacaBaB', 1, false],
665740
];
666741
}
667742

@@ -729,4 +804,49 @@ public static function provideReplaceIgnoreCase()
729804
['heMMo worMd', 3, 'hello world', 'L', 'M'],
730805
];
731806
}
807+
808+
/**
809+
* @dataProvider provideCamel
810+
*/
811+
public function testCamel(string $expectedString, string $origin)
812+
{
813+
$instance = static::createFromString($origin)->camel();
814+
815+
$this->assertEquals(static::createFromString($expectedString), $instance);
816+
}
817+
818+
public static function provideCamel()
819+
{
820+
return [
821+
['', ''],
822+
['symfonyIsGreat', 'symfony_is_great'],
823+
['symfony5IsGreat', 'symfony_5_is_great'],
824+
['symfonyIsGreat', 'Symfony is great'],
825+
['symfonyIsAGreatFramework', 'Symfony is a great framework'],
826+
['symfonyIsGREAT', '*Symfony* is GREAT!!'],
827+
];
828+
}
829+
830+
/**
831+
* @dataProvider provideSnake
832+
*/
833+
public function testSnake(string $expectedString, string $origin)
834+
{
835+
$instance = static::createFromString($origin)->snake();
836+
837+
$this->assertEquals(static::createFromString($expectedString), $instance);
838+
}
839+
840+
public static function provideSnake()
841+
{
842+
return [
843+
['', ''],
844+
['symfony_is_great', 'symfonyIsGreat'],
845+
['symfony5_is_great', 'symfony5IsGreat'],
846+
['symfony_is_great', 'Symfony is great'],
847+
['symfony_is_a_great_framework', 'symfonyIsAGreatFramework'],
848+
['symfony_is_great', 'symfonyIsGREAT'],
849+
['symfony_is_really_great', 'symfonyIsREALLYGreat'],
850+
];
851+
}
732852
}

0 commit comments

Comments
 (0)