summaryrefslogtreecommitdiffstats
path: root/application/tests/Test.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2015-02-15 11:11:49 +0100
committerFlorian Pritz <bluewind@xinu.at>2015-02-15 11:11:49 +0100
commit45c16c802720faf9de6c3028ba41753c5edba974 (patch)
treee20148091cf0f9b6ff11efe65a76cfe1d1bad24c /application/tests/Test.php
parent9535ede862e01d834ebdd553184b1f6544b06d2c (diff)
parent01226a9afd760a920e9cb3377913ee296f0ab2ca (diff)
Merge branch 'api-rework' into working
Diffstat (limited to 'application/tests/Test.php')
-rw-r--r--application/tests/Test.php109
1 files changed, 109 insertions, 0 deletions
diff --git a/application/tests/Test.php b/application/tests/Test.php
new file mode 100644
index 000000000..e18aa9374
--- /dev/null
+++ b/application/tests/Test.php
@@ -0,0 +1,109 @@
+<?php
+/*
+ * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * 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",
+ "Expect: ",
+ ));
+
+ $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 excpectStatus($testname, $reply, $status)
+ {
+ if (!isset($reply["status"]) || $reply["status"] != $status) {
+ $this->t->fail($testname);
+ $this->diagReply($reply);
+ } else {
+ $this->t->pass($testname);
+ }
+ 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:");
+ $this->t->diag(var_export($reply, true));
+ }
+
+ public function init()
+ {
+ }
+
+ public function cleanup()
+ {
+ }
+
+ public function __destruct()
+ {
+ $this->t->done_testing();
+ }
+}