about summary refs log tree commit diff stats
path: root/tests_integration/ApiTestCase.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests_integration/ApiTestCase.php')
-rw-r--r--tests_integration/ApiTestCase.php47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php
new file mode 100644
index 0000000..0cb7a3d
--- /dev/null
+++ b/tests_integration/ApiTestCase.php
@@ -0,0 +1,47 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+abstract class ApiTestCase extends TestCase {
+	protected string $apiUrl;
+
+	protected function setUp(): void
+	{
+		$this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php';
+	}
+
+	protected function tearDown(): void {
+
+	}
+
+	/**
+	 * Make an HTTP POST request and return [status_code, decoded_body].
+	 * @param array<string, string> $data
+	 * @return array<int, mixed>
+	 */
+	protected function post(string $endpoint, array $data, ?string $cookieFile = null): array
+	{
+		$ch = curl_init($endpoint);
+		curl_setopt_array($ch, [
+			CURLOPT_RETURNTRANSFER => true,
+			CURLOPT_POST           => true,
+			CURLOPT_POSTFIELDS     => http_build_query($data),
+			CURLOPT_TIMEOUT        => 10,
+			CURLOPT_CONNECTTIMEOUT => 5,
+			CURLOPT_COOKIEFILE     => $cookieFile ?? '',
+			CURLOPT_COOKIEJAR      => $cookieFile ?? '',
+		]);
+		$body    = curl_exec($ch);
+		$status  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+		$errno   = curl_errno($ch);
+		$error   = curl_error($ch);
+		curl_close($ch);
+
+		if ($errno) {
+			$this->fail("cURL error ($errno): $error");
+		}
+
+		/** @var array<string, mixed>|null $decoded */
+		$decoded = json_decode($body, true);
+		return [$status, $decoded ?? []];
+	}
+}