+
+
+
+
+
\ No newline at end of file
diff --git a/templates/panel.html b/templates/panel.html
new file mode 100644
index 0000000..207c655
--- /dev/null
+++ b/templates/panel.html
@@ -0,0 +1,11 @@
+
+
+
{{ title }}
+
+
+ {{ content }}
+
+
+
\ No newline at end of file
diff --git a/templates/param_help.html b/templates/param_help.html
new file mode 100644
index 0000000..df14e71
--- /dev/null
+++ b/templates/param_help.html
@@ -0,0 +1,5 @@
+
{{ name }} {{ optional }}
+
+ {{ type }}
+ {{ doc }}
+
\ No newline at end of file
diff --git a/templates/return_help.html b/templates/return_help.html
new file mode 100644
index 0000000..8f361cf
--- /dev/null
+++ b/templates/return_help.html
@@ -0,0 +1,2 @@
+{{ type }}
+{{ doc }}
diff --git a/tests/Webservice/.MockWebserviceSubclass.php.swp b/tests/Webservice/.MockWebserviceSubclass.php.swp
new file mode 100644
index 0000000..4245311
Binary files /dev/null and b/tests/Webservice/.MockWebserviceSubclass.php.swp differ
diff --git a/tests/Webservice/.WebserviceTest.php.swp b/tests/Webservice/.WebserviceTest.php.swp
new file mode 100644
index 0000000..5a8eca6
Binary files /dev/null and b/tests/Webservice/.WebserviceTest.php.swp differ
diff --git a/tests/Webservice/Formatter/FormatterTest.php b/tests/Webservice/Formatter/FormatterTest.php
index f9895db..dbe15fc 100644
--- a/tests/Webservice/Formatter/FormatterTest.php
+++ b/tests/Webservice/Formatter/FormatterTest.php
@@ -1,78 +1,13 @@
class)
- */
- protected static function registerFormats(array $formats) {
- foreach($formats as $f) {
- self::$formats[$f] = get_called_class();
- }
- }
-
- /**
- * @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];
+use PHPUnit\Framework\TestCase;
- $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'));
- }
- }
+class FormatterTest extends TestCase
+{
+ public function testFormatterAllwaysReturnsAJsonFormatter()
+ {
+ $formatter = Formatter::getFormatter();
+ $this->assertEquals($formatter instanceof Json, true);
}
-
- /**
- * @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);
}
diff --git a/tests/Webservice/Formatter/JsonTest.php b/tests/Webservice/Formatter/JsonTest.php
index 45d46a8..eda054c 100644
--- a/tests/Webservice/Formatter/JsonTest.php
+++ b/tests/Webservice/Formatter/JsonTest.php
@@ -1,17 +1,34 @@
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);
}
}
diff --git a/tests/Webservice/MockRenderer.php b/tests/Webservice/MockRenderer.php
new file mode 100644
index 0000000..a284274
--- /dev/null
+++ b/tests/Webservice/MockRenderer.php
@@ -0,0 +1,16 @@
+render($data);
+ }
+}
diff --git a/tests/Webservice/MockWebserviceSubclass.php b/tests/Webservice/MockWebserviceSubclass.php
new file mode 100644
index 0000000..06fde41
--- /dev/null
+++ b/tests/Webservice/MockWebserviceSubclass.php
@@ -0,0 +1,10 @@
+ '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);
}
}
diff --git a/tests/Webservice/WebserviceTest.php b/tests/Webservice/WebserviceTest.php
index 4035af5..464334e 100644
--- a/tests/Webservice/WebserviceTest.php
+++ b/tests/Webservice/WebserviceTest.php
@@ -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);
+ }
}
- public function test
+
+ /**
+ * @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 testRunThrowsAUsageExceptionWhenMissingRequiredParamsInRequest()
+ {
+ }
+
+ public function testRunThrowsAUsageExceptionWhenTooManyParamsInRequest()
+ {
+ }
}