From b839dcece03c707b5cc728e32752d2992ebde215 Mon Sep 17 00:00:00 2001 From: Ben Batschelet Date: Thu, 9 Dec 2021 16:55:09 -0600 Subject: [PATCH 1/2] Use updated PHPUnit regular expression methods by default. Fixes #62 --- src/Verify.php | 12 ++++++++++-- test/VerifyTest.php | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/Verify.php b/src/Verify.php index ae771e4..43460f7 100644 --- a/src/Verify.php +++ b/src/Verify.php @@ -860,9 +860,17 @@ public function matchRegExp(string $expression): self } if ($this->modifierCondition) { - a::assertRegExp($expression, $this->getActualValue(), $this->description); + if (method_exists(a::class, 'assertMatchesRegularExpression')) { + a::assertMatchesRegularExpression($expression, $this->getActualValue(), $this->description); + } else { + a::assertRegExp($expression, $this->getActualValue(), $this->description); + } } else { - a::assertNotRegExp($expression, $this->getActualValue(), $this->description); + if (method_exists(a::class, 'assertDoesNotMatchRegularExpression')) { + a::assertDoesNotMatchRegularExpression($expression, $this->getActualValue(), $this->description); + } else { + a::assertNotRegExp($expression, $this->getActualValue(), $this->description); + } } return $this; diff --git a/test/VerifyTest.php b/test/VerifyTest.php index 1fcee8d..91611de 100644 --- a/test/VerifyTest.php +++ b/test/VerifyTest.php @@ -139,6 +139,17 @@ public function containOnlyAssertMethods(): array ]; } + /** + * Single param methods that have been deprecated. + */ + public function deprecatedMethods(): array + { + return [ + [true, 'matchRegExp', 'assertRegExp'], + [false, 'matchRegExp', 'assertNotRegExp'], + ]; + } + /** * Assert methods used by equalTo(). */ @@ -273,7 +284,7 @@ public function singleParamMethods(): array [true, 'key', 'assertArrayHasKey'], [true, 'startWith', 'assertStringStartsWith'], [true, 'endWith', 'assertStringEndsWith'], - [true, 'matchRegExp', 'assertRegExp'], + [true, 'matchRegExp', 'assertMatchesRegularExpression'], [true, 'matchFormat', 'assertStringMatchesFormat'], [true, 'matchFormatFile', 'assertStringMatchesFormatFile'], [true, 'equalToJsonString', 'assertJsonStringEqualsJsonString'], @@ -293,7 +304,7 @@ public function singleParamMethods(): array [false, 'key', 'assertArrayNotHasKey'], [false, 'startWith', 'assertStringStartsNotWith'], [false, 'endWith', 'assertStringEndsNotWith'], - [false, 'matchRegExp', 'assertNotRegExp'], + [false, 'matchRegExp', 'assertDoesNotMatchRegularExpression'], [false, 'matchFormat', 'assertStringNotMatchesFormat'], [false, 'matchFormatFile', 'assertStringNotMatchesFormatFile'], [false, 'equalToJsonString', 'assertJsonStringNotEqualsJsonString'], @@ -528,6 +539,33 @@ public function testContainOnly(bool $modifierCondition, string $assertMethod) $this->assertSame($this->subject, $this->subject->containOnly('data type')); } + /** + * Test verify methods that take in a single value for comparison. + * + * @param mixed $expectedValue + * + * @dataProvider deprecatedMethods + * @runInSeparateProcess + * + * @return void + */ + public function testDeprecatedMethods( + bool $modifierCondition, + string $verifyMethod, + string $assertMethod, + $expectedValue = 'some value' + ) { + PHPMockery::mock('BeBat\\Verify', 'method_exists')->andReturn(false); + + $this->setModifierCondition($modifierCondition); + + $this->mockAssert->shouldReceive($assertMethod) + ->with($expectedValue, $this->defaultActualValue, 'some message') + ->once(); + + $this->assertSame($this->subject, $this->subject->{$verifyMethod}($expectedValue)); + } + /** * Test Verify::equalTo(). * From 7035ba3bd069465912a934b5cce58a8b71bc169e Mon Sep 17 00:00:00 2001 From: Ben Batschelet Date: Fri, 10 Dec 2021 09:28:52 -0600 Subject: [PATCH 2/2] Tell PHPStan to ignore missing regexp methods in older versions of PHPUnit Issue #62 --- phpstan.phpunit7.neon | 6 ++++++ phpstan.phpunit8.neon | 13 +++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 phpstan.phpunit8.neon diff --git a/phpstan.phpunit7.neon b/phpstan.phpunit7.neon index 389116d..7e39faf 100644 --- a/phpstan.phpunit7.neon +++ b/phpstan.phpunit7.neon @@ -17,6 +17,12 @@ parameters: - message: '/Call to an undefined static method .+Assert::assertStringNotEqualsFileIgnoringCase/' path: %currentWorkingDirectory%/src/Verify.php + - + message: '/Call to an undefined static method .+Assert::assertMatchesRegularExpression/' + path: %currentWorkingDirectory%/src/Verify.php + - + message: '/Call to an undefined static method .+Assert::assertDoesNotMatchRegularExpression/' + path: %currentWorkingDirectory%/src/Verify.php - message: '/Call to an undefined static method .+Assert::assertFileEqualsIgnoringCase/' path: %currentWorkingDirectory%/src/VerifyFile.php diff --git a/phpstan.phpunit8.neon b/phpstan.phpunit8.neon new file mode 100644 index 0000000..29615d6 --- /dev/null +++ b/phpstan.phpunit8.neon @@ -0,0 +1,13 @@ +# Ignorable errors specific to PHPUnit 8 + +includes: + - phpstan.neon.dist + +parameters: + ignoreErrors: + - + message: '/Call to an undefined static method .+Assert::assertMatchesRegularExpression/' + path: %currentWorkingDirectory%/src/Verify.php + - + message: '/Call to an undefined static method .+Assert::assertDoesNotMatchRegularExpression/' + path: %currentWorkingDirectory%/src/Verify.php