dqifn68206 2017-03-06 17:18
浏览 79
已采纳

使用ZF2 Paginator进行phpUnit测试

I'm coding a phpUnit test and got this fail:

Tests\Professor\Controller\DisciplinaControllerTest::testListaDisciplinas
Failed asserting response code "200", actual status code is "500"

To get a bit more information when something goes wrong in a test case, I set the protected $traceError member to true. And then got this error:

Tests\Professor\Controller\DisciplinaControllerTest::testListaDisciplinas
Argument 1 passed to Doctrine\ORM\Tools\Pagination\Paginator::cloneQuery() must be an instance of Doctrine\ORM\Query, array given, called in /uov-videos/www/david/plataovirtual/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Pagination/Paginator.php on line 239 and defined

This is happening because my query returns an Paginator object and my test is expecting an array. How do I instantiate a mocked Paginator to use it in my test?

phpUnit function code:

public function testListaDisciplinas()
{
    $this->auth();
    // simulate the repository to not affect the db
    $disciplinaRepository = $this->getMock('Domain\Model\Disciplina\DisciplinaRepository');

    $disciplinaRepository->method('getByProfessorTutor')
                         ->willReturn(array());

    $serviceManager = $this->getApplicationServiceLocator();
    $serviceManager->setAllowOverride(true);
    $serviceManager->setService('DisciplinaRepository', $disciplinaRepository);

    // Route to getList() above
    $result = $this->dispatch('/uov/professor/disciplinas');
    $this->assertResponseStatusCode(200);
}

Inside my Controller:

public function getList()
{
    $usuario = $this->getSessaoUsuario();

    $professor = $this->professorRepository()->getByUsuario($usuario);

    $disciplinas = $this->disciplinaRepository()->getByProfessorTutor($professor);

    $adapter = new DoctrinePaginator($disciplinas);
    $paginator = new Paginator($adapter);
    $paginator->setDefaultItemCountPerPage(20);

    $view = new ViewModel(array(
        'disciplinas' => $paginator,
    ));

    $request = $this->getRequest();

    $view->setTerminal($request->isXmlHttpRequest());

    return $view;
}

Function inside my repository:

public function getByProfessorTutor(Professor $professor)
{
    $query = $this->em->createQueryBuilder();

    $query->select('d')
          ->from($this->class, 'd')
          ->leftJoin('Domain\Model\Turma\Turma', 't', 'WITH', 't.disciplina = d')
          ->leftJoin('Domain\Model\Professor\ProfessorTurma', 'pt', 'WITH', 'pt.turma = t.id')
          ->where('(t.professor = :professor OR pt.professor = :professor)')
          ->setParameter('professor', $professor);

    return new Paginator($query, $fetchJoinCollection = false);
}
  • 写回答

1条回答 默认 最新

  • doujiang1939 2017-03-09 13:43
    关注

    I managed to solve this problem. Here it goes the code, how to simulate a Doctrine Paginator object.

    public function testListaDisciplinas()
    {
        $this->auth();
        // simulate the repository to not modify the db
        $disciplinaRepository = $this->getMock('Domain\Model\Disciplina\DisciplinaRepository');
    
        $em = $this->getApplicationServiceLocator()->get('Doctrine\ORM\EntityManager');
    
        $query = $em->createQueryBuilder();
        $query->select('d')
              ->from('Domain\Model\Disciplina\Disciplina', 'd')
              ->setMaxResults(1);
    
        $disciplinaRepository->method('getByProfessorTutor')
                             ->willReturn(new Paginator($query, $fetchJoinCollection = false));
    
        $serviceManager = $this->getApplicationServiceLocator();
        $serviceManager->setAllowOverride(true);
        $serviceManager->setService('DisciplinaRepository', $disciplinaRepository);
        $result = $this->dispatch('/uov/professor/disciplinas');
        $this->assertResponseStatusCode(200);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀