Created
November 4, 2012 13:58
-
-
Save eminetto/4012038 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
<?php | |
namespace Admin\Service; | |
use DateTime; | |
use Core\Test\ServiceTestCase; | |
use Admin\Model\User; | |
use Core\Model\EntityException; | |
use Zend\Authentication\AuthenticationService; | |
/** | |
* Testes do serviço Auth | |
* @category Admin | |
* @package Service | |
* @author Elton Minetto<[email protected]> | |
*/ | |
/** | |
* @group Service | |
*/ | |
class AuthTest extends ServiceTestCase | |
{ | |
/** | |
* Authenticação sem parâmetros | |
* @expectedException \Exception | |
* @return void | |
*/ | |
public function testAuthenticateWithoutParams() | |
{ | |
$authService = $this->serviceManager->get('Admin\Service\Auth'); | |
$authService->authenticate(); | |
} | |
/** | |
* Authenticação sem parâmetros | |
* @expectedException \Exception | |
* @expectedExceptionMessage Parâmetros inválidos | |
* @return void | |
*/ | |
public function testAuthenticateEmptyParams() | |
{ | |
$authService = $this->serviceManager->get('Admin\Service\Auth'); | |
$authService->authenticate(array()); | |
} | |
/** | |
* Teste da autenticação inválida | |
* @expectedException \Exception | |
* @expectedExceptionMessage Login ou senha inválidos | |
* @return void | |
*/ | |
public function testAuthenticateInvalidParameters() | |
{ | |
$authService = $this->serviceManager->get('Admin\Service\Auth'); | |
$authService->authenticate(array('username' => 'invalid', 'password' => 'invalid')); | |
} | |
/** | |
* Teste da autenticação Inválida | |
* @expectedException \Exception | |
* @expectedExceptionMessage Login ou senha inválidos | |
* @return void | |
*/ | |
public function testAuthenticateInvalidPassord() | |
{ | |
$authService = $this->serviceManager->get('Admin\Service\Auth'); | |
$user = $this->addUser(); | |
$authService->authenticate(array('username' => $user->username, 'password' => 'invalida')); | |
} | |
/** | |
* Teste da autenticação Válida | |
* @return void | |
*/ | |
public function testAuthenticateValidParams() | |
{ | |
$authService = $this->serviceManager->get('Admin\Service\Auth'); | |
$user = $this->addUser(); | |
$result = $authService->authenticate( | |
array('username' => $user->username, 'password' => 'apple') | |
); | |
$this->assertTrue($result); | |
//testar a se a authenticação foi criada | |
$auth = new AuthenticationService(); | |
$this->assertEquals($auth->getIdentity(), $user->username); | |
//verica se o usuário foi salvo na sessão | |
$session = $this->serviceManager->get('Session'); | |
$savedUser = $session->offsetGet('user'); | |
$this->assertEquals($user->id, $savedUser->id); | |
} | |
/** | |
* Limpa a autenticação depois de cada teste | |
* @return void | |
*/ | |
public function tearDown() | |
{ | |
parent::tearDown(); | |
$auth = new AuthenticationService(); | |
$auth->clearIdentity(); | |
} | |
/** | |
* Teste do logout | |
* @return void | |
*/ | |
public function testLogout() | |
{ | |
$authService = $this->serviceManager->get('Admin\Service\Auth'); | |
$user = $this->addUser(); | |
$result = $authService->authenticate( | |
array('username' => $user->username, 'password' => 'apple') | |
); | |
$this->assertTrue($result); | |
$result = $authService->logout(); | |
$this->assertTrue($result); | |
//verifica se removeu a identidade da autenticação | |
$auth = new AuthenticationService(); | |
$this->assertNull($auth->getIdentity()); | |
//verifica se o usuário foi removido da sessão | |
$session = $this->serviceManager->get('Session'); | |
$savedUser = $session->offsetGet('user'); | |
$this->assertNull($savedUser); | |
} | |
private function addUser() | |
{ | |
$user = new User(); | |
$user->username = 'steve'; | |
$user->password = md5('apple'); | |
$user->name = 'Steve <b>Jobs</b>'; | |
$user->valid = 1; | |
$user->role = 'admin'; | |
$saved = $this->getTable('Admin\Model\User')->save($user); | |
return $saved; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment