title: How to mock a method from the class you are testing with Prophecy?
body: <p>I want to use <a href="https://github.com/phpspec/prophecy" rel="nofollow">Prophecy</a> ("phpspec/prophecy-phpunit") for the first time to create unit tests for my classes. I want to test a function that calls another function in the same service, here's the code:</p>
  
  <pre><code>class UserManager
  {
      private $em;
      private $passwordHelper;
  
      public function __construct(\Doctrine\ORM\EntityManager $em, \MainBundle\Helper\PasswordHelper $passwordHelper)
       {
           $this-&gt;em = $em;
           $this-&gt;passwordHelper = $passwordHelper;
       }
  
       public function getUserForLdapLogin($ldapUser)
       {
            $dbUser = $this
                -&gt;em
                -&gt;getRepository('MainBundle:User')
                -&gt;findOneBy(array('username' =&gt; $ldapUser-&gt;getUsername()));
  
           return (!$dbUser) ?
                $this-&gt;createUserFromLdap($ldapUser) :
                $this-&gt;updateUserFromLdap($ldapUser, $dbUser);
       }
  </code></pre>
  
  <p>First problem I had was that I was using <code>findOneByUsername</code> and Prophecy, as far as my knowledge goes, does not allow you to: mock magic methods (<code>_call</code> for <code>EntityRepository</code>), mock methods that do not exist, mock the class you are testing. If these are true I'm in a bit of a pickle, meaning I cannot test this function without testing the other functions in the class. </p>
  
  <p>So far, my test looks like this:</p>
  
  <pre><code>class UserManagerTest extends \Prophecy\PhpUnit\ProphecyTestCase
  {
  
        public function testGetUserForLdapLoginWithNoUser()
        {
            $ldapUser = new LdapUser();
            $ldapUser-&gt;setUsername('username');
  
            $em = $this-&gt;prophesize('Doctrine\ORM\EntityManager');
            $passwordHelper = $this-&gt;prophesize('MainBundle\Helper\PasswordHelper');
  
            $repository = $this-&gt;prophesize('Doctrine\ORM\EntityRepository');
            $em-&gt;getRepository('MainBundle:User')-&gt;willReturn($repository);
            $repository-&gt;findOneBy(array('username' =&gt; 'username'))-&gt;willReturn(null);
  
            $em-&gt;getRepository('MainBundle:User')-&gt;shouldBeCalled();
            $repository-&gt;findOneBy(array('username' =&gt; 'username'))-&gt;shouldBeCalled();
  
            $service = $this-&gt;prophesize('MainBundle\Helper\UserManager')
              -&gt;willBeConstructedWith(array($em-&gt;reveal(), $passwordHelper-&gt;reveal()));
  
            $service-&gt;reveal();
            $service-&gt;getUserForLdapLogin($ldapUser);
       }
  }
  </code></pre>
  
  <p>And of course, the tests fail because the promises on <code>$em</code>, and the repository are not fulfilled. If I instantiate the class I am testing, the tests fail because the function then calls <code>createUserFromLdap()</code> on the same class and that is not tested. </p>
  
  <p>Any suggestions?</p>