Skip to content

Commit

Permalink
Do not use generic IsEqual constraint for method name matching for #3…
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 6, 2019
1 parent 65ac743 commit da3ca14
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
10 changes: 2 additions & 8 deletions src/Framework/MockObject/Matcher/MethodName.php
Expand Up @@ -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
Expand All @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions src/Framework/MockObject/MethodNameConstraint.php
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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);
}
}
10 changes: 5 additions & 5 deletions tests/unit/Framework/MockObject/MockObjectTest.php
Expand Up @@ -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()
);
Expand All @@ -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()
);
Expand All @@ -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()
Expand All @@ -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" .
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit da3ca14

Please sign in to comment.