mock function called, but expects($this->once()) fails
<p>I'm testing a Symfony command to send reminder Text messages. For this I have created a service for my text message interface and am mocking the container as well as the text messaging service:</p>

<p><strong>The function under test</strong></p>

<pre><code>protected function textReminders()
{
    $mailer = $this-&gt;getContainer()-&gt;get('mailer');
    $em = $this-&gt;getContainer()-&gt;get( 'doctrine' )-&gt;getManager();


    if ($this-&gt;getContainer()-&gt;get('kernel')-&gt;getEnvironment() == 'dev'){
        $debug = true;
    }else{
        $debug = false;
    }

    $textMessage = $this-&gt;getContainer()-&gt;get('text_messaging.interface');
    $textMessage-&gt;sendSMS( $target, $content, $debug);

}
</code></pre>

<p><strong>Test</strong></p>

<pre><code>private function getMockContainer()
{
    $container = $this-&gt;getMockBuilder('Symfony\Component\DependencyInjection\Container')
                      -&gt;disableOriginalConstructor()
                      -&gt;setMethods(array('get'))
                      -&gt;getMock();

    return $container;
}

protected function setupMocks()
{
    $mockText = $this-&gt;getMockBuilder('TextaHQ')
                     -&gt;disableOriginalConstructor()
                     -&gt;setMethods(array('sendSMS'))
                     -&gt;getMock();
    $mockContainer = $this-&gt;getMockContainer();

    $container = self::$kernel-&gt;getContainer();
    $mockContainer-&gt;method('get')
        -&gt;withConsecutive(['mailer'], ['doctrine'], ['kernel'], ['text_messaging.interface'])
        -&gt;willReturnOnConsecutiveCalls(
            $this-&gt;returnValue($container-&gt;get('mailer')),
            $this-&gt;returnValue($container-&gt;get('doctrine')),
            $this-&gt;returnValue(self::$kernel),
            $this-&gt;returnValue($mockText))
    ;

    $this-&gt;setMyMock([
                         'text' =&gt; $mockText,
                         'container' =&gt; $mockContainer
                     ]);
}

public function testExecute()
{
    $this-&gt;setupMocks();
    self::bootKernel();
    $application = new Application(self::$kernel);

    $application-&gt;add(new ActionRemindCommand());

    $command = $application-&gt;find( 'ahp:remind' );
    $command-&gt;setContainer($this-&gt;getMyMock()['container']);
    $commandTester = new CommandTester( $command );

    $commandTester-&gt;execute( array(
                                     'command' =&gt; $command-&gt;getName(),
                                     'type' =&gt; 'text'
                                 ) );
    $output = $commandTester-&gt;getDisplay();

    $this-&gt;assertions();
}

protected function assertions()
{
    $this-&gt;getMyMock()['text']
        -&gt;expects( $this-&gt;once() )
            -&gt;method( 'sendSMS' )
            ;

}
</code></pre>

<p><strong>Updated test, all in one file</strong></p>

<pre><code>public function testExecute()
{
    $insertSql = 'echo "'
        . str_replace(
            array('"'    ,'`'  ),
            array('\\"'  ,'\\`'),
            $this-&gt;getPrepSql() )
        . '" | mysql ahp_example_com';
    exec($insertSql);

    self::bootKernel();

    $mockText = $this-&gt;getMockBuilder('TextaHQ')
                     -&gt;disableOriginalConstructor()
                     -&gt;setMethods(array('sendSMS'))
                     -&gt;getMock();
    $mockContainer = $this-&gt;getMockBuilder('Symfony\Component\DependencyInjection\Container')
                                       -&gt;disableOriginalConstructor()
                                       -&gt;setMethods(array('get'))
                                       -&gt;getMock();

    $container = self::$kernel-&gt;getContainer();
    $mockContainer-&gt;method('get')
                  -&gt;withConsecutive(['mailer'], ['doctrine'], ['kernel'], ['text_messaging.interface'])
                  -&gt;willReturnOnConsecutiveCalls(
                      $this-&gt;returnValue($container-&gt;get('mailer')),
                      $this-&gt;returnValue($container-&gt;get('doctrine')),
                      $this-&gt;returnValue(self::$kernel),
                      $this-&gt;returnValue($mockText))
    ;


    $application = new Application(self::$kernel);

    $application-&gt;add(new ActionRemindCommand());

    $mailer = self::$kernel-&gt;getContainer()-&gt;get('swiftmailer.mailer');
    $logger = new \Swift_Plugins_MessageLogger();
    $mailer-&gt;registerPlugin( $logger );
    $this-&gt;setMailCollector($logger);

    $output = '';
    for($i=1;$i&lt;=$this-&gt;getRunNoTimes();$i++) {
        $command = $application-&gt;find( 'ahp:remind' );
        $command-&gt;setContainer($mockContainer);
        $commandTester = new CommandTester( $command );

        $commandTester-&gt;execute( array(
                                     'command' =&gt; $command-&gt;getName(),
                                     'type' =&gt; 'text'
                                 ) );
        $output .= $commandTester-&gt;getDisplay();
    }

    $mockText
        -&gt;expects( $this-&gt;once() )
        -&gt;method( 'sendSMS' )
    ;
}
</code></pre>

<p>**PHPStorm Test call **</p>

<pre><code>/usr/bin/php     /home/jochen/projects/ahp/trunk/vendor/phpunit/phpunit/phpunit --configuration /home/jochen/projects/ahp/trunk/app/phpunit.xml.dist AgriHealth\AhpBundle\Tests\Command\ActionRemindCommandVet7DaysBeforeTest /home/jochen/projects/ahp/trunk/src/AgriHealth/AhpBundle/Tests/Command/RemindText/ActionRemindCommandVet7DaysBeforeTest.php --teamcity
Testing started at 10:25 AM ...
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.


Expectation failed for method name is equal to &lt;string:sendSMS&gt; when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.



Time: 1.12 seconds, Memory: 18.00MB


FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Process finished with exit code 1
</code></pre>

<p>When I debug the test, I can see that $textMessage is a mock.</p>

<p>However at the end of the test in assertions(), I get an error:</p>

<pre><code>Expectation failed for method name is equal to &lt;string:sendsms&gt; when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
</code></pre>

<p>The debugger shows the mocked function as lower case: "sendsms", but renaming the function did not help.</p>