この単純なクエリをテストしたい:
public function findArticlesByUsers($ids) {
$qb = $this->createQueryBuilder();
$qb
->addOr($qb->expr()->field('creator.id')->in($ids))
->addOr($qb->expr()->field('modifier.id')->in($ids))
->addOr($qb->expr()->field('publisher.id')->in($ids));
$query = $qb->getQuery();
$results = $query->execute();
return $results;
}
作成者、変更者、および公開者は、異なるユーザーになることができます。
私は Symfony doc のこの例に従っています: http://symfony.com/doc/current/cookbook/testing/database.html#mocking-the-repository-in-a-unit-test
このクエリビルダーをテストする方法は?. 私のコードは次のようになります。
public function testFindArticlesByUsers()
{
$article = $this->getMock('\Acme\DemoBundle\Document\Article');
$article->expects($this->once())
->method('getCreator')
->will($this->returnValue($this->getMock('\Acme\DemoBundle\Document\\User')));
$article->expects($this->once())
->method('getModifier')
->will($this->returnValue($this->getMock('\Acme\DemoBundle\Document\\User')));
$article->expects($this->once())
->method('getPublisher')
->will($this->returnValue($this->getMock('\Acme\DemoBundle\Document\\User')));
$articleRepository = $this->getMockBuilder('\Doctrine\ODM\DocumentRepository')
->setMethods(array('findArticlesByUsers'))
->disableOriginalConstructor()
->getMock();
$articleRepository->expects($this->once())
->method('findArticlesByUsers')
->will($this->returnValue($article));
$documentManager = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
->getMock();
$documentManager->expects($this->once())
->method('getRepository')
->will($this->returnValue($articleRepository));
$article2 = $documentManager->getRepository('BackendBundle:Article')
->findArticlesByUsers(1);
}
私はそれを継続するかどうか、そしてこれが正しい方法であるかどうかはわかりません。エラーは次のとおりです。
Expectation failed for method name is equal to <string:getCreator> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
しかし、これをテストして結果を確認する方法がわかりません。
アップデート:
テストに合格するようにコードを変更しました(OK、緑色です)が、findArticlesByUsersメソッドを削除してもテストが緑色であるため、このテストはうまく機能しません。私が間違っていることは何ですか?
public function testFindArticlesByUsers()
{
$userCreator = $this->getMock('\TT\BackendBundle\Document\User');
$userCreator->expects($this->any())
->method('getId')
->will($this->returnValue(1));
$userModifier = $this->getMock('\TT\BackendBundle\Document\User');
$userModifier->expects($this->any())
->method('getId')
->will($this->returnValue(2));
$userPublisher = $this->getMock('\TT\BackendBundle\Document\User');
$userPublisher->expects($this->any())
->method('getId')
->will($this->returnValue(3));
//Article mock
$article = $this->getMock('\TT\BackendBundle\Document\Article');
$article->expects($this->once())
->method('getCreator')
->will($this->returnValue($userCreator));
$article->expects($this->once())
->method('getModifier')
->will($this->returnValue($userModifier));
$article->expects($this->once())
->method('getPublisher')
->will($this->returnValue($userPublisher));
$articleRepository = $this->getMockBuilder('\Doctrine\ODM\DocumentRepository')
->setMethods(array('findArticlesByUsers'))
->disableOriginalConstructor()
->getMock();
$articleRepository->expects($this->once())
->method('findArticlesByUsers')
->will($this->returnValue($article));
$documentManager = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
->getMock();
$documentManager->expects($this->once())
->method('getRepository')
->will($this->returnValue($articleRepository));
$article = $documentManager->getRepository('BackendBundle:Article')
->findArticlesByUsers(1);
$this->assertEquals(1, $article->getCreator()->getId());
$this->assertEquals(2, $article->getModifier()->getId());
$this->assertEquals(3, $article->getPublisher()->getId());
}
アップデート2:
ようやくモックオブジェクトを理解したと思います:)上記のように、リポジトリをモックしていますが、これは目的ではありません。このメソッドをテストする最後のコードは次のとおりです。
public function testFindArticlesByUsers()
{
$userId = $this->dm->createQueryBuilder('AcmeBundle:User')
->field('username')->equals('fakeUser')
->getQuery()->getSingleResult()->getId();
$articles = $this->dm->getRepository('AcmeBundle:Article')
->findArticlesByUsers(array($userId));
$this->assertGreaterThanOrEqual(1, count($articles));
}
ユーザーが存在し、記事を持っているかどうかに依存しますが、テストは機能します。テスト大丈夫?