[Ivan Diaz] - Add Mocks and Model tests
This commit is contained in:
parent
c1b1ce20df
commit
b39fd2f948
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
include_once '__tests__/__lib__/Mock.php';
|
||||
include_once '__tests__/__mocks__/SlimMock.php';
|
||||
include_once 'models/Response.php';
|
||||
|
||||
//use \Mockery as Mockery;
|
||||
|
||||
class ResponseTest extends PHPUnit_Framework_TestCase {
|
||||
public function testErrorResponseFormat() {
|
||||
//Mock data
|
||||
$mockErrorValue = 'MOCK_ERROR_VALUE';
|
||||
$mockData = array('example' => 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));
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
namespace RedBeanPHP {
|
||||
class Facade extends \Mock {
|
||||
private static $functionList = array();
|
||||
|
||||
public static function initStubs(){
|
||||
self::$functionList = 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 $function($arguments);
|
||||
}
|
||||
}
|
||||
|
||||
public static function get($key) {
|
||||
return self::$functionList[$key];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
include_once 'tests/__lib__/Mock.php';
|
||||
include_once 'tests/__mocks__/SlimMock.php';
|
||||
include_once 'tests/__mocks__/RedBeanMock.php';
|
||||
include_once 'models/DataStore.php';
|
||||
|
||||
use RedBeanPHP\Facade as RedBean;
|
||||
|
||||
class DataStoreMock extends DataStore {
|
||||
const TABLE = 'MOCK_TABLE';
|
||||
const PROPERTIES = array(
|
||||
'prop1',
|
||||
'prop2',
|
||||
'prop3'
|
||||
);
|
||||
|
||||
public function getDefaultProperties() {
|
||||
return array(
|
||||
'prop1' => 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));
|
||||
}
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
<?php
|
||||
include_once '__tests__/__lib__/Mock.php';
|
||||
include_once '__tests__/__mocks__/SlimMock.php';
|
||||
include_once 'tests/__lib__/Mock.php';
|
||||
include_once 'tests/__mocks__/SlimMock.php';
|
||||
include_once 'models/Response.php';
|
||||
|
||||
//use \Mockery as Mockery;
|
||||
|
||||
class ResponseTest extends PHPUnit_Framework_TestCase {
|
||||
public function testErrorResponseFormat() {
|
||||
//Mock data
|
Loading…
Reference in New Issue