[Ivan DIaz] - Add unit testing boostrap

This commit is contained in:
Ivan Diaz 2015-11-18 21:58:22 -03:00
parent 4873613799
commit fb1475957e
5 changed files with 328 additions and 0 deletions

View File

@ -0,0 +1,75 @@
<?php
class Stub {
private $function;
private $timesCalled = 0;
private $lastArgs = null;
public function __construct($function = null) {
$this->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}()");
}
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Slim {
class Response extends \Mock {
protected static $instance;
public function __construct() {}
public static function getInstance() {
if (self::$instance === null ) {
self::$instance = new \Mock();
self::$instance->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;
}
}
}

View File

@ -0,0 +1,74 @@
<?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));
}
}

View File

@ -0,0 +1,75 @@
<?php
class Stub {
private $function;
private $timesCalled = 0;
private $lastArgs = null;
public function __construct($function = null) {
$this->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}()");
}
}
}

View File

@ -0,0 +1,74 @@
<?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));
}
}