diff options
| -rw-r--r-- | autoload.php | 19 | ||||
| -rw-r--r-- | index.php | 16 | ||||
| -rw-r--r-- | private/View/Output.php | 3 | ||||
| -rw-r--r-- | test/Controller/ControllerTest.php | 96 | ||||
| -rw-r--r-- | test/Model/QueryTest.php | 42 | ||||
| -rw-r--r-- | test/Model/ResultTest.php | 37 | ||||
| -rw-r--r-- | test/View/OutputTest.php | 33 |
7 files changed, 229 insertions, 17 deletions
diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..b9d471b --- /dev/null +++ b/autoload.php @@ -0,0 +1,19 @@ +<?php + +declare(strict_types=1); + +// Borrowed and modified from PSR-4 Closure Example +spl_autoload_register( + function ($class) { + $prefix = 'AugustOffensive\\'; + $relative_class = substr($class, strlen($prefix)); + + // find file in /private/ in respective namespace path + $file = __DIR__ . '/private/' . str_replace('\\', '/', $relative_class) . '.php'; + + // if the file exists, require it + if (file_exists($file)) { + require $file; + } + } +); @@ -4,21 +4,7 @@ declare(strict_types=1); namespace AugustOffensive; -// Borrowed and modified from PSR-4 Closure Example -spl_autoload_register( - function ($class) { - $prefix = 'AugustOffensive\\'; - $relative_class = substr($class, strlen($prefix)); - - // find file in /private/ in respective namespace path - $file = __DIR__ . '/private/' . str_replace('\\', '/', $relative_class) . '.php'; - - // if the file exists, require it - if (file_exists($file)) { - require $file; - } - } -); +require_once "autoload.php"; use AugustOffensive\View; use AugustOffensive\Controller; diff --git a/private/View/Output.php b/private/View/Output.php index 8773e42..af9975e 100644 --- a/private/View/Output.php +++ b/private/View/Output.php @@ -15,11 +15,10 @@ class Output * Formats the result into JSON. * * @param Model\Result $result The result to return to the client. - * @param bool $prepare Whether to set the content type of the response (default true). * * @return string */ - public static function json (Model\Result $result, bool $prepare = true): string + public static function json (Model\Result $result): string { return json_encode(array( "Result-Type" => $result->getResultType(), diff --git a/test/Controller/ControllerTest.php b/test/Controller/ControllerTest.php new file mode 100644 index 0000000..1517b64 --- /dev/null +++ b/test/Controller/ControllerTest.php @@ -0,0 +1,96 @@ +<?php + +declare(strict_types=1); + +namespace AugustOffensive\Controller; + +use PHPUnit\Framework\TestCase; +use AugustOffensive\Model; + +/** + * Integration test: requires DB connection. Expect side effects (use test db if possible). + * + * @covers Controller + */ +final class ControllerTest extends \PHPUnit\Framework\TestCase +{ + public function testDBConnection() + { + try { + $this->assertInstanceOf( + Model\Connection::class, + Controller::initiateConnection() + ); + } catch (\PDOException $err) { + $this->fail("Database not initialized correctly: " . $err->getMessage()); + } + } + + public function testCreateQuery() + { + $path = array("api", "create", "query"); + $request = "DELETE"; + $content = array("c" => "cherry", "d" => "dike"); + $query = Controller::createQuery($path, $request, $content); + + $this->assertInstanceOf( + Model\Query::class, + $query + ); + + $this->assertEquals( + $path, + $query->getPath() + ); + $this->assertEquals( + $request, + $query->getRequest() + ); + $this->assertEquals( + $content, + $query->getContent() + ); + } + + public function testCreateResult() + { + + $resultType = "TYPE"; + $result = array("no", "values"); + $resultObject = Controller::createResult($resultType, $result); + + $this->assertInstanceOf( + Model\Result::class, + $resultObject + ); + + $this->assertEquals( + $resultType, + $resultObject->getResultType() + ); + $this->assertEquals( + $result, + $resultObject->getResult() + ); + } + + public function testErrorResult() + { + $message = "Oh no! Oops!"; + $errorResult = Controller::errorResult(new \Exception($message)); + + $this->assertInstanceOf( + Model\Result::class, + $errorResult + ); + + $this->assertEquals( + "ERROR", + $errorResult->getResultType() + ); + $this->assertEquals( + array("error" => $message), + $errorResult->getResult() + ); + } +} diff --git a/test/Model/QueryTest.php b/test/Model/QueryTest.php new file mode 100644 index 0000000..ef6cda7 --- /dev/null +++ b/test/Model/QueryTest.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +namespace AugustOffensive\Model; + +use PHPUnit\Framework\TestCase; + +/** + * @covers Query + */ +final class QueryTest extends \PHPUnit\Framework\TestCase +{ + public function testCanBeCreated() + { + $this->assertInstanceOf( + Query::class, + new Query(array("api", "path"), "GET", array()) + ); + } + + public function testHasAccessibleValues() + { + $path = array("api", "query", "path"); + $request = "PUT"; + $content = array("a" => "apple", "b" => "bearing"); + $query = new Query($path, $request, $content); + + $this->assertEquals( + $path, + $query->getPath() + ); + $this->assertEquals( + $request, + $query->getRequest() + ); + $this->assertEquals( + $content, + $query->getContent() + ); + } +} diff --git a/test/Model/ResultTest.php b/test/Model/ResultTest.php new file mode 100644 index 0000000..0380182 --- /dev/null +++ b/test/Model/ResultTest.php @@ -0,0 +1,37 @@ +<?php + +declare(strict_types=1); + +namespace AugustOffensive\Model; + +use PHPUnit\Framework\TestCase; + +/** + * @covers Result + */ +final class ResultTest extends \PHPUnit\Framework\TestCase +{ + public function testCanBeCreated() + { + $this->assertInstanceOf( + Result::class, + new Result("TEST_SUCCESS", array("it worked")) + ); + } + + public function testHasAccessibleValues() + { + $resultType = "VISIBLE"; + $result = array("array", "of", "values"); + $resultObject = new Result($resultType, $result); + + $this->assertEquals( + $resultType, + $resultObject->getResultType() + ); + $this->assertEquals( + $result, + $resultObject->getResult() + ); + } +} diff --git a/test/View/OutputTest.php b/test/View/OutputTest.php new file mode 100644 index 0000000..f687ee8 --- /dev/null +++ b/test/View/OutputTest.php @@ -0,0 +1,33 @@ +<?php + +declare(strict_types=1); + +namespace AugustOffensive\View; + +use PHPUnit\Framework\TestCase; +use AugustOffensive\Model; + +/** + * @covers Output + */ +final class OutputTest extends \PHPUnit\Framework\TestCase +{ + public function testJSONOutput() + { + $resultType = "JSON_ENCODED"; + $result = array("json", "1.6"); + $resultObject = new Model\Result($resultType, $result); + + // If JSON is properly formatted, we can decode and test the values + $output = json_decode(Output::json($resultObject), true); + + $this->assertEquals( + $resultType, + $output["Result-Type"] + ); + $this->assertEquals( + $result, + $output["Content"] + ); + } +}
\ No newline at end of file |
