Skip to content

Commit

Permalink
Fix endsWith for multibyte suffix (webmozarts#79)
Browse files Browse the repository at this point in the history
* Fix `endsWith` for multibyte suffix
* Add tests to prove correct behaviour
  • Loading branch information
guilliamxavier authored and BackEndTea committed Jun 28, 2019
1 parent 80778ec commit 4950625
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public static function startsWithLetter($value, $message = '')

public static function endsWith($value, $suffix, $message = '')
{
if ($suffix !== substr($value, -static::strlen($suffix))) {
if ($suffix !== substr($value, -strlen($suffix))) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected a value to end with %2$s. Got: %s',
static::valueToString($value),
Expand Down
48 changes: 48 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,41 @@ public function getTests()
array('contains', array('abcd', 'cd'), true),
array('contains', array('abcd', 'de'), false),
array('contains', array('', 'de'), false),
array('contains', array('äþçð', 'äþ'), true),
array('contains', array('äþçð', 'þç'), true),
array('contains', array('äþçð', 'çð'), true),
array('contains', array('äþçð', 'ðé'), false),
array('contains', array('', 'ðé'), false),
array('contains', array('あいうえ', 'あい'), true),
array('contains', array('あいうえ', 'いう'), true),
array('contains', array('あいうえ', 'うえ'), true),
array('contains', array('あいうえ', 'えお'), false),
array('contains', array('', 'えお'), false),
array('contains', array('😄😑☹️', '😄'), true),
array('contains', array('😄😑☹️', '😑'), true),
array('contains', array('😄😑☹️', '☹️'), true),
array('contains', array('😄😑☹️', '😄☹️'), false),
array('contains', array('', '😑'), false),
array('notContains', array('abcd', 'ab'), false),
array('notContains', array('abcd', 'bc'), false),
array('notContains', array('abcd', 'cd'), false),
array('notContains', array('abcd', 'de'), true),
array('notContains', array('', 'de'), true),
array('notContains', array('äþçð', 'äþ'), false),
array('notContains', array('äþçð', 'þç'), false),
array('notContains', array('äþçð', 'çð'), false),
array('notContains', array('äþçð', 'ðé'), true),
array('notContains', array('', 'ðé'), true),
array('notContains', array('あいうえ', 'あい'), false),
array('notContains', array('あいうえ', 'いう'), false),
array('notContains', array('あいうえ', 'うえ'), false),
array('notContains', array('あいうえ', 'えお'), true),
array('notContains', array('', 'えお'), true),
array('notContains', array('😄😑☹️', '😄'), false),
array('notContains', array('😄😑☹️', '😑'), false),
array('notContains', array('😄😑☹️', '☹️'), false),
array('notContains', array('😄😑☹️', '😄☹️'), true),
array('notContains', array('', '😑'), true),
array('notWhitespaceOnly', array('abc'), true),
array('notWhitespaceOnly', array('123'), true),
array('notWhitespaceOnly', array(' abc '), true),
Expand All @@ -225,6 +255,15 @@ public function getTests()
array('startsWith', array('abcd', 'ab'), true),
array('startsWith', array('abcd', 'bc'), false),
array('startsWith', array('', 'bc'), false),
array('startsWith', array('äþçð', 'äþ'), true),
array('startsWith', array('äþçð', 'þç'), false),
array('startsWith', array('', 'þç'), false),
array('startsWith', array('あいうえ', 'あい'), true),
array('startsWith', array('あいうえ', 'いう'), false),
array('startsWith', array('', 'いう'), false),
array('startsWith', array('😄😑☹️', '😄'), true),
array('startsWith', array('😄😑☹️', '😑'), false),
array('startsWith', array('', '😑'), false),
array('startsWithLetter', array('abcd'), true),
array('startsWithLetter', array('a'), true),
array('startsWithLetter', array('a1'), true),
Expand All @@ -234,6 +273,15 @@ public function getTests()
array('endsWith', array('abcd', 'cd'), true),
array('endsWith', array('abcd', 'bc'), false),
array('endsWith', array('', 'bc'), false),
array('endsWith', array('äþçð', 'çð'), true),
array('endsWith', array('äþçð', 'þç'), false),
array('endsWith', array('', 'þç'), false),
array('endsWith', array('あいうえ', 'うえ'), true),
array('endsWith', array('あいうえ', 'いう'), false),
array('endsWith', array('', 'いう'), false),
array('endsWith', array('😄😑☹️', '☹️'), true),
array('endsWith', array('😄😑☹️', '😑'), false),
array('endsWith', array('', '😑'), false),
array('regex', array('abcd', '~^ab~'), true),
array('regex', array('abcd', '~^bc~'), false),
array('regex', array('', '~^bc~'), false),
Expand Down

0 comments on commit 4950625

Please sign in to comment.