Skip to content

Commit 5374081

Browse files
minor #33932 [String] Added test for replaceMatches (honarkhah)
This PR was merged into the 5.0-dev branch. Discussion ---------- [String] Added test for replaceMatches | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Fix #33905 | License | MIT | Doc PR | Added test for String:replaceMatches Commits ------- 1cbbddd4a9 [String] Added test for replaceMatches
2 parents 120f6e8 + 12f9cb6 commit 5374081

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Tests/AbstractAsciiTestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,34 @@ public static function provideReplace()
833833
];
834834
}
835835

836+
/**
837+
* @dataProvider provideReplaceMatches
838+
*/
839+
public function testReplaceMatches(string $expectedString, string $origin, string $fromRegexp, $to)
840+
{
841+
$origin = static::createFromString($origin);
842+
$result = $origin->replaceMatches($fromRegexp, $to);
843+
844+
$this->assertEquals(static::createFromString($expectedString), $result);
845+
}
846+
847+
public static function provideReplaceMatches()
848+
{
849+
return [
850+
['April,15,2003', 'April 15, 2003', '/(\w+) (\d+), (\d+)/i', '${1},$2,$3'],
851+
['5/27/1999', '1999-5-27', '/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/', '\3/\4/\1\2'],
852+
['Copyright 2000', 'Copyright 1999', '([0-9]+)', '2000'],
853+
['hello world! this is a test', 'HELLO WORLD! THIS is a test', '/\b([A-Z]+)\b/', function ($word) {
854+
return strtolower($word[1]);
855+
}],
856+
['COPYRIGHT 1999', 'Copyright 1999', '/[a-z]/', function ($matches) {
857+
foreach ($matches as $match) {
858+
return strtoupper($match);
859+
}
860+
}],
861+
];
862+
}
863+
836864
/**
837865
* @dataProvider provideReplaceIgnoreCase
838866
*/

Tests/AbstractUnicodeTestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,16 @@ public static function provideReplace(): array
439439
);
440440
}
441441

442+
public static function provideReplaceMatches(): array
443+
{
444+
return array_merge(
445+
parent::provideReplaceMatches(),
446+
[
447+
['This is a dj-vu situation!', 'This is a déjà-vu situation!', '/([à-ú])/', ''],
448+
]
449+
);
450+
}
451+
442452
public static function provideReplaceIgnoreCase(): array
443453
{
444454
return array_merge(

0 commit comments

Comments
 (0)