From 70ef99429394e142905a3058efc4c83a42517b67 Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Wed, 13 Sep 2017 00:50:17 -0400 Subject: 2 - Tests (#3) * Move autoload to separate script, add two unit tests * Add integration tests for Controller * Add unit test for Output * Fix Model\Result-as-a-method error * encode JSON output as utf-8, and retrieve as assoc array * Ignore utf-8 definitions --- test/Controller/ControllerTest.php | 96 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 test/Controller/ControllerTest.php (limited to 'test/Controller') 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 @@ +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() + ); + } +} -- cgit