From a06e40ae49123fe71d78f9d8d94e97826d5ebbae Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Thu, 28 Apr 2016 09:47:06 +0200 Subject: [PATCH] fix tests for php 5.5.35/5.6.21/7.0.6 --- src/Patchwork/Utf8.php | 4 +++- tests/PHP/Shim/IntlTest.php | 2 +- tests/Utf8/HhvmTest.php | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Patchwork/Utf8.php b/src/Patchwork/Utf8.php index 585f063..4363a08 100644 --- a/src/Patchwork/Utf8.php +++ b/src/Patchwork/Utf8.php @@ -235,7 +235,9 @@ public static function strlen($s) } public static function strpos($s, $needle, $offset = 0) { - return grapheme_strpos($s, $needle, $offset); + // ignore invalid negative offset to keep compatility + // with php < 5.5.35, < 5.6.21, < 7.0.6 + return grapheme_strpos($s, $needle, $offset > 0 ? $offset : 0); } public static function strrpos($s, $needle, $offset = 0) { diff --git a/tests/Patchwork/Tests/PHP/Shim/IntlTest.php b/tests/PHP/Shim/IntlTest.php index 1d4d4a7..83b3aa8 100644 --- a/tests/Patchwork/Tests/PHP/Shim/IntlTest.php +++ b/tests/Patchwork/Tests/PHP/Shim/IntlTest.php @@ -122,7 +122,7 @@ public function testGrapheme_strpos() $this->assertSame(false, grapheme_strpos('abc', '')); $this->assertSame(false, grapheme_strpos('abc', 'd')); $this->assertSame(false, grapheme_strpos('abc', 'a', 3)); - $this->assertSame(0, grapheme_strpos('abc', 'a', -1)); + $this->assertSame(0, grapheme_strpos('abc', 'a', 0)); $this->assertSame(1, grapheme_strpos('한국어', '국')); $this->assertSame(3, grapheme_stripos('DÉJÀ', 'à')); $this->assertSame(false, grapheme_strrpos('한국어', '')); diff --git a/tests/Patchwork/Tests/Utf8/HhvmTest.php b/tests/Utf8/HhvmTest.php index 8a77da0..4eb6866 100644 --- a/tests/Patchwork/Tests/Utf8/HhvmTest.php +++ b/tests/Patchwork/Tests/Utf8/HhvmTest.php @@ -12,7 +12,8 @@ public function test1() public function test2() { // Negative offset are not allowed but native PHP silently casts them to zero - $this->assertSame(0, grapheme_strpos('abc', 'a', -1)); + // Starting with 5.5.35, 5.6.21, 7.0.6, PHP refuse them + $this->assertSame(0, grapheme_strpos('abc', 'a', 0)); } public function test3()