Skip to content

Instantly share code, notes, and snippets.

@cezar08
Created August 30, 2014 17:20
Show Gist options
  • Select an option

  • Save cezar08/9c57ad0c0a745af2e8e4 to your computer and use it in GitHub Desktop.

Select an option

Save cezar08/9c57ad0c0a745af2e8e4 to your computer and use it in GitHub Desktop.
<?php
namespace Admin\Service;
use Core\Test\ServiceTestCase;
use Admin\Model\User;
use Zend\Authentication\AuthenticationService;
/**
* @group Service
*/
class AuthTest extends ServiceTestCase {
/**
* @expectedException \Exception
*/
public function testAuthenticateWithoutParams() {
$authService = $this->serviceManager->get('Admin\Service\Auth');
$authService->authenticate();
}
/**
* @expectedException \Exception
* @expectedExeptionMessage Parâmetros inválidos
*/
public function testAuthenticateEmptyParams() {
$authService = $this->serviceManager->get('Admin\Service\Auth');
$authService->authenticate(array());
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Login ou senha inválidos
*/
public function testAuthenticateInvalidParameters() {
$authService = $this->serviceManager->get('Admin\Service\Auth');
$authService->authenticate(array(
'login' => 'foo', 'password' => 'foo')
);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Login ou senha inválidos
*
*/
public function testAuthenticateInvalidPassword() {
$authService = $this->serviceManager->get('Admin\Service\Auth');
$user = $this->addUser();
$authService->authenticate(array(
'login' => $user->login, 'password' => 'foo')
);
}
public function testAuthenticateValidParams() {
$authService = $this->serviceManager->get('Admin\Service\Auth');
$user = $this->addUser();
$result = $authService->authenticate(
array('login' => $user->login, 'password' => '123456')
);
$this->assertTrue($result);
$auth = new AuthenticationService();
$this->assertEquals($auth->getIdentity(), $user->login);
$session = $this->serviceManager->get('Session');
$savedUser = $session->offsetGet('user');
$this->assertEquals($user->id, $savedUser->id);
}
public function tearDown() {
parent::tearDown();
$auth = new AuthenticationService();
$auth->clearIdentity();
}
public function testLogout() {
$authService = $this->serviceManager->get('Admin\Service\Auth');
$user = $this->addUser();
$result = $authService->authenticate(
array('login' => $user->login, 'password' => '123456')
);
$this->assertTrue($result);
$result = $authService->logout();
$this->assertTrue($result);
$auth = new AuthenticationService();
$this->assertNull($auth->getIdentity());
$session = $this->serviceManager->get('Session');
$savedUser = $session->offsetGet('user');
$this->assertNull($savedUser);
}
private function addUser() {
$user = new User();
$user->login = 'zoro';
$user->password = md5('123456');
$user->role = 'admin';
$this->getTable('Admin\Model\User')->save($user);
return $user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment