title: PHPUnit - how to mock/stub a redirect method?
body: <p>I am working on a PHP Unit test where I need to test (while 100% code coverage is required) a class that implements it's own <code>redirect($url, $code)</code> method. This method calls <code>exit;</code> at the end to make sure no other code is executed after the redirect (and without this <code>exit;</code> the redirection is not done in the end...).</p>
  
  <p>Now this redirect method is called in 7 different conditions which all have their own unit test case. If I run the unit tests while the <code>exit;</code> is present in the tested class, the unit tests are exited as well. Each of the test cases is testing something like:</p>
  
  <pre class="lang-php prettyprint-override"><code>$this-&gt;assertRedirect();
  $this-&gt;assertResponseStatusCode(401);
  </code></pre>
  
  <p>Therefore I need to <strong>stub</strong> the <code>redirect</code> method so that it set's the <code>Location</code> header as well as the status code.</p>
  
  <p>Here is what I am trying to do (using the redirect method's content except that <code>exit;</code> part - but it is not working obviously):</p>
  
  <pre class="lang-php prettyprint-override"><code>    $this-&gt;class = $this-&gt;getMockBuilder('Some\Name\Space\Class')
              -&gt;disableOriginalConstructor()
              -&gt;getMock();
      $this-&gt;class-&gt;expects($this-&gt;any())
              -&gt;method('redirect')
              -&gt;will($this-&gt;returnCallback(function () {
                  $this-&gt;response-&gt;getHeaders()-&gt;addHeaderLine('Location: /');
                  $this-&gt;response-&gt;setStatusCode(401);
                  $this-&gt;response-&gt;sendHeaders();
              }));
  </code></pre>
  
  <p>and what I have tried before (and didn't work as well):</p>
  
  <pre class="lang-php prettyprint-override"><code>    $this-&gt;class = $this-&gt;getMockBuilder('Some\Name\Space\Class')
              -&gt;disableOriginalConstructor()
              -&gt;getMock();
      $this-&gt;class-&gt;expects($this-&gt;any())
              -&gt;method('redirect')
              -&gt;will($this-&gt;returnCallback(function () {
                  header('Location: /');
              }));
  </code></pre>
  
  <p>Now the purpose of the class is to redirect somewhere in 90% of code coverage (only in one case - 2 lines out of 90 - it is not redirecting) so without the possibility to stub this redirect (and without possibility to test redirects at all) the test cases would not have almost any sense for this class (which is unwilling).</p>
  
  <p>Any help is highly appreciated.</p>
  
  <p><strong>EDIT:</strong> Looks like <a href="https://github.com/php-test-helpers/php-test-helpers/blob/master/README.markdown" rel="nofollow">phpunit/test_helpers</a> could do the trick, but unfortunately, I am not able to install this extension to our preview and production environments where the tests are automatically run per each application deploy.</p>