We are experimenting a strange behaviour of Mockery (0.9.2) while tdd-ing a Symfony controller which makes use of several request parameters grabbed using the Request service. We are using PHPUnit (3.7) as testing framework.
The way we approach TDD is using setUp method for creating mocks and configuring them by using byDefault() so they can provide a neutral happy-flow scenario. Then, in each test method, we become specific about our expectations about mock behaviour.
I isolated the problem in a proof of concept test just for making the analysis easier. Here we go.
This is the test class itself:
class FooTest extends \PHPUnit_Framework_TestCase
{
private $request;
public function setUp()
{
$this->request = \Mockery::mock('Symfony\Component\HttpFoundation\Request');
$this->request->shouldReceive('get')->with('a')->andReturnNull()->byDefault();
$this->request->shouldReceive('get')->with('b')->andReturnNull()->byDefault();
}
public function test_bar_checks_request_a_parameter()
{
$this->request->shouldReceive('get')->with('a')->andReturn('a')->once();
$foo = new Foo($this->request);
$foo->bar();
}
}
And this is the tested class:
use Symfony\Component\HttpFoundation\Request;
class Foo
{
private $request;
function __construct(Request $request)
{
$this->request = $request;
}
public function bar()
{
$a = $this->request->get('a');
$b = $this->request->get('b');
}
}
In the test test_bar_checks_request_a_parameter I would expect bar() method to get 'a' when calling get('a') on the request mock while getting null when calling get('b') instead.
But, instead, we are getting this error:
No matching handler found for Mockery_0_Symfony_Component_HttpFoundation_Request::get("b").
Which seems to say that the Request mock forgot the setUp we made for get('b') call
shouldReceive('get')->with('b')->andReturnNull()->byDefault()
Is this a Mockery limitation? Is a bad approach on our side, a test smell maybe?
Thanks in advance