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

Using throwException inside returnValueMap doesn't work #974

Closed
MTGap opened this issue Jul 29, 2013 · 4 comments
Closed

Using throwException inside returnValueMap doesn't work #974

MTGap opened this issue Jul 29, 2013 · 4 comments

Comments

@MTGap
Copy link

@MTGap MTGap commented Jul 29, 2013

I'm trying to use mocks to throw an exception if a method is called with specific arguments. It doesn't work when using a returnValueMap. However, it works fine when I call $this->throwException inside will()

Here's an example of this in code: https://gist.github.com/MTGap/6107547
And the result:

There was 1 failure:

  1. ThrowException::testCatchingExceptionWithMap
    Failed asserting that false is true.
@whatthejeff

This comment has been minimized.

Copy link
Contributor

@whatthejeff whatthejeff commented Jul 30, 2013

This is expected behavior. PHPUnit_Framework_TestCase::returnValueMap() does not do the same thing as PHPUnit_Framework_MockObject_Builder_InvocationMocker::will(). The returnValueMap() method will not throw exceptions–it only configures specific return values for specific inputs.

When you do:

$thrower = $this->getMock('Thrower');
$map = array(
    array(true, $this->throwException(new Exception())),
);
$thrower->expects($this->once())
        ->method('test')
        ->will($this->returnValueMap($map));

You are configuring your mock to return an instance of PHPUnit_Framework_MockObject_Stub_Exception when $thrower->test(true) is called.

@batusa

This comment has been minimized.

Copy link

@batusa batusa commented Nov 2, 2015

would be nice if we had a method that would allow us to check for exceptions based on certain arguments. @whatthejeff will we have something like that in the near future?

@elboletaire

This comment has been minimized.

Copy link

@elboletaire elboletaire commented Nov 14, 2016

@whatthejeff will we have something like that in the near future?

Or a workaround for this?

@lezhnev74

This comment has been minimized.

Copy link

@lezhnev74 lezhnev74 commented Nov 29, 2016

@elboletaire, @batusa, I believe you can set callback to decide which values to return:

$callback = function($arg) {
            if($arg == "A") {
                return "Good response"
            }
            if($arg == "B") {
                throw new Exception("Bad response");
            }
};
$stub = $this->createMock(Some::class);
$stub->method('MOckingMethod')->will($this->returnCallback($callback));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.