title: Test Doctrine ODM respository with phpunit in Symfony2
body: <p>I want to test this simple query:</p>
  
  <pre><code>public function findArticlesByUsers($ids) {
  
      $qb = $this-&gt;createQueryBuilder();
      $qb
              -&gt;addOr($qb-&gt;expr()-&gt;field('creator.id')-&gt;in($ids))
              -&gt;addOr($qb-&gt;expr()-&gt;field('modifier.id')-&gt;in($ids))
              -&gt;addOr($qb-&gt;expr()-&gt;field('publisher.id')-&gt;in($ids));
  
      $query = $qb-&gt;getQuery();
      $results = $query-&gt;execute();
  
      return $results;
  }
  </code></pre>
  
  <p>Where <strong>creator, modifier and publisher can be differents user</strong>.</p>
  
  <p>I'm following this example of Symfony doc: <a href="http://symfony.com/doc/current/cookbook/testing/database.html#mocking-the-repository-in-a-unit-test" rel="nofollow">http://symfony.com/doc/current/cookbook/testing/database.html#mocking-the-repository-in-a-unit-test</a></p>
  
  <p>How to test this query builder?. My code look like this:</p>
  
  <pre><code>public function testFindArticlesByUsers()
  {
      $article = $this-&gt;getMock('\Acme\DemoBundle\Document\Article');
      $article-&gt;expects($this-&gt;once())
          -&gt;method('getCreator')
          -&gt;will($this-&gt;returnValue($this-&gt;getMock('\Acme\DemoBundle\Document\\User')));
      $article-&gt;expects($this-&gt;once())
          -&gt;method('getModifier')
          -&gt;will($this-&gt;returnValue($this-&gt;getMock('\Acme\DemoBundle\Document\\User')));
      $article-&gt;expects($this-&gt;once())
          -&gt;method('getPublisher')
          -&gt;will($this-&gt;returnValue($this-&gt;getMock('\Acme\DemoBundle\Document\\User')));
  
      $articleRepository = $this-&gt;getMockBuilder('\Doctrine\ODM\DocumentRepository')
          -&gt;setMethods(array('findArticlesByUsers'))
          -&gt;disableOriginalConstructor()
          -&gt;getMock();
      $articleRepository-&gt;expects($this-&gt;once())
          -&gt;method('findArticlesByUsers')
          -&gt;will($this-&gt;returnValue($article));
  
      $documentManager = $this-&gt;getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
          -&gt;disableOriginalConstructor()
          -&gt;getMock();
      $documentManager-&gt;expects($this-&gt;once())
          -&gt;method('getRepository')
          -&gt;will($this-&gt;returnValue($articleRepository));
  
      $article2 = $documentManager-&gt;getRepository('BackendBundle:Article')
          -&gt;findArticlesByUsers(1);
  }
  </code></pre>
  
  <p>I haven't idea continuous with it and if this is the correct way. The error is:</p>
  
  <pre><code>Expectation failed for method name is equal to &lt;string:getCreator&gt; when invoked 1 time(s).
  Method was expected to be called 1 times, actually called 0 times.
  </code></pre>
  
  <p>But I'm not sure how to test this and check the result.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I changed the code to pass the test(ok, it's green) but for me this test doesn't work fine because although delete findArticlesByUsers method the test is green. What I'm doing wrong?</p>
  
  <pre><code>public function testFindArticlesByUsers()
  {
      $userCreator = $this-&gt;getMock('\TT\BackendBundle\Document\User');
      $userCreator-&gt;expects($this-&gt;any())
          -&gt;method('getId')
          -&gt;will($this-&gt;returnValue(1));
  
      $userModifier = $this-&gt;getMock('\TT\BackendBundle\Document\User');
      $userModifier-&gt;expects($this-&gt;any())
          -&gt;method('getId')
          -&gt;will($this-&gt;returnValue(2));
  
      $userPublisher = $this-&gt;getMock('\TT\BackendBundle\Document\User');
      $userPublisher-&gt;expects($this-&gt;any())
          -&gt;method('getId')
          -&gt;will($this-&gt;returnValue(3));
  
      //Article mock
      $article = $this-&gt;getMock('\TT\BackendBundle\Document\Article');
      $article-&gt;expects($this-&gt;once())
          -&gt;method('getCreator')
          -&gt;will($this-&gt;returnValue($userCreator));
      $article-&gt;expects($this-&gt;once())
          -&gt;method('getModifier')
          -&gt;will($this-&gt;returnValue($userModifier));
      $article-&gt;expects($this-&gt;once())
          -&gt;method('getPublisher')
          -&gt;will($this-&gt;returnValue($userPublisher));
  
      $articleRepository = $this-&gt;getMockBuilder('\Doctrine\ODM\DocumentRepository')
          -&gt;setMethods(array('findArticlesByUsers'))
          -&gt;disableOriginalConstructor()
          -&gt;getMock();
      $articleRepository-&gt;expects($this-&gt;once())
          -&gt;method('findArticlesByUsers')
          -&gt;will($this-&gt;returnValue($article));
  
      $documentManager = $this-&gt;getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
          -&gt;disableOriginalConstructor()
          -&gt;getMock();
      $documentManager-&gt;expects($this-&gt;once())
          -&gt;method('getRepository')
          -&gt;will($this-&gt;returnValue($articleRepository));
  
      $article = $documentManager-&gt;getRepository('BackendBundle:Article')
          -&gt;findArticlesByUsers(1);
  
      $this-&gt;assertEquals(1, $article-&gt;getCreator()-&gt;getId());
      $this-&gt;assertEquals(2, $article-&gt;getModifier()-&gt;getId());
      $this-&gt;assertEquals(3, $article-&gt;getPublisher()-&gt;getId());
  }
  </code></pre>
  
  <p><strong>Update2:</strong></p>
  
  <p>I think finally I understand mock object :) so doing as above I'm mocking the repository but this isn't the objetive. My last code to test this method is:</p>
  
  <pre><code>public function testFindArticlesByUsers()
  {
      $userId = $this-&gt;dm-&gt;createQueryBuilder('AcmeBundle:User')
                                -&gt;field('username')-&gt;equals('fakeUser')
                                -&gt;getQuery()-&gt;getSingleResult()-&gt;getId();
  
      $articles = $this-&gt;dm-&gt;getRepository('AcmeBundle:Article')
          -&gt;findArticlesByUsers(array($userId));
  
      $this-&gt;assertGreaterThanOrEqual(1, count($articles));
  }
  </code></pre>
  
  <p>The test works although it depends on of it the user exists and has articles. Is ok the test?</p>