From da3ca14041c91fbe41c2bf8a4c86daf5c1086c55 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 6 Sep 2019 16:14:46 +0200 Subject: [PATCH] Do not use generic IsEqual constraint for method name matching for https://github.com/sebastianbergmann/phpunit/issues/3745#issuecomment-528857616 --- .../MockObject/Matcher/MethodName.php | 10 +---- .../MockObject/MethodNameConstraint.php | 45 +++++++++++++++++++ .../Framework/MockObject/MockObjectTest.php | 10 ++--- 3 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 src/Framework/MockObject/MethodNameConstraint.php diff --git a/src/Framework/MockObject/Matcher/MethodName.php b/src/Framework/MockObject/Matcher/MethodName.php index bf6284685ae..11fe2c0c0bf 100644 --- a/src/Framework/MockObject/Matcher/MethodName.php +++ b/src/Framework/MockObject/Matcher/MethodName.php @@ -10,9 +10,9 @@ namespace PHPUnit\Framework\MockObject\Matcher; use PHPUnit\Framework\Constraint\Constraint; -use PHPUnit\Framework\Constraint\IsEqual; use PHPUnit\Framework\InvalidArgumentException; use PHPUnit\Framework\MockObject\Invocation as BaseInvocation; +use PHPUnit\Framework\MockObject\MethodNameConstraint; /** * @internal This class is not covered by the backward compatibility promise for PHPUnit @@ -37,13 +37,7 @@ public function __construct($constraint) throw InvalidArgumentException::create(1, 'string'); } - $constraint = new IsEqual( - $constraint, - 0, - 10, - false, - true - ); + $constraint = new MethodNameConstraint($constraint); } $this->constraint = $constraint; diff --git a/src/Framework/MockObject/MethodNameConstraint.php b/src/Framework/MockObject/MethodNameConstraint.php new file mode 100644 index 00000000000..fa187f7ad39 --- /dev/null +++ b/src/Framework/MockObject/MethodNameConstraint.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\Constraint\Constraint; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class MethodNameConstraint extends Constraint +{ + /** + * @var string + */ + private $methodName; + + public function __construct(string $methodName) + { + $this->methodName = \strtolower($methodName); + } + + public function toString(): string + { + return \sprintf( + 'is "%s"', + $this->methodName + ); + } + + protected function matches($other): bool + { + if (!\is_string($other)) { + return false; + } + + return $this->methodName === \strtolower($other); + } +} diff --git a/tests/unit/Framework/MockObject/MockObjectTest.php b/tests/unit/Framework/MockObject/MockObjectTest.php index 25deb416b0b..0c971f185e6 100644 --- a/tests/unit/Framework/MockObject/MockObjectTest.php +++ b/tests/unit/Framework/MockObject/MockObjectTest.php @@ -670,7 +670,7 @@ public function testVerificationOfMethodNameFailsWithoutParameters(): void $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is equal to 'right' when invoked 1 time(s).\n" . + "Expectation failed for method name is \"right\" when invoked 1 time(s).\n" . 'Method was expected to be called 1 times, actually called 0 times.' . "\n", $e->getMessage() ); @@ -695,7 +695,7 @@ public function testVerificationOfMethodNameFailsWithParameters(): void $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is equal to 'right' when invoked 1 time(s).\n" . + "Expectation failed for method name is \"right\" when invoked 1 time(s).\n" . 'Method was expected to be called 1 times, actually called 0 times.' . "\n", $e->getMessage() ); @@ -718,7 +718,7 @@ public function testVerificationOfMethodNameFailsWithWrongParameters(): void $mock->right(['second']); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is equal to 'right' when invoked 1 time(s)\n" . + "Expectation failed for method name is \"right\" when invoked 1 time(s)\n" . 'Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.' . "\n" . 'Failed asserting that two arrays are equal.', $e->getMessage() @@ -732,7 +732,7 @@ public function testVerificationOfMethodNameFailsWithWrongParameters(): void // $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is equal to 'right' when invoked 1 time(s).\n" . + "Expectation failed for method name is \"right\" when invoked 1 time(s).\n" . 'Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.' . "\n" . 'Failed asserting that two arrays are equal.' . "\n" . '--- Expected' . "\n" . @@ -811,7 +811,7 @@ public function testWithAnythingInsteadOfWithAnyParameters(): void $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is equal to 'right' when invoked 1 time(s)\n" . + "Expectation failed for method name is \"right\" when invoked 1 time(s)\n" . 'Parameter count for invocation SomeClass::right() is too low.' . "\n" . 'To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.', $e->getMessage()