Created
November 4, 2012 13:54
-
-
Save eminetto/4012024 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Testa a página inicial, que deve mostrar os posts | |
*/ | |
public function testIndexAction() | |
{ | |
// Cria posts para testar | |
$postA = $this->addPost(); | |
$postB = $this->addPost(); | |
// Invoca a rota index | |
$this->routeMatch->setParam('action', 'index'); | |
$result = $this->controller->dispatch($this->request, $this->response); | |
// Verifica o response | |
$response = $this->controller->getResponse(); | |
$this->assertEquals(200, $response->getStatusCode()); | |
// Testa se um ViewModel foi retornado | |
$this->assertInstanceOf('Zend\View\Model\ViewModel', $result); | |
// Testa os dados da view | |
$variables = $result->getVariables(); | |
$this->assertArrayHasKey('posts', $variables); | |
// Faz a comparação dos dados | |
$controllerData = $variables["posts"]->getCurrentItems()->toArray(); | |
$this->assertEquals($postA->title, $controllerData[0]['title']); | |
$this->assertEquals($postB->title, $controllerData[1]['title']); | |
} | |
/** | |
* Testa a página inicial, que deve mostrar os posts com paginador | |
*/ | |
public function testIndexActionPaginator() | |
{ | |
// Cria posts para testar | |
$post = array(); | |
for($i=0; $i< 25; $i++) { | |
$post[] = $this->addPost(); | |
} | |
// Invoca a rota index | |
$this->routeMatch->setParam('action', 'index'); | |
$result = $this->controller->dispatch($this->request, $this->response); | |
// Verifica o response | |
$response = $this->controller->getResponse(); | |
$this->assertEquals(200, $response->getStatusCode()); | |
// Testa se um ViewModel foi retornado | |
$this->assertInstanceOf('Zend\View\Model\ViewModel', $result); | |
// Testa os dados da view | |
$variables = $result->getVariables(); | |
$this->assertArrayHasKey('posts', $variables); | |
//testa o paginator | |
$paginator = $variables["posts"]; | |
$this->assertEquals('Zend\Paginator\Paginator', get_class($paginator)); | |
$posts = $paginator->getCurrentItems()->toArray(); | |
$this->assertEquals(10, count($posts)); | |
$this->assertEquals($post[0]->id, $posts[0]['id']); | |
$this->assertEquals($post[1]->id, $posts[1]['id']); | |
//testa a terceira página da paginação | |
$this->routeMatch->setParam('action', 'index'); | |
$this->routeMatch->setParam('page', 3); | |
$result = $this->controller->dispatch($this->request, $this->response); | |
$variables = $result->getVariables(); | |
$controllerData = $variables["posts"]->getCurrentItems()->toArray(); | |
$this->assertEquals(5, count($controllerData)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment