created mocks for testing
This commit is contained in:
BIN
tests/Webservice/.MockWebserviceSubclass.php.swp
Normal file
BIN
tests/Webservice/.MockWebserviceSubclass.php.swp
Normal file
Binary file not shown.
BIN
tests/Webservice/.WebserviceTest.php.swp
Normal file
BIN
tests/Webservice/.WebserviceTest.php.swp
Normal file
Binary file not shown.
@@ -1,78 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace BSR\Webservice\Formatter;
|
||||
|
||||
abstract class Formatter {
|
||||
private static $formats = array();
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @param array $formats New available formats, array(mimetype => class)
|
||||
*/
|
||||
protected static function registerFormats(array $formats) {
|
||||
foreach($formats as $f) {
|
||||
self::$formats[$f] = get_called_class();
|
||||
}
|
||||
class FormatterTest extends TestCase
|
||||
{
|
||||
public function testFormatterAllwaysReturnsAJsonFormatter()
|
||||
{
|
||||
$formatter = Formatter::getFormatter();
|
||||
$this->assertEquals($formatter instanceof Json, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Formatter The formatter to use for this request
|
||||
*/
|
||||
public static function getFormatter() {
|
||||
self::loadFormatters();
|
||||
$format = self::getFormatFromHeader();
|
||||
|
||||
return new $format();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all formatters in the current directory
|
||||
*/
|
||||
private static function loadFormatters() {
|
||||
preg_match('/(.+)\\\([a-zA-Z0-9]+)/', get_called_class(), $parts);
|
||||
$us = $parts[2];
|
||||
$namespace = $parts[1];
|
||||
|
||||
$base = __DIR__.'/';
|
||||
$ext = '.php';
|
||||
$files = glob(sprintf('%s%s%s', $base, '*', $ext));
|
||||
foreach($files as $f) {
|
||||
$c = str_replace(array($base, $ext), '', $f);
|
||||
if($c !== $us) {
|
||||
$c = $namespace.'\\'.$c;
|
||||
call_user_func(array($c, 'init'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The class name to instantiate in accord to the Accept header
|
||||
*/
|
||||
private static function getFormatFromHeader() {
|
||||
//TODO this is ugly
|
||||
return 'BSR\Webservice\Formatter\Json';
|
||||
if(isset($_SERVER['HTTP_ACCEPT'])) {
|
||||
$formats = array_map(function($f) {
|
||||
$parts = explode(';', $f);
|
||||
$parts[1] = (isset($parts[1]) ? (float) preg_replace('/[^0-9\.]/', '', $parts[1]) : 1.0) * 100;
|
||||
return $parts;
|
||||
}, explode(',', $_SERVER['HTTP_ACCEPT']));
|
||||
|
||||
usort($formats, function($a, $b) { return $b[1] - $a[1]; });
|
||||
|
||||
foreach($formats as $f) {
|
||||
if(isset(self::$formats[$f[0]])) {
|
||||
return self::$formats[$f[0]];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 'BSR\Webservice\Formatter\Json';
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the content for the given data
|
||||
* @param array $data
|
||||
*/
|
||||
abstract public function render($data);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace BSR\Webservice\Formatter;
|
||||
|
||||
class Json extends Formatter {
|
||||
protected static function init() {
|
||||
self::registerFormats(array(
|
||||
'application/json',
|
||||
'application/x-json',
|
||||
));
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class JsonTest extends TestCase
|
||||
{
|
||||
protected $accept = array(
|
||||
'application/json',
|
||||
'application/x-json',
|
||||
);
|
||||
|
||||
public function testRenderEncodesDataIntoJsonFormat()
|
||||
{
|
||||
$format = new Json();
|
||||
$someArray = array(
|
||||
'asdf' => array(1, 'a', 'b', array()),
|
||||
'1' => array(3 => 'c'),
|
||||
0 => 'hello',
|
||||
);
|
||||
ob_start();
|
||||
$format->render($someArray);
|
||||
$jsonRepresentation = ob_get_clean();
|
||||
$this->assertSame(json_decode($jsonRepresentation, true), $someArray);
|
||||
}
|
||||
|
||||
public function render($data) {
|
||||
echo json_encode($data);
|
||||
public function testRegistersItsFormatAcceptHeader()
|
||||
{
|
||||
Formatter::getFormatter();
|
||||
$formats = Formatter::getRegisterdFormats();
|
||||
$this->assertSame(array_intersect($this->accept, array_keys($formats)), $this->accept);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
16
tests/Webservice/MockRenderer.php
Normal file
16
tests/Webservice/MockRenderer.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace BSR\Webservice;
|
||||
|
||||
use BSR\Webservice\Formatter\Formatter;
|
||||
|
||||
class MockRenderer extends Renderer
|
||||
{
|
||||
/**
|
||||
* Override to avoid sending headers and flushing buffers
|
||||
*/
|
||||
public function render($status, $data)
|
||||
{
|
||||
$formatter = Formatter::getFormatter();
|
||||
$formatter->render($data);
|
||||
}
|
||||
}
|
||||
10
tests/Webservice/MockWebserviceSubclass.php
Normal file
10
tests/Webservice/MockWebserviceSubclass.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace BSR\Webservice;
|
||||
|
||||
class MockWebserviceSubclass extends Webservice
|
||||
{
|
||||
public function someMockFunction($one, $two, $threeOpt = null)
|
||||
{
|
||||
return 'Some Mock Function Return';
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,11 @@
|
||||
namespace BSR\Webservice;
|
||||
|
||||
use BSR\Webservice\Formatter\Formatter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class Renderer {
|
||||
private static $statusMessages = array(
|
||||
class RendererTest extends TestCase
|
||||
{
|
||||
private $someData = array(
|
||||
200 => 'Ok',
|
||||
400 => 'Bad request',
|
||||
404 => 'Not Found',
|
||||
@@ -13,17 +15,15 @@ class Renderer {
|
||||
500 => 'Server Error',
|
||||
);
|
||||
|
||||
public function __construct() {
|
||||
public function testThatOutputBufferGetsInializedOnConstruction()
|
||||
{
|
||||
$text = 'This text should be captured by the renderers ob_start()';
|
||||
ob_start();
|
||||
}
|
||||
|
||||
public function render($status, $data) {
|
||||
header(sprintf('HTTP/1.0 %s %s', $status, self::$statusMessages[$status]));
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
ob_clean();
|
||||
flush();
|
||||
|
||||
$formatter = Formatter::getFormatter();
|
||||
$formatter->render($data);
|
||||
echo 'Rubish text, that is not intended to be outputed';
|
||||
ob_get_clean();
|
||||
$renderer = new Renderer();
|
||||
echo $text;
|
||||
$obcontent = ob_get_clean();
|
||||
$this->assertSame($text, $obcontent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,131 @@
|
||||
namespace BSR\Webservice;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use BSR\Utils\Logger\Logger;
|
||||
use BSR\Utils\Configuration\Configuration;
|
||||
|
||||
class WebserviceTest extends TestCase
|
||||
{
|
||||
private $webservice;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->webservice = new MockWebserviceSubclass('1.2.3');
|
||||
}
|
||||
|
||||
private function removeLogs()
|
||||
{
|
||||
$logsDir = realpath(dirname(Configuration::get('log.file')));
|
||||
$files = glob("$logsDir/*");
|
||||
foreach($files as $file){
|
||||
if(is_file($file)) unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see config/configuration.local.php for the mock renderer used
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testRunSavesTheRequestIntoALogFile()
|
||||
{
|
||||
$this->removeLogs();
|
||||
$log = Logger::getLastLogs();
|
||||
$this->assertEquals(Logger::NO_LOG_YET_MSG, $log);
|
||||
$this->webservice->run(false);
|
||||
$response = ob_get_clean();
|
||||
$log = Logger::getLastLogs();
|
||||
$this->assertNotEquals(Logger::NO_LOG_YET_MSG, $log);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see config/configuration.local.php for the mock renderer used
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testReturnsErrorNoArgumentsWhenNoParamtersInGetPostRequest()
|
||||
{
|
||||
$this->removeLogs();
|
||||
$log = Logger::getLastLogs();
|
||||
$this->assertEquals(Logger::NO_LOG_YET_MSG, $log);
|
||||
|
||||
$_GET = array(); $_POST = array();
|
||||
|
||||
$this->webservice->run(false);
|
||||
$response = ob_get_clean();
|
||||
$responseArray = json_decode($response, true);
|
||||
$this->assertEquals(100, $responseArray['error']['code']);
|
||||
$this->assertEquals('NoArguments', $responseArray['error']['name']);
|
||||
}
|
||||
|
||||
public function testDoesNotReturnErrorNoArgumentsWhenParamtersInGetRequest()
|
||||
{
|
||||
$this->removeLogs();
|
||||
$log = Logger::getLastLogs();
|
||||
$this->assertEquals(Logger::NO_LOG_YET_MSG, $log);
|
||||
|
||||
$_GET = array('a' => 'a1');
|
||||
|
||||
$this->webservice->run(false);
|
||||
$response = ob_get_clean();
|
||||
$responseArray = json_decode($response, true);
|
||||
$this->assertNotEquals(100, $responseArray['error']['code']);
|
||||
$this->assertNotEquals('NoArguments', $responseArray['error']['name']);
|
||||
}
|
||||
|
||||
public function testDoesNotReturnErrorNoArgumentsWhenParamtersInPostRequest()
|
||||
{
|
||||
$this->removeLogs();
|
||||
$log = Logger::getLastLogs();
|
||||
$this->assertEquals(Logger::NO_LOG_YET_MSG, $log);
|
||||
|
||||
$_POST = array('a' => 'a1');
|
||||
|
||||
$this->webservice->run(false);
|
||||
$response = ob_get_clean();
|
||||
$responseArray = json_decode($response, true);
|
||||
var_dump($responseArray);
|
||||
$this->assertNotEquals(100, $responseArray['error']['code']);
|
||||
$this->assertNotEquals('NoArguments', $responseArray['error']['name']);
|
||||
}
|
||||
|
||||
public function testReturnsErrorMissingMethodWhenNoFuncParamInRequest()
|
||||
{
|
||||
$this->removeLogs();
|
||||
$log = Logger::getLastLogs();
|
||||
$this->assertEquals(Logger::NO_LOG_YET_MSG, $log);
|
||||
|
||||
$_POST = array('a' => 'a1');
|
||||
|
||||
$this->webservice->run(false);
|
||||
$response = ob_get_clean();
|
||||
$responseArray = json_decode($response, true);
|
||||
$this->assertEquals(101, $responseArray['error']['code']);
|
||||
$this->assertEquals('MissingMethod', $responseArray['error']['name']);
|
||||
}
|
||||
|
||||
public function testDoesNotReturnErrorMissingMethodWhenFuncParamInRequest()
|
||||
{
|
||||
$this->removeLogs();
|
||||
$log = Logger::getLastLogs();
|
||||
$this->assertEquals(Logger::NO_LOG_YET_MSG, $log);
|
||||
|
||||
$_GET = array('func' => 'someMockFunction'); $_POST = array();
|
||||
|
||||
$this->webservice->run(false);
|
||||
$response = ob_get_clean();
|
||||
$responseArray = json_decode($response, true);
|
||||
$this->assertEquals(100, $responseArray['error']['code']);
|
||||
}
|
||||
|
||||
public function testRunThrowsAUsageExceptionWhenFuncParamIsNotCallableOnSubclass()
|
||||
{
|
||||
}
|
||||
|
||||
public function test
|
||||
public function testRunThrowsAUsageExceptionWhenMissingRequiredParamsInRequest()
|
||||
{
|
||||
}
|
||||
|
||||
public function testRunThrowsAUsageExceptionWhenTooManyParamsInRequest()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user