From b39fd2f948d8bc43d0079ea17d9ecde8c5c54b7b Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Fri, 27 Nov 2015 19:58:05 -0300 Subject: [PATCH] [Ivan Diaz] - Add Mocks and Model tests --- server/__tests__/models/ResponseTest.php | 74 ---------------- .../{__tests__/lib => tests/__lib__}/Mock.php | 2 +- server/tests/__mocks__/RedBeanMock.php | 32 +++++++ .../__mocks__/SlimMock.php | 0 .../{__tests__/__lib__ => tests/lib}/Mock.php | 0 server/tests/models/DataStoreTest.php | 84 +++++++++++++++++++ .../models}/ResponseTest.php | 6 +- 7 files changed, 119 insertions(+), 79 deletions(-) delete mode 100644 server/__tests__/models/ResponseTest.php rename server/{__tests__/lib => tests/__lib__}/Mock.php (98%) create mode 100644 server/tests/__mocks__/RedBeanMock.php rename server/{__tests__ => tests}/__mocks__/SlimMock.php (100%) rename server/{__tests__/__lib__ => tests/lib}/Mock.php (100%) create mode 100644 server/tests/models/DataStoreTest.php rename server/{__tests__/__models__ => tests/models}/ResponseTest.php (95%) diff --git a/server/__tests__/models/ResponseTest.php b/server/__tests__/models/ResponseTest.php deleted file mode 100644 index b5c71e49..00000000 --- a/server/__tests__/models/ResponseTest.php +++ /dev/null @@ -1,74 +0,0 @@ - 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 similarity index 98% rename from server/__tests__/lib/Mock.php rename to server/tests/__lib__/Mock.php index 1e8318a9..ec5b5d88 100644 --- a/server/__tests__/lib/Mock.php +++ b/server/tests/__lib__/Mock.php @@ -34,7 +34,7 @@ class Stub { } public function hasBeenCalledWithArgs() { - $argumentsMatchAssertion = serialize(func_get_args()) === serialize($this->lastArgs); + $argumentsMatchAssertion = serialize(func_get_args()) === serialize($this->lastArgs[0]); return $this->timesCalled && $argumentsMatchAssertion; } diff --git a/server/tests/__mocks__/RedBeanMock.php b/server/tests/__mocks__/RedBeanMock.php new file mode 100644 index 00000000..187577f0 --- /dev/null +++ b/server/tests/__mocks__/RedBeanMock.php @@ -0,0 +1,32 @@ + 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 $function($arguments); + } + } + + public static function get($key) { + return self::$functionList[$key]; + } + } +} diff --git a/server/__tests__/__mocks__/SlimMock.php b/server/tests/__mocks__/SlimMock.php similarity index 100% rename from server/__tests__/__mocks__/SlimMock.php rename to server/tests/__mocks__/SlimMock.php diff --git a/server/__tests__/__lib__/Mock.php b/server/tests/lib/Mock.php similarity index 100% rename from server/__tests__/__lib__/Mock.php rename to server/tests/lib/Mock.php diff --git a/server/tests/models/DataStoreTest.php b/server/tests/models/DataStoreTest.php new file mode 100644 index 00000000..28838f23 --- /dev/null +++ b/server/tests/models/DataStoreTest.php @@ -0,0 +1,84 @@ + 0, + 'prop2' => 'hello' + ); + } + + public static function deleteDataStore($dataStore) { + return parent::deleteDataStore($dataStore); + } +} + +class DataStoreTest extends PHPUnit_Framework_TestCase { + + protected function setUp() { + RedBean::initStubs(); + + $this->instance = new DataStoreMock(); + } + + public function testContructor() { + $newInstance = new DataStoreMock($this->instance->getBeanInstance()); + + $this->assertEquals($newInstance->prop1, 0); + $this->assertEquals($newInstance->prop2, 'hello'); + } + + public function testDataStoreCustomData() { + $this->instance->setProperties(array( + 'prop3' => 'EXTRA_DATA' + )); + + $this->assertEquals($this->instance->prop1, 0); + $this->assertEquals($this->instance->prop2, 'hello'); + $this->assertEquals($this->instance->prop3, 'EXTRA_DATA'); + } + + public function testStore() { + $this->instance->store(); + $this->assertTrue(RedBean::get('store')->hasBeenCalled()); + } + + public function testGetDataStore() { + RedBean::setStatics(array( + 'findOne' => \Mock::stub()->returns(array('TEST_PROP' => 'TEST_VALUE')) + )); + + $dataStoreIntance = DataStoreMock::getDataStore('ID_VALUE'); + + $this->assertEquals($dataStoreIntance->TEST_PROP, 'TEST_VALUE'); + + $this->assertTrue(RedBean::get('findOne')->hasBeenCalledWithArgs( + 'MOCK_TABLE', + ':property=:value', + array( + ':property' => 'id', + ':value' => 'ID_VALUE' + ) + )); + } + + public function testDeleteDataStore() { + $beanInstance = $this->instance->getBeanInstance(); + DataStoreMock::deleteDataStore($this->instance); + + $this->assertTrue(RedBean::get('trash')->hasBeenCalledWithArgs($beanInstance)); + } +} diff --git a/server/__tests__/__models__/ResponseTest.php b/server/tests/models/ResponseTest.php similarity index 95% rename from server/__tests__/__models__/ResponseTest.php rename to server/tests/models/ResponseTest.php index b5c71e49..b7907e6c 100644 --- a/server/__tests__/__models__/ResponseTest.php +++ b/server/tests/models/ResponseTest.php @@ -1,10 +1,8 @@