title: Laravel phpunit Cannot modify header information - headers already sent
body: <p>I've gone around the multiple posts around the same issue and it can't seem to cover my case.</p>
  
  <p>My idea:</p>
  
  <p>I'm using JWT to authenticate users and my idea is to determine that whenever a token is nearly expiration date, I want to return a new token within the headers (Header Authorization).</p>
  
  <p>I've got this middleware made, just to test some things out</p>
  
  <pre><code>public function handle($request, Closure $next)
      {
          try {
              $user = JWTAuth::parseToken()-&gt;authenticate();
              $claims = JWTAuth::getPayload(JWTAuth::getToken())-&gt;toArray();
  
              if ($claims['exp'] - (strtotime(date("Y-m-d H:i:s"))) &lt;= 900) {
                  $token = JWTAuth::refresh(JWTAuth::getToken());
                  header('Authorization: Bearer ' . $token);
              }
          } catch (Exception $e) {
              if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException) {
                  return $this-&gt;response([], 'Authorization Token is Invalid', 401);
              } elseif ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException) {
                  try {
                      $token = JWTAuth::refresh(JWTAuth::getToken());
                      header('Authorization: Bearer ' . $token);
                      return $this-&gt;response([], 'Authorization Token is Expired', 401);
                  } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
                      return $this-&gt;response([], 'Authorization Token is Invalid', 401);
                  }
              } else {
                  return $this-&gt;response([], 'Authorization Token not found', 401);
              }
          }
          return $next($request);
      }
  </code></pre>
  
  <p>So far so good, testing with POSTMAN I get my desirable outcome. However, I went along to write down an acceptance test in order to test that indeed, this is correct.</p>
  
  <pre><code>public function testUserReceivesNewTokenInHeaderWhenExpiresAtIsWithin15Minutes()
      {
          //Arrange
          $token = JWTAuth::claims(['exp' =&gt; strtotime("+1 minutes", strtotime(date('Y-m-d H:i:s')))])-&gt;fromUser($this-&gt;user);
          $this-&gt;headers['HTTP_Authorization'] = "Bearer $token";
          //Act
          $response = $this-&gt;get('/api/justAnotherURL', $this-&gt;headers);
          //Assert
          $response-&gt;assertStatus(200);
          $this-&gt;assertTrue($response-&gt;headers-&gt;has('Authorization'));
      }
  </code></pre>
  
  <p>I set the <code>exp</code> date to be within the next minute (so it enters the validation above), it does enter as expected, but when it reaches <code>Header(Authorization: Bearer $token)</code>, it throws an <code>Exception</code>:</p>
  
  <blockquote>
    <p>Cannot modify header information - headers already sent by (output started at /var/www/project/vendor/phpunit/phpunit/src/Util/Printer.php:109)</p>
  </blockquote>
  
  <p>As far as my research goes (and attempts), it's something about outputting  the request on the console, but I've been unsuccessful so far. </p>
  
  <blockquote>
    <p>Related post: <a href="https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php?noredirect=1&amp;lq=1">How to fix &quot;Headers already sent&quot; error in PHP</a></p>
  </blockquote>
  
  <p>Small notes:</p>
  
  <p><code>this-&gt;response</code> is my own custom response to the request, you can ignore that</p>
  
  <p>Tried <code>@runInSeparateProcess</code> from another post and error dissapears, but the header isn't set (default header is returned)</p>
  
  <p>Edit: I've tried placing the header at the top of the file, before the try catch and it remains the same (I was wondering if jwt would make some output without me knowing) and this is the only middleware being used atm</p>