Search code examples
phpzend-framework2mockingphpunitpaginator

phpUnit testing with ZF2 Paginator


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

Solution

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