title: PHPunit method mocking does not work
body: <p>I do not understand why not work my method mocking, because the method run really, and do not want to return with the mocked value. (Symfony)</p>
  
  <pre><code>    $this-&gt;mockedObject = $this
          -&gt;getMockBuilder('Object')
          //-&gt;setConstructorArgs(array('em' =&gt; $this-&gt;entityManager, 'router' =&gt; $this-&gt;mockedRouter))
          -&gt;disableOriginalConstructor()
          -&gt;setMethods(array('method1', 'method2'))
          -&gt;getMock();
  
      $this-&gt;mockedObject
          -&gt;expects($this-&gt;once())
          -&gt;method('method2')
          -&gt;willReturn(9);
  </code></pre>
  
  <p>So as you see I mocked the method2, but method2 never returns with 9 value, because it is run:</p>
  
  <pre><code>Error: Call to a member function createQueryBuilder() on null
  </code></pre>
  
  <p>Here the original method2:</p>
  
  <pre><code>   private function method2($repository) {
          $qb = $this-&gt;em-&gt;createQueryBuilder();
          $qb-&gt;select('count(e.id)');
          $qb-&gt;from($repository, 'e');
          return $qb-&gt;getQuery()-&gt;getSingleScalarResult();
      }
  </code></pre>
  
  <p>And here the problem, because the mockedObject calls the method2() with the <code>createQueryBuilder</code> method</p>
  
  <p>How can I achieve that the <strong>method2</strong> be mocked and returns with 9 value?</p>