PHPunit method expected to be called 1 time, actually called 0 times
<p>I've been stuck on this for a while and I'm not sure why PHPunit can't see that the function is being called.</p>

<p>This is the code I'm trying to test:</p>

<pre><code>public function handle()
{
    $path = $this-&gt;request-&gt;getPath();
    $requestMethod = $this-&gt;request-&gt;getMethod();


    if (!$path) {
        $this-&gt;redirect('home');
    } else if (!$this-&gt;isMethodPathFound($path, $requestMethod)) {
        $this-&gt;redirect('404');
    } else {
        $handler = $this-&gt;getControllerFullName($this-&gt;routes[$path]['handler']);
        if (is_callable($handler)) {
            call_user_func($handler);
        } else {
            $this-&gt;redirect('404');
        }
    }
}

/**
 * @param string $path
 * @param int $statusCode
 */
public function redirect($path, $statusCode = 303)
{
    if (defined('TESTING_ENVIRONMENT') &amp;&amp; TESTING_ENVIRONMENT) {
        return;
    }
    header(
        'Location: ' . $this-&gt;request-&gt;getProtocol() .
        $this-&gt;request-&gt;getHost() . '/' . $path,
        true,
        $statusCode
    );
    die();
}
</code></pre>

<p>The TESTING_ENVIRONMENT variable is set for the header function so it does not trigger on running PHPunit (I don't want to create another class to have that redirect function just to be able to mock it for one test) and this is the testing code:</p>

<pre><code>public function testHandlePathIsEmpty()
{
    $requestMock = $this-&gt;getMockBuilder('\services\Request')-&gt;getMock();
    $requestMock-&gt;expects($this-&gt;once())-&gt;method('getPath')-&gt;willReturn('');
    $requestMock-&gt;expects($this-&gt;once())-&gt;method('getMethod')-&gt;willReturn('GET');
    $routerMock = $this-&gt;getMockBuilder('\services\Router')
        -&gt;setConstructorArgs([$this-&gt;routes, $requestMock])
        -&gt;enableProxyingToOriginalMethods()
        -&gt;getMock();
    $routerMock-&gt;expects($this-&gt;once())-&gt;method('redirect')
        -&gt;with('asdasd')-&gt;willReturn(true);
    $routerMock-&gt;handle();
}
</code></pre>

<p>The $routerMock object should definitely invoke the "redirect" function, and it says that it does not get invoked..even though when I var_dump/die inside the function, it does go inside of it.</p>

<p>Thanks for the help!</p>