使用 ZF2 分页器进行 phpUnit 测试
phpUnit testing with ZF2 Paginator
我正在编写 phpUnit 测试,但失败了:
Tests\Professor\Controller\DisciplinaControllerTest::testListaDisciplinas
Failed asserting response code "200", actual status code is "500"
为了在测试用例出现问题时获得更多信息,我将受保护的 $traceError 成员设置为 true。然后得到这个错误:
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
这是因为我的查询 returns 一个分页器对象 而我的测试 需要一个数组 。如何实例化一个模拟分页器以在我的测试中使用它?
phpUnit函数代码:
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);
}
在我的控制器中:
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;
}
我存储库中的函数:
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);
}
我设法解决了这个问题。这是代码,如何模拟 Doctrine Paginator 对象。
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);
}
我正在编写 phpUnit 测试,但失败了:
Tests\Professor\Controller\DisciplinaControllerTest::testListaDisciplinas
Failed asserting response code "200", actual status code is "500"
为了在测试用例出现问题时获得更多信息,我将受保护的 $traceError 成员设置为 true。然后得到这个错误:
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
这是因为我的查询 returns 一个分页器对象 而我的测试 需要一个数组 。如何实例化一个模拟分页器以在我的测试中使用它?
phpUnit函数代码:
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);
}
在我的控制器中:
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;
}
我存储库中的函数:
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);
}
我设法解决了这个问题。这是代码,如何模拟 Doctrine Paginator 对象。
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);
}