diff --git a/tests/Unit/Utilities/PregTest.php b/tests/Unit/Utilities/PregTest.php index bd62c0b6..b23377ff 100644 --- a/tests/Unit/Utilities/PregTest.php +++ b/tests/Unit/Utilities/PregTest.php @@ -182,6 +182,46 @@ public function replaceReplaces($pattern, $replacement, string $subject, int $li self::assertSame($expectedResult, $result); } + /** + * @test + */ + public function replaceSetsCount(): void + { + $subject = new Preg(); + + $subject->replace('/a/', 'fab', 'abba', -1, $count); + + self::assertSame(2, $count); + } + + /** + * @test + */ + public function replaceReturnsSubjectOnError(): void + { + $subject = new Preg(); + + $result = @$subject->replace('/', 'fab', 'abba'); + + self::assertSame('abba', $result); + } + + /** + * @param array $matches + */ + private function callbackForReplaceCallback(array $matches): string + { + if (\is_array($this->replaceCallbackReplacement)) { + if ($matches[0] !== $this->lastReplaceCallbackMatch) { + ++$this->replaceCallbackReplacementIndex; + $this->lastReplaceCallbackMatch = $matches[0]; + } + return $this->replaceCallbackReplacement[$this->replaceCallbackReplacementIndex]; + } else { + return $this->replaceCallbackReplacement; + } + } + /** * @test * @@ -215,11 +255,19 @@ public function replaceCallbackReplaces( /** * @test */ - public function replaceSetsCount(): void + public function replaceCallbackSetsCount(): void { $subject = new Preg(); - $subject->replace('/a/', 'fab', 'abba', -1, $count); + $subject->replaceCallback( + '/a/', + static function (array $matches): string { + return 'fab'; + }, + 'abba', + -1, + $count + ); self::assertSame(2, $count); } @@ -227,21 +275,19 @@ public function replaceSetsCount(): void /** * @test */ - public function replaceCallbackSetsCount(): void + public function replaceCallbackReturnsSubjectOnError(): void { $subject = new Preg(); - $subject->replaceCallback( - '/a/', + $result = @$subject->replaceCallback( + '/', static function (array $matches): string { return 'fab'; }, - 'abba', - -1, - $count + 'abba' ); - self::assertSame(2, $count); + self::assertSame('abba', $result); } /** @@ -298,6 +344,31 @@ public function splitSplits(string $pattern, string $subject, int $limit, int $f self::assertSame($expectedResult, $result); } + /** + * @test + */ + public function splitReturnsArrayContainingSubjectOnError(): void + { + $subject = new Preg(); + + $result = @$subject->split('/', 'abba'); + + self::assertSame(['abba'], $result); + } + + /** + * @test + */ + public function splitWithOffsetCaptureIsNotSupported(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(1726506348); + $this->expectExceptionMessage('PREG_SPLIT_OFFSET_CAPTURE'); + $subject = new Preg(); + + $result = $subject->split('/a/', 'abba', -1, PREG_SPLIT_OFFSET_CAPTURE); + } + /** * @return arrayreplace('/', 'fab', 'abba'); - - self::assertSame('abba', $result); - } - - /** - * @test - */ - public function replaceCallbackReturnsSubjectOnError(): void - { - $subject = new Preg(); - - $result = @$subject->replaceCallback( - '/', - static function (array $matches): string { - return 'fab'; - }, - 'abba' - ); - - self::assertSame('abba', $result); - } - - /** - * @test - */ - public function splitReturnsArrayContainingSubjectOnError(): void - { - $subject = new Preg(); - - $result = @$subject->split('/', 'abba'); - - self::assertSame(['abba'], $result); - } - - /** - * @test - */ - public function splitWithOffsetCaptureIsNotSupported(): void - { - $this->expectException(\RuntimeException::class); - $this->expectExceptionCode(1726506348); - $this->expectExceptionMessage('PREG_SPLIT_OFFSET_CAPTURE'); - $subject = new Preg(); - - $result = $subject->split('/a/', 'abba', -1, PREG_SPLIT_OFFSET_CAPTURE); - } - /** * @test */ @@ -460,20 +476,4 @@ public function matchSetsMatchesToEmptyArrayOnError(): void self::assertSame([], $matches); } - - /** - * @param array $matches - */ - private function callbackForReplaceCallback(array $matches): string - { - if (\is_array($this->replaceCallbackReplacement)) { - if ($matches[0] !== $this->lastReplaceCallbackMatch) { - ++$this->replaceCallbackReplacementIndex; - $this->lastReplaceCallbackMatch = $matches[0]; - } - return $this->replaceCallbackReplacement[$this->replaceCallbackReplacementIndex]; - } else { - return $this->replaceCallbackReplacement; - } - } }