Skip to content

Instantly share code, notes, and snippets.

@juizmill
Created April 18, 2014 18:22
Show Gist options
  • Save juizmill/11057666 to your computer and use it in GitHub Desktop.
Save juizmill/11057666 to your computer and use it in GitHub Desktop.
TDD UserControlleTest
<?php
/**
* WebPatterns (http://webpatterns.com.br/)
*
* @copyright Copyright (c) 2014-2014. (http://www.webpatterns.com.br)
* @license http://webpatterns.com.br/license
*/
namespace UserTest\WPAclTest\Controller;
use Doctrine\ORM\Tools\SchemaTool;
use User\Controller\UserController;
use User\Entity\User;
use UserTest\Framework\TestCaseController;
use WPAcl\Entity\Role;
use Zend\Http\Request;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
class UserControllerTest extends TestCaseController
{
protected $traceError = true;
public function setupDB()
{
$tool = new SchemaTool($this->getEm());
$tool->createSchema(array(
$this->getEm()->getClassMetadata('WPAcl\\Entity\\Role')
));
$role = new Role();
$role->setName('role_teste')->setIsAdmin(true);
$this->getEm()->persist($role);
$this->getEm()->flush();
$user = new User();
$user->setName('usuario_teste')
->setPassword('12345')
->setConfirmation('12345')
->setActive(true)
->setEmail('teste@teste.com')
->setLogin('teste_user')
->setRole($this->getEm()->getReference('WPAcl\Entity\Role', 1));
$this->getEm()->persist($user);
$this->getEm()->flush();
}
public function test_classe_existe()
{
$this->assertTrue(class_exists('User\\Controller\\UserController'));
$this->assertInstanceOf('WPDefault\\Controller\\AbstractWPController', new UserController());
}
public function data_provider_atributos()
{
return array(
array('itensPerPage'),
array('controller'),
array('entity'),
array('route'),
array('form')
);
}
public function data_provider_methods()
{
return array(
array('__construct'),
array('indexAction'),
array('novoAction'),
array('editarAction'),
array('deleteAction'),
array('getEm'),
);
}
/**
* @dataProvider data_provider_atributos
*/
public function test_verifica_atributos_esperados($atributo)
{
$this->assertClassHasAttribute($atributo, 'User\\Controller\\UserController');
}
/**
* @dataProvider data_provider_methods
*/
public function test_verifica_methods_esperados($method)
{
$this->assertTrue(method_exists('User\\Controller\\UserController', $method));
}
public function test_construtor()
{
$roleController = new UserController();
$this->assertEquals('User\\Entity\\User', $roleController->entity);
$this->assertEquals('user', $roleController->controller);
$this->assertEquals('user.form', $roleController->form);
$this->assertEquals('user/default', $roleController->route);
}
public function test_erro404()
{
$this->dispatch('/cms/usuario_error');
$this->assertResponseStatusCode(404);
}
public function test_indexAction()
{
$this->setupDB();
$this->dispatch('/cms/usuario');
$this->assertResponseStatusCode(200);
$this->assertModuleName('User');
$this->assertControllerName('User\Controller\User');
$this->assertControllerClass('UserController');
$this->assertMatchedRouteName('user');
// get and assert mvc event
$mvcEvent = $this->getApplication()->getMvcEvent();
$this->assertEquals(true, $mvcEvent instanceof MvcEvent);
$this->assertEquals($mvcEvent->getApplication(), $this->getApplication());
// get and assert view controller
$viewModel = $mvcEvent->getResult();
$this->assertEquals(true, $viewModel instanceof ViewModel);
// testa variavel enviada na viewmodel
$var = $viewModel->getVariables();
$this->assertArrayHasKey('data', $var);
//testa se retorna Paginator
$this->assertInstanceOf('Zend\Paginator\Paginator', $var['data']);
//testa retorno da view
foreach($var['data'] as $value){
$this->assertEquals('usuario_teste', $value->getName());
}
}
public function test_novoAction()
{
$tool = new SchemaTool($this->getEm());
$tool->createSchema(array(
$this->getEm()->getClassMetadata('WPAcl\\Entity\\Role')
));
$role = new Role();
$role->setName('role_teste')->setIsAdmin(true);
$this->getEm()->persist($role);
$this->getEm()->flush();
$this->dispatch('/cms/usuario/novo');
$this->assertResponseStatusCode(200);
$this->assertModuleName('User');
$this->assertControllerName('User\Controller\User');
$this->assertControllerClass('UserController');
$this->assertMatchedRouteName('user/default');
// get and assert mvc event
$mvcEvent = $this->getApplication()->getMvcEvent();
$this->assertEquals(true, $mvcEvent instanceof MvcEvent);
$this->assertEquals($mvcEvent->getApplication(), $this->getApplication());
// get and assert view controller
$viewModel = $mvcEvent->getResult();
$this->assertEquals(true, $viewModel instanceof ViewModel);
// testa variavel enviada na viewmodel
$var = $viewModel->getVariables();
$this->assertArrayHasKey('form', $var);
//Testa formulário na View
$this->assertInstanceOf('Zend\\Form\\Form', $var['form']);
}
public function test_POST_novoAction()
{
$tool = new SchemaTool($this->getEm());
$tool->createSchema(array(
$this->getEm()->getClassMetadata('WPAcl\\Entity\\Role')
));
$role = new Role();
$role->setName('role_teste')->setIsAdmin(true);
$this->getEm()->persist($role);
$this->getEm()->flush();
$this->dispatch('/cms/usuario/novo', Request::METHOD_POST, array(
'name' => 'usuario_teste2',
'password' => '12345',
'confirmation' => '12345',
'email' => '2teste@teste.com',
'login' => 'login_teste',
'role' => 1,
'active' => true
));
$request = $this->getRequest();
$this->assertEquals($request->getMethod(), Request::METHOD_POST);
$entity = $this->getEm()->getRepository('User\\Entity\\User')->find(1);
$this->assertEquals('usuario_teste2', $entity->getName());
}
public function test_editarAction()
{
$this->setupDB();
$this->dispatch('/cms/usuario/1/editar');
$this->assertResponseStatusCode(200);
$this->assertModuleName('User');
$this->assertControllerName('User\Controller\User');
$this->assertControllerClass('UserController');
$this->assertMatchedRouteName('user/default');
// get and assert mvc event
$mvcEvent = $this->getApplication()->getMvcEvent();
$this->assertEquals(true, $mvcEvent instanceof MvcEvent);
$this->assertEquals($mvcEvent->getApplication(), $this->getApplication());
// get and assert view controller
$viewModel = $mvcEvent->getResult();
$this->assertEquals(true, $viewModel instanceof ViewModel);
// testa variavel enviada na viewmodel
$var = $viewModel->getVariables();
$this->assertArrayHasKey('form', $var);
//Testa formulário na View
$this->assertInstanceOf('Zend\\Form\\Form', $var['form']);
}
public function test_redirect_editarAction()
{
$this->setupDB();
$this->dispatch('/cms/usuario/5/editar');
$this->assertResponseStatusCode(302);
$this->assertModuleName('User');
$this->assertControllerName('User\Controller\User');
$this->assertControllerClass('UserController');
$this->assertMatchedRouteName('user/default');
$this->assertRedirectTo('/cms/usuario');
}
public function test_POST_editarAction()
{
$this->setupDB();
$this->dispatch('/cms/usuario/1/editar', Request::METHOD_POST, array(
'name' => 'usuario_teste2',
'password' => '',
'confirmation' => '',
'email' => 'teste@teste.com',
'login' => 'teste_user',
'role' => 1,
'active' => true
));
$entity = $this->getEm()->getRepository('User\\Entity\\User')->find(1);
$this->assertEquals('usuario_teste2', $entity->getName());
$request = $this->getRequest();
$this->assertEquals($request->getMethod(), Request::METHOD_POST);
}
public function test_redirect_deleteAction_si_nao_for_XmlHttpRequest()
{
$this->setupDB();
$this->dispatch('/cms/usuario/1/delete');
$this->assertResponseStatusCode(302);
$this->assertModuleName('User');
$this->assertControllerName('User\Controller\User');
$this->assertControllerClass('UserController');
$this->assertMatchedRouteName('user/default');
$this->assertRedirectTo('/cms/usuario');
}
public function test_deleteAction()
{
$this->setupDB();
$this->dispatch('/cms/usuario/1/delete', Request::METHOD_GET, array(), true);
$this->assertResponseStatusCode(200);
$this->assertModuleName('User');
$this->assertControllerName('User\Controller\User');
$this->assertControllerClass('UserController');
$this->assertMatchedRouteName('user/default');
// get and assert mvc event
$mvcEvent = $this->getApplication()->getMvcEvent();
$this->assertEquals(true, $mvcEvent instanceof MvcEvent);
$this->assertEquals($mvcEvent->getApplication(), $this->getApplication());
// get and assert view controller
$viewModel = $mvcEvent->getResult();
$this->assertEquals(true, $viewModel instanceof JsonModel);
// testa variavel enviada na viewmodel
$var = $viewModel->getVariables();
$this->assertTrue($var[0]);
$this->dispatch('/cms/usuario/3/delete', Request::METHOD_GET, array(), true);
$mvcEvent = $this->getApplication()->getMvcEvent();
// get and assert view controller
$viewModel = $mvcEvent->getResult();
// testa variavel enviada na viewmodel
$var = $viewModel->getVariables();
$this->assertFalse($var[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment