Skip to content

Commit

Permalink
Keep method prophecies with lowecase method name key
Browse files Browse the repository at this point in the history
Technically it is a BC Break, as getMethodProphecies now gives different
result than previously. But as long as PHP class methods are case
insensitive it shouldn't be a problem.

- per @stof
  • Loading branch information
michalbundyra committed Sep 5, 2019
1 parent 343219c commit c091b00
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
31 changes: 27 additions & 4 deletions spec/Prophecy/Prophecy/ObjectProphecySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function its_addMethodProphecy_adds_method_prophecy(
$this->addMethodProphecy($methodProphecy);

$this->getMethodProphecies()->shouldReturn(array(
'getUsername' => array($methodProphecy)
'getusername' => array($methodProphecy)
));
}

Expand All @@ -145,7 +145,30 @@ function its_addMethodProphecy_handles_prophecies_with_different_arguments(
$this->addMethodProphecy($methodProphecy2);

$this->getMethodProphecies()->shouldReturn(array(
'getUsername' => array(
'getusername' => array(
$methodProphecy1,
$methodProphecy2,
)
));
}

function its_addMethodProphecy_handles_prophecies_for_caseinsensitive_method_names(
MethodProphecy $methodProphecy1,
MethodProphecy $methodProphecy2,
ArgumentsWildcard $argumentsWildcard1,
ArgumentsWildcard $argumentsWildcard2
) {
$methodProphecy1->getArgumentsWildcard()->willReturn($argumentsWildcard1);
$methodProphecy1->getMethodName()->willReturn('getUsername');

$methodProphecy2->getArgumentsWildcard()->willReturn($argumentsWildcard2);
$methodProphecy2->getMethodName()->willReturn('getUserName');

$this->addMethodProphecy($methodProphecy1);
$this->addMethodProphecy($methodProphecy2);

$this->getMethodProphecies()->shouldReturn(array(
'getusername' => array(
$methodProphecy1,
$methodProphecy2,
)
Expand All @@ -168,10 +191,10 @@ function its_addMethodProphecy_handles_prophecies_for_different_methods(
$this->addMethodProphecy($methodProphecy2);

$this->getMethodProphecies()->shouldReturn(array(
'getUsername' => array(
'getusername' => array(
$methodProphecy1
),
'isUsername' => array(
'isusername' => array(
$methodProphecy2
)
));
Expand Down
6 changes: 3 additions & 3 deletions src/Prophecy/Call/CallCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ public function makeCall(ObjectProphecy $prophecy, $methodName, array $arguments
*/
public function findCalls($methodName, ArgumentsWildcard $wildcard)
{
$lowerMethodName = strtolower($methodName);
$methodName = strtolower($methodName);

return array_values(
array_filter($this->recordedCalls, function (Call $call) use ($lowerMethodName, $wildcard) {
return $lowerMethodName === strtolower($call->getMethodName())
array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) {
return $methodName === strtolower($call->getMethodName())
&& 0 < $call->getScore($wildcard)
;
})
Expand Down
15 changes: 5 additions & 10 deletions src/Prophecy/Prophecy/ObjectProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function addMethodProphecy(MethodProphecy $methodProphecy)
), $methodProphecy);
}

$methodName = $methodProphecy->getMethodName();
$methodName = strtolower($methodProphecy->getMethodName());

if (!isset($this->methodProphecies[$methodName])) {
$this->methodProphecies[$methodName] = array();
Expand All @@ -168,18 +168,13 @@ public function getMethodProphecies($methodName = null)
return $this->methodProphecies;
}

$lowerMethodName = strtolower($methodName);
$methodName = strtolower($methodName);

$result = array();
foreach ($this->methodProphecies as $name => $prophecies) {
if (strtolower($name) === $lowerMethodName) {
foreach ($prophecies as $prophecy) {
$result[] = $prophecy;
}
}
if (!isset($this->methodProphecies[$methodName])) {
return array();
}

return $result;
return $this->methodProphecies[$methodName];
}

/**
Expand Down

0 comments on commit c091b00

Please sign in to comment.