Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit test custom doctrine repository
    text
    copied!<p>I have a custom entity repository. For example, it looks like this:</p> <pre><code>namespace Foo\Repository; use Doctrine\ORM\EntityRepository; class Article extends EntityRepository { public function findRecent($limit) { $qb = $this-&gt;createQueryBuilder('a'); $qb-&gt;andWhere('a.publishDate IS NOT NULL') -&gt;orderBy('a.publishDate', 'DESC') -&gt;setMaxResults($limit); return $qb-&gt;getQuery()-&gt;getResult(); } } </code></pre> <p>I want to test in this case:</p> <ol> <li>There is an ORDER BY in "recent"</li> <li>There is a limit</li> <li>The entity must have a publish date</li> </ol> <p>I <strong>do not</strong> want to validate the SQL output of the query builder, since Doctrine can change the SQL between different versions. That will break my unit test. Therefore, my idea was this:</p> <ol> <li>Create a mock of my repository</li> <li>Create a mock of the query builder</li> <li>Make sure <code>$this-&gt;createQueryBuilder('a')</code> returns the mocked query builder</li> <li>Test for method calls on the query builder</li> </ol> <p>In code:</p> <pre><code>namespace FooTest\Repository; use PHPUnit_Framework_TestCase as TestCase; class ArticleRepositoryTest extends TestCase { protected $qb; protected $repository; public function setUp() { $this-&gt;qb = $this-&gt;getMockBuilder('Doctrine\ORM\QueryBuilder') -&gt;disableOriginalConstructor() -&gt;getMock(); $this-&gt;repository = $this-&gt;getMockBuilder('Foo\Repository\Article') -&gt;disableOriginalConstructor() -&gt;getMock(); $this-&gt;repository-&gt;expects($this-&gt;once()) -&gt;method('createQueryBuilder') -&gt;with($this-&gt;equalTo('a')) -&gt;will($this-&gt;returnValue($this-&gt;qb)); } public function testFindRecentLimitsToGivenLimit() { $limit = '1'; $this-&gt;qb-&gt;expects($this-&gt;any()) -&gt;method('setMaxResults') -&gt;with($this-&gt;equalTo($limit)); $this-&gt;repository-&gt;findRecent($limit); } public function testFindRecentOrdersByPublishDate() { $this-&gt;qb-&gt;expects($this-&gt;any()) -&gt;method('andWhere') -&gt;with($this-&gt;equalTo('a.publishDate'), $this-&gt;equalTo('DESC')); $this-&gt;repository-&gt;findRecent(1); } } </code></pre> <p>This <code>findRecent()</code> call however <strong>never</strong> calls <code>createQueryBuilder</code> internally. PhpUnit points out:</p> <blockquote> <p>FooTest\Repository\ArticleRepositoryTest::testFindRecentLimitsToGivenLimit Expectation failed for method name is equal to when invoked 1 time(s). Method was expected to be called 1 times, actually called 0 times.</p> </blockquote> <p>I think I did something wrong in creating the repository mock. How can I make sure this approach works? Or if there is a better alternative, what is that?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload