I'm starting out with some TDD in Laravel 4. While I understand the basics of dependency injection, I can't seem to get my head around how to mock the Auth functionality. Below is my current user controller with just the index method and it's applicable test. The current setup keeps throwing me errors when I run phpunit, namely "undefined index" errors for Auth. Is there a better way of doing this?
My Controller:
class UserController extends \BaseController {
/**
* User instance
*
* @var User
*/
protected $user;
/**
* The master layout that View's will be built upon.
*
* @var string
*/
protected $layout = 'layout.user-master';
/**
* Constructor
*
* @param User $user
* @param View $view
*/
public function __construct(User $user)
{
$this->user = $user;
// Filters
$this->beforeFilter('csrf', array('on' => 'post'));
$this->beforeFilter('auth', array('except' => array('create', 'store')));
}
/**
* Display a listing of the user.
*
* @return Response
*/
public function index()
{
$id = Auth::user()->id;
$user = $this->user->find($id);
$this->layout->content = View::make('user.index', compact('user'));
}
}
User Controller Test
<?php
use \Mockery;
class UserControllerTest extends TestCase {
public function __construct()
{
// Mock an Eloquent User Model instance
$this->mock = Mockery::mock('Eloquent', 'User');
}
public function tearDown()
{
Mockery::close();
}
public function testIndex()
{
Auth::shouldReceive('user')->once()->andReturn(Mockery::any());
$this->mock
->shouldReceive('find')
->once()
->with(Mockery::any())
->andReturn('foo');
$this->app->instance('User', $this->mock);
$this->call('GET', 'user');
$this->assertViewHas('user');
}
}