Merged in unit-testing-login-controller (pull request #8)

Unit testing login controller
This commit is contained in:
Ivan Diaz 2016-04-16 15:39:07 -03:00
commit 215c0d2063
10 changed files with 161 additions and 29 deletions

2
api/run-tests.sh Executable file
View File

@ -0,0 +1,2 @@
phpunit --colors tests/models
phpunit --colors tests/controllers

View File

@ -2,8 +2,8 @@
class Stub {
private $function;
private $timesCalled = 0;
private $lastArgs = null;
public $timesCalled = 0;
public $lastArgs = null;
public function __construct($function = null) {
$this->function = ($function === null) ? function (){} : $function;
@ -48,6 +48,24 @@ class Mock {
return new Stub;
}
public static function setStatics($statics) {
foreach ($statics as $key => $static) {
static::$functionList[$key] = $static;
}
}
public static function __callStatic($key, $arguments) {
if (static::$functionList[$key]) {
$function = static::$functionList[$key];
return call_user_func_array($function, $arguments);
}
}
public static function get($key) {
return static::$functionList[$key];
}
public function __construct($arguments = array()) {
if (!empty($arguments)) {
foreach ($arguments as $property => $argument) {
@ -71,7 +89,7 @@ class Mock {
public function __call($method, $arguments) {
if (isset($this->{$method}) && is_callable($this->{$method})) {
return call_user_func_array($this->{$method}, $arguments);
} else {
} else if (!self::__callStatic($method, $arguments)) {
throw new Exception("Fatal error: Call to undefined method stdObject::{$method}()");
}
}

View File

@ -0,0 +1,12 @@
<?php
class Controller extends \Mock {
public static $functionList = array();
public static function initStubs() {
parent::setStatics(array(
'request' => parent::stub()->returns('mockRequestValue'),
'checkUserLogged' => parent::stub()->returns(true)
));
}
}

View File

@ -1,32 +1,14 @@
<?php
namespace RedBeanPHP {
class Facade extends \Mock {
private static $functionList = array();
public static $functionList = array();
public static function initStubs(){
self::$functionList = array(
public static function initStubs() {
self::setStatics(array(
'trash' => parent::stub(),
'store' => parent::stub(),
'dispense' => parent::stub()->returns(array())
);
}
public static function setStatics($statics) {
foreach ($statics as $key => $static) {
self::$functionList[$key] = $static;
}
}
public static function __callStatic($key, $arguments) {
if (self::$functionList[$key]) {
$function = self::$functionList[$key];
return call_user_func_array($function, $arguments);
}
}
public static function get($key) {
return self::$functionList[$key];
));
}
}
}

View File

@ -0,0 +1,12 @@
<?php
class Response extends \Mock {
public static $functionList = array();
public static function initStubs() {
parent::setStatics(array(
'respondSuccess' => parent::stub(),
'respondError' => parent::stub()
));
}
}

View File

@ -0,0 +1,27 @@
<?php
class Session extends \Mock {
public static $functionList = array();
public static function initStubs() {
self::setStatics(array(
'getInstance' => parent::stub()->returns(self::getInstanceMock()),
));
}
public static function mockInstanceFunction($functionName, $functionMock) {
self::getInstance()->{$functionName} = $functionMock;
}
private static function getInstanceMock() {
return new \Mock(array(
'initSession' => parent::stub(),
'closeSession' => parent::stub(),
'createSession' => parent::stub(),
'getToken' => parent::stub()->returns('TEST_TOKEN'),
'sessionExists' => parent::stub()->returns(false),
'checkAuthentication' => parent::stub()->returns(true),
'isLoggedWithId' => parent::stub()->returns(true),
));
}
}

View File

@ -2,10 +2,11 @@
namespace Slim {
class Response extends \Mock {
protected static $instance;
public function __construct() {}
public static function getInstance() {
if (self::$instance === null ) {
if (self::$instance === null) {
self::$instance = new \Mock();
self::$instance->setBody = \Mock::stub();
self::$instance->finalize = \Mock::stub();
@ -17,10 +18,12 @@ namespace Slim {
class Slim extends \Mock {
protected static $instance;
public function __construct() {}
public function __construct() {
}
public static function getInstance() {
if (self::$instance === null ) {
if (self::$instance === null) {
self::$instance = new Slim();
self::$instance->response = \Mock::stub()->returns(Response::getInstance());
}

View File

@ -0,0 +1,21 @@
<?php
class User extends \Mock {
public static $functionList = array();
public static function initStubs() {
parent::setStatics(array(
'authenticate' => parent::stub()->returns(self::getUserInstanceMock()),
));
}
private static function getUserInstanceMock() {
$mockUserInstance = new \stdClass();
$mockUserInstance->id = 'MOCK_ID';
$mockUserInstance->email = 'MOCK_EMAIL';
$mockUserInstance->password = 'MOCK_PASSWORD';
$mockUserInstance->admin = 'MOCK_ADMIN_VALUE';
return $mockUserInstance;
}
}

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));
}
}