在Symfony2中使用phpunit测试Doctrine ODM存储库

时间:2013-05-06 10:57:30

标签: symfony mocking phpunit doctrine-odm

我想测试这个简单的查询:

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.

但我不确定如何测试并检查结果。

更新

我更改了代码以通过测试(好吧,它是绿色),但对我来说这个测试不能正常工作,因为虽然删除了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());
}

UPDATE2:

我认为最终我理解模拟对象:)所以如上所述,我正在嘲笑存储库,但这不是客观的。我测试此方法的最后一个代码是:

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));
}

测试工作虽然取决于用户存在并且有文章。可以考试吗?

0 个答案:

没有答案