summaryrefslogtreecommitdiff
path: root/tests/integration/ApiTest.php
blob: 67e7f02495dadabad4f87015dca3591601c7273f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
use PHPUnit\Framework\TestCase;

/** @group integration */
final class ApiTest extends TestCase {

	/** @var string */
	private $api_url;

	/** @var string */
	private $sid;

	function __construct() {
		$this->api_url = getenv('API_URL');

		parent::__construct();
	}

	/** @param array<mixed> $payload
	 * @return array<mixed>
	 */
	function api(array $payload) : ?array {
		$ch = curl_init($this->api_url);

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-type: application/json"]);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));

		$resp = curl_exec($ch);

		$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

		if ($status != 200) {
			print("error: failed with HTTP status: $status");
			return null;
		}

		curl_close($ch);

		return json_decode($resp, true);
	}

	/** @param array<mixed> $resp */
	public function common_assertions(array $resp) : void {
		$this->assertArrayHasKey("content", $resp);
		$this->assertArrayNotHasKey("error", $resp['content'], $resp['content']['error'] ?? '');
	}

	public function test_login() : void {
		$resp = $this->api(["op" => "login", "user" => "test", "password" => "test"]);
		$this->common_assertions($resp);

		$this->assertArrayHasKey("session_id", $resp['content']);
		$this->sid = $resp['content']['session_id'];
	}

	public function test_getVersion() : void {

		$this->test_login();

		$resp = $this->api(["op" => "getVersion", "sid" => $this->sid]);
		$this->common_assertions($resp);
		$this->assertArrayHasKey("version", $resp['content']);
	}
}