title: Laravel testing with PHPUnit and Mockery - Setting up dependencies on Controller test
body: <p>After finally getting my stupid simple test to pass, I have a feeling that I'm not doing it correctly. </p>
  
  <p>I have a SessionsController, that is responsible for displaying a login page and logging a user in. </p>
  
  <p>I have decided not to use facades so that I wouldn't have to extend Laravel's TestCase and take a performance hit on my unit tests. Therefore, I have injected all the dependencies through the controller, like so -</p>
  
  <p><strong>SessionsController</strong> - Constructor</p>
  
  <pre><code>public function __construct(UserRepositoryInterface $user, 
                              AuthManager $auth, 
                              Redirector $redirect,
                              Environment $view )
  {
      $this-&gt;user = $user;
      $this-&gt;auth = $auth;
      $this-&gt;redirect = $redirect; 
      $this-&gt;view = $view;
  }
  </code></pre>
  
  <p>I have done the necessary declaring of variables and using the namespaces, which I'm not going to include here as its unnecessary. </p>
  
  <p>the create method detects if a user is authorized, if they are then I redirect them to the home page, otherwise they are displayed the login form. </p>
  
  <p><strong>SessionsController</strong> - Create</p>
  
  <pre><code>public function create()
  {
      if ($this-&gt;auth-&gt;user()) return $this-&gt;redirect-&gt;to('/');
  
      return $this-&gt;view-&gt;make('sessions.login');
  }
  </code></pre>
  
  <p>Now for the testing, I'm brand new to it, so bear with me.</p>
  
  <p><strong>SessionsControllerTest</strong></p>
  
  <pre><code>class SessionsControllerTest extends PHPUnit_Framework_TestCase {
  
  
      public function tearDown()
      {
          Mockery::close();
      }
  
      public function test_logged_in_user_cannot_see_login_page()
      {
          # Arrange (Create mocked versions of dependencies)
  
          $user = Mockery::mock('Glenn\Repositories\User\UserRepositoryInterface');
  
          $authorizedUser = Mockery::mock('Illuminate\Auth\AuthManager');
          $authorizedUser-&gt;shouldReceive('user')-&gt;once()-&gt;andReturn(true);
  
          $redirect = Mockery::mock('Illuminate\Routing\Redirector');
          $redirect-&gt;shouldReceive('to')-&gt;once()-&gt;andReturn('redirected to home');
  
          $view = Mockery::mock('Illuminate\View\Environment');
  
  
          # Act (Attempt to go to login page)
  
          $session = new SessionsController($user, $authorizedUser, $redirect, $view);
          $result = $session-&gt;create();
  
          # Assert (Return to home page) 
      }
  }
  </code></pre>
  
  <p>This all passes, but I don't want to have to declare all of these mocked dependencies for each test that I write in my SessionsControllerTest. Is there a way to declare these mocked dependencies once in say a constructor? and then call them by there variables for mocking? </p>