Skip to content

Commit

Permalink
Fix strlen for fullwidth characters (webmozarts#78)
Browse files Browse the repository at this point in the history
Affected assertions:

- length
- minLength
- maxLength
- lengthBetween

Closes webmozarts#55
  • Loading branch information
guilliamxavier authored and BackEndTea committed Jun 24, 2019
1 parent 08ed54d commit 2bff8aa
Show file tree
Hide file tree
Showing 2 changed files with 15 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 @@ -1194,7 +1194,7 @@ protected static function strlen($value)
return strlen($value);
}

return mb_strwidth($value, $encoding);
return mb_strlen($value, $encoding);
}

protected static function reportInvalidArgument($message)
Expand Down
14 changes: 14 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,27 @@ public function getTests()
array('length', array('äbcd', 4), true, true),
array('length', array('äbc', 4), false, true),
array('length', array('äbcde', 4), false, true),
array('length', array('あbcd', 4), true, true), // 'HIRAGANA LETTER A' (U+3042)
array('length', array('あbc', 4), false, true),
array('length', array('あbcde', 4), false, true),
array('minLength', array('abcd', 4), true),
array('minLength', array('abcde', 4), true),
array('minLength', array('abc', 4), false),
array('minLength', array('äbcd', 4), true, true),
array('minLength', array('äbcde', 4), true, true),
array('minLength', array('äbc', 4), false, true),
array('minLength', array('あbcd', 4), true, true),
array('minLength', array('あbcde', 4), true, true),
array('minLength', array('あbc', 4), false, true),
array('maxLength', array('abcd', 4), true),
array('maxLength', array('abc', 4), true),
array('maxLength', array('abcde', 4), false),
array('maxLength', array('äbcd', 4), true, true),
array('maxLength', array('äbc', 4), true, true),
array('maxLength', array('äbcde', 4), false, true),
array('maxLength', array('あbcd', 4), true, true),
array('maxLength', array('あbc', 4), true, true),
array('maxLength', array('あbcde', 4), false, true),
array('lengthBetween', array('abcd', 3, 5), true),
array('lengthBetween', array('abc', 3, 5), true),
array('lengthBetween', array('abcde', 3, 5), true),
Expand All @@ -285,6 +294,11 @@ public function getTests()
array('lengthBetween', array('äbcde', 3, 5), true, true),
array('lengthBetween', array('äb', 3, 5), false, true),
array('lengthBetween', array('äbcdef', 3, 5), false, true),
array('lengthBetween', array('あbcd', 3, 5), true, true),
array('lengthBetween', array('あbc', 3, 5), true, true),
array('lengthBetween', array('あbcde', 3, 5), true, true),
array('lengthBetween', array('あb', 3, 5), false, true),
array('lengthBetween', array('あbcdef', 3, 5), false, true),
array('fileExists', array(__FILE__), true),
array('fileExists', array(__DIR__), true),
array('fileExists', array(__DIR__.'/foobar'), false),
Expand Down

0 comments on commit 2bff8aa

Please sign in to comment.