title: Write tests in phpunit for Laravel json API
body: <p>I have trouble in create tests for my API in laravel. Here how look my routes.php:</p>
  
  <pre><code>Route::group(['middleware' =&gt;'api', 'prefix' =&gt; '/api/v1', 'namespace' =&gt; 'Api\V1'], function () {
  
  
      Route::post('/login', 'Auth\AuthController@postLogin');
  
      Route::group(['middleware' =&gt;'jwt.auth'], function () {
          Route::post('/projects', 'ProjectsController@postProjects');
          ...
      });
  
  });
  </code></pre>
  
  <p>My <code>ProjectsController@postProjects</code> looks like this (I test it by POSTman and it works fine):</p>
  
  <pre><code>public function postProjects() {
      $project = new Project;
      $project-&gt;fill(request()-&gt;all());
      $project-&gt;user_id = Auth::user()-&gt;id;
      $project-&gt;save();
      return ['project_id' =&gt; $project-&gt;id];
  }
  </code></pre>
  
  <p>I write my own middelware for authentication using JWT cookie which is described in details <a href="https://stackoverflow.com/questions/38415851/how-to-make-jwt-cookie-authentication-in-laravel/38416675#38416675">here</a>. But in my test, I try to turn off middelware (<code>use WithoutMiddleware</code>)and act as logged user (<code>actingAs($user)</code> - I have user (id=1) in my DB). My test looks like that (I wrote it using <a href="https://laravel.com/docs/5.2/testing#testing-json-apis" rel="nofollow noreferrer">this documentation</a>):</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Foundation\Testing\WithoutMiddleware;
  use Illuminate\Foundation\Testing\DatabaseMigrations;
  use Illuminate\Foundation\Testing\DatabaseTransactions;
  use App\Models\User;
  
  class ProjectsTest extends TestCase
  {
      use WithoutMiddleware;
  
      /**
       * A basic functional test example.
       *
       * @return void
       */
      public function testBasicExample()
      {
  
          $user = User::find(1);
  
          $this-&gt;actingAs($user)
                  -&gt;json('POST', '/projects', ['name' =&gt; 'Project_test_name' ])
                  -&gt;seeJson([
                   'project_id' =&gt; 1,
               ]); // THIS IS LINE 26
      }
  }
  </code></pre>
  
  <p>But I get the following error during execution of <code>vendor/bin/phpunit</code>:</p>
  
  <blockquote>
    <p>Time: 182 ms, Memory: 13.75MB</p>
    
    <p>There was 1 error:</p>
    
    <p>1) ProjectsTest::testBasicExample ErrorException: Invalid argument
    supplied for foreach()</p>
    
    <p>/Users/Kamil/Desktop/Code/kumarajiva.com/vendor/laravel/framework/src/Illuminate/Support/Arr.php:494
    /Users/Kamil/Desktop/Code/kumarajiva.com/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:232
    /Users/Kamil/Desktop/Code/kumarajiva.com/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:257
    /Users/Kamil/Desktop/Code/kumarajiva.com/tests/ProjectsTest.php:26</p>
    
    <p>FAILURES! Tests: 2, Assertions: 2, Errors: 1.</p>
  </blockquote>
  
  <p>Any ideas what to do with this error?</p>