title: How to mock LDAP Laravel auth for unit testing
body: <p>In my Laravel project I use <code>Ldap-connector</code> <a href="https://github.com/SaschaDens/ldap-connector" rel="noreferrer">package</a> to authenticate users against LDAP</p>
  
  <p>Auth works fine out of the box.</p>
  
  <p>In /app/providers/AuthServiceProvider.php I have a policy defined to manage LDAP user access, for example: </p>
  
  <pre><code>public function boot(GateContract $gate)
  {
      $this-&gt;registerPolicies($gate);
  
      $gate-&gt;define('rewards', function ($user) {
          return ($user-&gt;getAdLDAP()-&gt;inGroup('Admin') || $user-&gt;getAdLDAP()-&gt;inGroup('Marketing')) ? true : false;
      });
      // other policies 
      // ...
  }
  </code></pre>
  
  <p>In the controller I check the policy for logged in user like:</p>
  
  <pre><code>class RewardController extends Controller {
      public function __construct($handler = null) {
          $this-&gt;authorize('rewards');
      }
      // other methods
      // ...
  }
  </code></pre>
  
  <p>Everything works fine and if logged in user doesn't have <code>Marketing</code> or <code>Admin</code> group, the controller will throw <code>403</code> exception.</p>
  
  <p>Now, for my <code>phpunit</code> tests I need to mock LDAP auth and provide the access to the controller to tests it's methods, otherwise policy throws me <code>This action is unauthorized.</code> error</p>
  
  <p>Since ldap auth driver is implemented I don't think user model <code>App/User</code> is used and I can't just have it done with <code>$this-&gt;actingAs($user)</code> from Laravel docs from <a href="https://laravel.com/docs/5.2/testing#sessions-and-authentication" rel="noreferrer">here</a></p>