Skip to content

Commit 189a0bb

Browse files
[12.x] Add test coverage for Str::replaceMatches method (#54930)
* Add tests for Str::replaceMatches with string replacements This commit adds tests for the basic functionality of the replaceMatches method using string replacements and array of patterns. * Complete Str::replaceMatches tests with limit parameter tests This commit finalizes the test coverage for the replaceMatches method by adding tests for the limit parameter, which controls the maximum number of replacements to be performed.
1 parent 8fd1ef1 commit 189a0bb

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/Support/SupportStrTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,38 @@ public function testChopEnd()
17581758
$this->assertSame($expected, Str::chopEnd($subject, $needle));
17591759
}
17601760
}
1761+
1762+
public function testReplaceMatches()
1763+
{
1764+
// Test basic string replacement
1765+
$this->assertSame('foo bar bar', Str::replaceMatches('/baz/', 'bar', 'foo baz bar'));
1766+
$this->assertSame('foo baz baz', Str::replaceMatches('/404/', 'found', 'foo baz baz'));
1767+
1768+
// Test with array of patterns
1769+
$this->assertSame('foo XXX YYY', Str::replaceMatches(['/bar/', '/baz/'], ['XXX', 'YYY'], 'foo bar baz'));
1770+
1771+
// Test with callback
1772+
$result = Str::replaceMatches('/ba(.)/', function ($match) {
1773+
return 'ba'.strtoupper($match[1]);
1774+
}, 'foo baz bar');
1775+
1776+
$this->assertSame('foo baZ baR', $result);
1777+
1778+
$result = Str::replaceMatches('/(\d+)/', function ($match) {
1779+
return $match[1] * 2;
1780+
}, 'foo 123 bar 456');
1781+
1782+
$this->assertSame('foo 246 bar 912', $result);
1783+
1784+
// Test with limit parameter
1785+
$this->assertSame('foo baz baz', Str::replaceMatches('/ba(.)/', 'ba$1', 'foo baz baz', 1));
1786+
1787+
$result = Str::replaceMatches('/ba(.)/', function ($match) {
1788+
return 'ba'.strtoupper($match[1]);
1789+
}, 'foo baz baz bar', 1);
1790+
1791+
$this->assertSame('foo baZ baz bar', $result);
1792+
}
17611793
}
17621794

17631795
class StringableObjectStub

0 commit comments

Comments
 (0)