diff --git a/server/__tests__/__lib__/Mock.php b/server/__tests__/__lib__/Mock.php new file mode 100644 index 00000000..1e8318a9 --- /dev/null +++ b/server/__tests__/__lib__/Mock.php @@ -0,0 +1,75 @@ +function = ($function === null) ? function (){} : $function; + } + + public function __invoke() { + $this->timesCalled++; + $this->lastArgs = func_get_args(); + + return call_user_func_array($this->function, func_get_args()); + } + + public function returns($arg) { + $this->function = function () use ($arg) { + return $arg; + }; + + return $this; + } + + public function hasBeenCalled() { + return !!$this->timesCalled; + } + + public function reset() { + $this->timesCalled = 0; + $this->lastArgs = null; + } + + public function hasBeenCalledWithArgs() { + $argumentsMatchAssertion = serialize(func_get_args()) === serialize($this->lastArgs); + + return $this->timesCalled && $argumentsMatchAssertion; + } +} + +class Mock { + public static function stub() { + return new Stub; + } + + public function __construct($arguments = array()) { + if (!empty($arguments)) { + foreach ($arguments as $property => $argument) { + if ($argument instanceOf Closure) { + $this->{$property} = self::stub($argument); + } else { + $this->{$property} = $argument; + } + } + } + } + + public function __set($key, $value){ + if ($value instanceOf Closure) { + $this->{$key} = self::stub($value); + } else { + $this->{$key} = $value; + } + } + + public function __call($method, $arguments) { + if (isset($this->{$method}) && is_callable($this->{$method})) { + return call_user_func_array($this->{$method}, $arguments); + } else { + throw new Exception("Fatal error: Call to undefined method stdObject::{$method}()"); + } + } +} diff --git a/server/__tests__/__mocks__/SlimMock.php b/server/__tests__/__mocks__/SlimMock.php new file mode 100644 index 00000000..b9bac85b --- /dev/null +++ b/server/__tests__/__mocks__/SlimMock.php @@ -0,0 +1,30 @@ +setBody = \Mock::stub(); + } + + return self::$instance; + } + } + + class Slim extends \Mock { + protected static $instance; + public function __construct() {} + + public static function getInstance() { + if (self::$instance === null ) { + self::$instance = new Slim(); + self::$instance->response = \Mock::stub()->returns(Response::getInstance()); + } + + return self::$instance; + } + } +} diff --git a/server/__tests__/__models__/ResponseTest.php b/server/__tests__/__models__/ResponseTest.php new file mode 100644 index 00000000..b5c71e49 --- /dev/null +++ b/server/__tests__/__models__/ResponseTest.php @@ -0,0 +1,74 @@ + true); + $expectedArgument = json_encode(array( + 'status' => 'fail', + 'message' => $mockErrorValue, + 'data' => $mockData + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondError($mockErrorValue, $mockData); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } + + public function testErrorResponseFormatWithoutData() { + //Mock data + $mockErrorValue = 'MOCK_ERROR_VALUE'; + $expectedArgument = json_encode(array( + 'status' => 'fail', + 'message' => $mockErrorValue, + 'data' => null + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondError($mockErrorValue); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } + + public function testSuccessFormat() { + //Mock data + $mockData = array('example' => true); + $expectedArgument = json_encode(array( + 'status' => 'success', + 'data' => $mockData + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondSuccess($mockData); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } + + public function testSuccessFormatWithoutData() { + //Mock data + $expectedArgument = json_encode(array( + 'status' => 'success', + 'data' => null + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondSuccess(); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } +} diff --git a/server/__tests__/lib/Mock.php b/server/__tests__/lib/Mock.php new file mode 100644 index 00000000..1e8318a9 --- /dev/null +++ b/server/__tests__/lib/Mock.php @@ -0,0 +1,75 @@ +function = ($function === null) ? function (){} : $function; + } + + public function __invoke() { + $this->timesCalled++; + $this->lastArgs = func_get_args(); + + return call_user_func_array($this->function, func_get_args()); + } + + public function returns($arg) { + $this->function = function () use ($arg) { + return $arg; + }; + + return $this; + } + + public function hasBeenCalled() { + return !!$this->timesCalled; + } + + public function reset() { + $this->timesCalled = 0; + $this->lastArgs = null; + } + + public function hasBeenCalledWithArgs() { + $argumentsMatchAssertion = serialize(func_get_args()) === serialize($this->lastArgs); + + return $this->timesCalled && $argumentsMatchAssertion; + } +} + +class Mock { + public static function stub() { + return new Stub; + } + + public function __construct($arguments = array()) { + if (!empty($arguments)) { + foreach ($arguments as $property => $argument) { + if ($argument instanceOf Closure) { + $this->{$property} = self::stub($argument); + } else { + $this->{$property} = $argument; + } + } + } + } + + public function __set($key, $value){ + if ($value instanceOf Closure) { + $this->{$key} = self::stub($value); + } else { + $this->{$key} = $value; + } + } + + public function __call($method, $arguments) { + if (isset($this->{$method}) && is_callable($this->{$method})) { + return call_user_func_array($this->{$method}, $arguments); + } else { + throw new Exception("Fatal error: Call to undefined method stdObject::{$method}()"); + } + } +} diff --git a/server/__tests__/models/ResponseTest.php b/server/__tests__/models/ResponseTest.php new file mode 100644 index 00000000..b5c71e49 --- /dev/null +++ b/server/__tests__/models/ResponseTest.php @@ -0,0 +1,74 @@ + true); + $expectedArgument = json_encode(array( + 'status' => 'fail', + 'message' => $mockErrorValue, + 'data' => $mockData + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondError($mockErrorValue, $mockData); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } + + public function testErrorResponseFormatWithoutData() { + //Mock data + $mockErrorValue = 'MOCK_ERROR_VALUE'; + $expectedArgument = json_encode(array( + 'status' => 'fail', + 'message' => $mockErrorValue, + 'data' => null + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondError($mockErrorValue); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } + + public function testSuccessFormat() { + //Mock data + $mockData = array('example' => true); + $expectedArgument = json_encode(array( + 'status' => 'success', + 'data' => $mockData + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondSuccess($mockData); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } + + public function testSuccessFormatWithoutData() { + //Mock data + $expectedArgument = json_encode(array( + 'status' => 'success', + 'data' => null + )); + $responseInstance = \Slim\Slim::getInstance()->response(); + + //Execute Response + Response::respondSuccess(); + + //Should have been called with expected format + $this->assertTrue($responseInstance->setBody->hasBeenCalledWithArgs($expectedArgument)); + } +}