Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disable spy behavior #509

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions spec/Prophecy/Call/CallCenterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ function it_executes_promise_of_method_prophecy_that_matches_with_highest_score_
->shouldReturn('second');
}

function createWithFailFast(bool $failFast)
{
$callCenter = new self();
$callCenter->makeUnexpectedCallsFailFast($failFast);

return $callCenter;
}

function it_throws_exception_if_call_does_not_match_any_of_defined_method_prophecies_and_fail_fast_is_enabled(
$objectProphecy,
MethodProphecy $method,
ArgumentsWildcard $arguments
) {
$method->getMethodName()->willReturn('getName');
$method->getArgumentsWildcard()->willReturn($arguments);
$arguments->scoreArguments(array('world', 'everything'))->willReturn(false);
$arguments->__toString()->willReturn('arg1, arg2');

$objectProphecy->getMethodProphecies()->willReturn(array('method1' => array($method)));
$objectProphecy->getMethodProphecies('getName')->willReturn(array($method));

$this->makeUnexpectedCallsFailFast(true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test that got deleted in #120, I tried to revert it back but this line doesn't seem to work; I need to call that method as a new precondition to make it pass, how should I do it?


$this->shouldThrow('Prophecy\Exception\Call\UnexpectedCallException')
->duringMakeCall($objectProphecy, 'getName', array('world', 'everything'));
}

function it_returns_null_if_method_prophecy_that_matches_makeCall_arguments_has_no_promise(
$objectProphecy,
MethodProphecy $method,
Expand Down
10 changes: 10 additions & 0 deletions spec/Prophecy/Prophecy/ObjectProphecySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ function it_throws_UnexpectedCallException_during_checkPredictions_if_unexpected
$this->shouldThrow('Prophecy\Exception\Call\UnexpectedCallException')
->duringCheckProphecyMethodsPredictions();
}

function it_makes_CallCenter_fail_fast_if_spy_behaviour_is_disabled($lazyDouble, CallCenter $callCenter)
{
$this->beConstructedWith($lazyDouble, $callCenter);

$callCenter->makeUnexpectedCallsFailFast(true)
->shouldBeCalled();

$this->enableSpyBehavior(false);
}
}

class ObjectProphecySpecFixtureA
Expand Down
17 changes: 16 additions & 1 deletion src/Prophecy/Call/CallCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class CallCenter
{
private $util;

/**
* @var bool
*/
private $failFast = false;

/**
* @var Call[]
*/
Expand Down Expand Up @@ -81,7 +86,12 @@ public function makeCall(ObjectProphecy $prophecy, $methodName, array $arguments
// There are method prophecies, so it's a fake/stub. Searching prophecy for this call
$matches = $this->findMethodProphecies($prophecy, $methodName, $arguments);

// If fake/stub doesn't have method prophecy for this call - throw exception
// If fake/stub doesn't have method prophecy for this call - throw exception to fail fast
if ($this->failFast && !count($matches)) {
$this->createUnexpectedCallException($prophecy, $methodName, $arguments);
}

// If fake/stub doesn't have method prophecy for this call - record unexpected call and fail on checkPredictions
if (!count($matches)) {
$this->unexpectedCalls->attach(new Call($methodName, $arguments, null, null, $file, $line), $prophecy);
$this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line);
Expand Down Expand Up @@ -145,6 +155,11 @@ public function findCalls($methodName, ArgumentsWildcard $wildcard)
);
}

public function makeUnexpectedCallsFailFast(bool $failFast): void
{
$this->failFast = $failFast;
}

/**
* @throws UnexpectedCallException
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Prophecy/Prophecy/ObjectProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ public function reveal()
return $double;
}

/**
* Disables the possibility of using this as a spy, making unexpected calls fail immediately
*
* @param bool $enable
*/
public function enableSpyBehavior(bool $enable = true): void
{
$this->callCenter->makeUnexpectedCallsFailFast(! $enable);
}

/**
* Adds method prophecy to object prophecy.
*
Expand Down