Skip to content

Commit

Permalink
Merge pull request #63 from bbatsche/fix-regexp-deprecation
Browse files Browse the repository at this point in the history
Update PHPUnit RegExp Methods
  • Loading branch information
bbatsche authored Dec 10, 2021
2 parents 6b4f1dc + 7035ba3 commit e7aa4de
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
6 changes: 6 additions & 0 deletions phpstan.phpunit7.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions phpstan.phpunit8.neon
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions src/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 40 additions & 2 deletions test/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand Down Expand Up @@ -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'],
Expand All @@ -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'],
Expand Down Expand Up @@ -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().
*
Expand Down

0 comments on commit e7aa4de

Please sign in to comment.