diff --git a/api/run-tests.sh b/api/run-tests.sh new file mode 100755 index 00000000..487b82e1 --- /dev/null +++ b/api/run-tests.sh @@ -0,0 +1,2 @@ +phpunit --colors tests/models +phpunit --colors tests/controllers \ No newline at end of file diff --git a/api/tests/__lib__/Mock.php b/api/tests/__lib__/Mock.php index 91acb362..f15e86c0 100644 --- a/api/tests/__lib__/Mock.php +++ b/api/tests/__lib__/Mock.php @@ -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}()"); } } diff --git a/api/tests/__mocks__/ControllerMock.php b/api/tests/__mocks__/ControllerMock.php new file mode 100644 index 00000000..6f9376c5 --- /dev/null +++ b/api/tests/__mocks__/ControllerMock.php @@ -0,0 +1,12 @@ + parent::stub()->returns('mockRequestValue'), + 'checkUserLogged' => parent::stub()->returns(true) + )); + } +} \ No newline at end of file diff --git a/api/tests/__mocks__/RedBeanMock.php b/api/tests/__mocks__/RedBeanMock.php index a069bc8c..3a9872b5 100644 --- a/api/tests/__mocks__/RedBeanMock.php +++ b/api/tests/__mocks__/RedBeanMock.php @@ -1,32 +1,14 @@ 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]; + )); } } } diff --git a/api/tests/__mocks__/ResponseMock.php b/api/tests/__mocks__/ResponseMock.php new file mode 100644 index 00000000..568dbcdf --- /dev/null +++ b/api/tests/__mocks__/ResponseMock.php @@ -0,0 +1,12 @@ + parent::stub(), + 'respondError' => parent::stub() + )); + } +} \ No newline at end of file diff --git a/api/tests/__mocks__/SessionMock.php b/api/tests/__mocks__/SessionMock.php new file mode 100644 index 00000000..de102462 --- /dev/null +++ b/api/tests/__mocks__/SessionMock.php @@ -0,0 +1,27 @@ + 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), + )); + } +} \ No newline at end of file diff --git a/api/tests/__mocks__/SlimMock.php b/api/tests/__mocks__/SlimMock.php index c7837f96..740bb4e8 100644 --- a/api/tests/__mocks__/SlimMock.php +++ b/api/tests/__mocks__/SlimMock.php @@ -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()); } @@ -28,4 +31,4 @@ namespace Slim { return self::$instance; } } -} +} \ No newline at end of file diff --git a/api/tests/__mocks__/UserMock.php b/api/tests/__mocks__/UserMock.php new file mode 100644 index 00000000..b4447c85 --- /dev/null +++ b/api/tests/__mocks__/UserMock.php @@ -0,0 +1,21 @@ + 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; + } +} \ No newline at end of file diff --git a/api/tests/controllers/.gitkeep b/api/tests/controllers/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/api/tests/controllers/user/loginTest.php b/api/tests/controllers/user/loginTest.php new file mode 100644 index 00000000..46f56f60 --- /dev/null +++ b/api/tests/controllers/user/loginTest.php @@ -0,0 +1,55 @@ +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)); + } +}