From d3726c7c0e497def97efcf610fdcac9bbebb0f3e Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 5 Feb 2015 21:49:12 +0100 Subject: Add simple testsuite Signed-off-by: Florian Pritz --- application/tests/Test.php | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 application/tests/Test.php (limited to 'application/tests/Test.php') diff --git a/application/tests/Test.php b/application/tests/Test.php new file mode 100644 index 000000000..81225b312 --- /dev/null +++ b/application/tests/Test.php @@ -0,0 +1,98 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace tests; + +abstract class Test { + protected $t; + protected $server = ""; + + public function __construct() + { + require_once APPPATH."/third_party/test-more-php/Test-More-OO.php"; + $this->t = new \TestMore(); + $this->t->plan("no_plan"); + } + + public function setServer($server) + { + $this->server = $server; + } + + // Method: POST, PUT, GET etc + // Data: array("param" => "value") ==> index.php?param=value + // Source: http://stackoverflow.com/a/9802854/953022 + protected function CallAPI($method, $url, $data = false) + { + $curl = curl_init(); + + switch ($method) { + case "POST": + curl_setopt($curl, CURLOPT_POST, 1); + + if ($data) + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + break; + case "PUT": + curl_setopt($curl, CURLOPT_PUT, 1); + break; + default: + if ($data) + $url = sprintf("%s?%s", $url, http_build_query($data)); + } + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_HTTPHEADER, array( + "Accept: application/json", + )); + + $result = curl_exec($curl); + + curl_close($curl); + + $json = json_decode($result, true); + if ($json === NULL) { + $this->t->fail("json decode"); + $this->diagReply($result); + } + + return $json; + } + + protected function expectSuccess($testname, $reply) + { + if (!isset($reply["status"]) || $reply["status"] != "success") { + $this->t->fail($testname); + $this->diagReply($reply); + } else { + $this->t->pass($testname); + } + return $reply; + } + + protected function diagReply($reply) + { + $this->t->diag("Request got unexpected response:"); + $this->t->diag(var_export($reply, true)); + } + + public function init() + { + } + + public function cleanup() + { + } + + public function __destruct() + { + $this->t->done_testing(); + } +} -- cgit v1.2.3-24-g4f1b From 5b225c751d60d79916da4a7db761f823e12148de Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 6 Feb 2015 12:42:08 +0100 Subject: Add more tests Signed-off-by: Florian Pritz --- application/tests/Test.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'application/tests/Test.php') diff --git a/application/tests/Test.php b/application/tests/Test.php index 81225b312..192061db9 100644 --- a/application/tests/Test.php +++ b/application/tests/Test.php @@ -66,9 +66,9 @@ abstract class Test { return $json; } - protected function expectSuccess($testname, $reply) + protected function excpectStatus($testname, $reply, $status) { - if (!isset($reply["status"]) || $reply["status"] != "success") { + if (!isset($reply["status"]) || $reply["status"] != $status) { $this->t->fail($testname); $this->diagReply($reply); } else { @@ -77,6 +77,16 @@ abstract class Test { return $reply; } + protected function expectSuccess($testname, $reply) + { + return $this->excpectStatus($testname, $reply, "success"); + } + + protected function expectError($testname, $reply) + { + return $this->excpectStatus($testname, $reply, "error"); + } + protected function diagReply($reply) { $this->t->diag("Request got unexpected response:"); -- cgit v1.2.3-24-g4f1b From 1e18ad9d38e0cf6e0e37d3c1ad66eace2bb82003 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Feb 2015 10:57:48 +0100 Subject: tests: Fix php dev server being slow Signed-off-by: Florian Pritz --- application/tests/Test.php | 1 + 1 file changed, 1 insertion(+) (limited to 'application/tests/Test.php') diff --git a/application/tests/Test.php b/application/tests/Test.php index 192061db9..e18aa9374 100644 --- a/application/tests/Test.php +++ b/application/tests/Test.php @@ -51,6 +51,7 @@ abstract class Test { curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Accept: application/json", + "Expect: ", )); $result = curl_exec($curl); -- cgit v1.2.3-24-g4f1b