Question

I try to test my controller actions in laravel with mockery. I already read this tutorial here:

http://culttt.com/2013/07/15/how-to-structure-testable-controllers-in-laravel-4/

I use DI in my constructor like this:

public function __construct(User $user, Cartalyst\Sentry\Sentry $sentry)
{
    $this->user = $user;
    $this->sentry = $sentry;

    ...

}

My problem is the following code in my Controller:

public function getShow($id)
{
    try
    {
        // this is a problem, because I dont know how to tell mockery, to mock the
        // Userprovider
        $user = $this->sentry->getUserProvider()->findById($id);
        // this is not a problem for me
        $this->user->all();

        ...

I am trying to work with Mockery as a mock framework. My question is how to mock a call like $this->sentry->getUserProvider() (Cartalyst Sentry is a advanced authorization bundle). To mock the User Model i write:

$this->user = Mockery::mock('Eloquent', 'User');

Any idea how to mock the Userprovider or should I handle this in another way ? I want to test my UserController if I am getting the user details depending on the id.

Was it helpful?

Solution

You can stub the getUserProvider method to return another stub, e.g.

$sentry = m::mock("F\Q\C\N\Sentry");
$userProvider = m::mock("F\Q\C\N\UserProvider");
$sentry->shouldReceive("getUserProvider")->andReturn($userProvider)->byDefault();

$userProvider->shouldReceive("findById")->andReturn(new User);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top