[Ivan Diaz] - API - Add user/login.php Test

This commit is contained in:
Ivan Diaz 2016-04-07 20:56:27 -03:00
parent 9c86153811
commit 0c77e8cdc4
1 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,55 @@
<?php
// MOCKS
include_once 'tests/__lib__/Mock.php';
include_once 'tests/__mocks__/ResponseMock.php';
include_once 'tests/__mocks__/ControllerMock.php';
include_once 'tests/__mocks__/SessionMock.php';
include_once 'tests/__mocks__/UserMock.php';
include_once 'models/ERRORS.php';
include_once 'controllers/user/login.php';
class LoginControllerTest extends PHPUnit_Framework_TestCase {
private $loginController;
protected function setUp() {
Session::initStubs();
Controller::initStubs();
User::initStubs();
Response::initStubs();
$this->loginController = new LoginController();
}
public function testShouldRespondErrorIfAlreadyLoggedIn() {
Session::mockInstanceFunction('sessionExists', \Mock::stub()->returns(true));
$this->loginController->handler();
$this->assertTrue(Response::get('respondError')->hasBeenCalledWithArgs(ERRORS::SESSION_EXISTS));
}
public function testShouldCreateSessionAndRespondSuccessIfCredentialsAreValid() {
Session::mockInstanceFunction('sessionExists', \Mock::stub()->returns(false));
$this->loginController->handler();
$this->assertTrue(Session::getInstance()->createSession->hasBeenCalledWithArgs('MOCK_ID'));
$this->assertTrue(Response::get('respondSuccess')->hasBeenCalledWithArgs(array(
'userId' => 'MOCK_ID',
'userEmail' => 'MOCK_EMAIL',
'userIsAdmin' => 'MOCK_ADMIN_VALUE',
'token' => 'TEST_TOKEN'
)));
}
public function testShouldRespondErrorIfCredentialsAreInvalid() {
User::setStatics(array(
'authenticate' => \Mock::stub()->returns(null)
));
$this->loginController->handler();
$this->assertTrue(Response::get('respondError')->hasBeenCalledWithArgs(ERRORS::INVALID_CREDENTIALS));
}
}