From 9c861538118d9c209075c1a77367f08c5b683964 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Thu, 7 Apr 2016 20:54:47 -0300 Subject: [PATCH 1/3] [Ivan Diaz] - API - Update Mocks and Mock library --- .../controllers/.gitkeep => run-tests.sh} | 0 api/tests/__lib__/Mock.php | 24 ++++++++++++++--- api/tests/__mocks__/ControllerMock.php | 12 +++++++++ api/tests/__mocks__/RedBeanMock.php | 26 +++--------------- api/tests/__mocks__/ResponseMock.php | 12 +++++++++ api/tests/__mocks__/SessionMock.php | 27 +++++++++++++++++++ api/tests/__mocks__/SlimMock.php | 11 +++++--- api/tests/__mocks__/UserMock.php | 21 +++++++++++++++ api/tests/controllers/user/loginTest.php | 0 9 files changed, 104 insertions(+), 29 deletions(-) rename api/{tests/controllers/.gitkeep => run-tests.sh} (100%) create mode 100644 api/tests/__mocks__/ControllerMock.php create mode 100644 api/tests/__mocks__/ResponseMock.php create mode 100644 api/tests/__mocks__/SessionMock.php create mode 100644 api/tests/__mocks__/UserMock.php create mode 100644 api/tests/controllers/user/loginTest.php diff --git a/api/tests/controllers/.gitkeep b/api/run-tests.sh similarity index 100% rename from api/tests/controllers/.gitkeep rename to api/run-tests.sh 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/user/loginTest.php b/api/tests/controllers/user/loginTest.php new file mode 100644 index 00000000..e69de29b From 0c77e8cdc410406b2d312a687fbc84fe0b82a1d4 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Thu, 7 Apr 2016 20:56:27 -0300 Subject: [PATCH 2/3] [Ivan Diaz] - API - Add user/login.php Test --- api/tests/controllers/user/loginTest.php | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/api/tests/controllers/user/loginTest.php b/api/tests/controllers/user/loginTest.php index e69de29b..46f56f60 100644 --- a/api/tests/controllers/user/loginTest.php +++ 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)); + } +} From 1f7bba595ddfc192d142468e6c1812b17736c055 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Thu, 7 Apr 2016 20:56:38 -0300 Subject: [PATCH 3/3] [Ivan Diaz] - API - Add test runner script --- api/run-tests.sh | 2 ++ 1 file changed, 2 insertions(+) mode change 100644 => 100755 api/run-tests.sh diff --git a/api/run-tests.sh b/api/run-tests.sh old mode 100644 new mode 100755 index e69de29b..487b82e1 --- a/api/run-tests.sh +++ b/api/run-tests.sh @@ -0,0 +1,2 @@ +phpunit --colors tests/models +phpunit --colors tests/controllers \ No newline at end of file