From 7cd2c5cac8344157f2d9cef6ca659f97d0600daf Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 29 Oct 2023 09:42:53 +0300 Subject: [PATCH] fix apitest --- tests/integration/ApiTest.php | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/tests/integration/ApiTest.php b/tests/integration/ApiTest.php index 80f8af983..67e7f0249 100644 --- a/tests/integration/ApiTest.php +++ b/tests/integration/ApiTest.php @@ -7,6 +7,9 @@ final class ApiTest extends TestCase { /** @var string */ private $api_url; + /** @var string */ + private $sid; + function __construct() { $this->api_url = getenv('API_URL'); @@ -25,34 +28,40 @@ final class ApiTest extends TestCase { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); - $response = curl_exec($ch); + $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($response, true); + return json_decode($resp, true); } - /** @param array $response */ - public function common_assertions(array $response) : void { - $this->assertArrayHasKey("content", $response); - $this->assertArrayNotHasKey("error", $response['content'], $response['content']['error']); + /** @param array $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 { - $response = $this->api(["op" => "login", "user" => "test", "password" => "test"]); + $resp = $this->api(["op" => "login", "user" => "test", "password" => "test"]); + $this->common_assertions($resp); - $this->common_assertions($response); + $this->assertArrayHasKey("session_id", $resp['content']); + $this->sid = $resp['content']['session_id']; } public function test_getVersion() : void { - $response = $this->api(["op" => "getVersion"]); - - $this->common_assertions($response); - + $this->test_login(); + $resp = $this->api(["op" => "getVersion", "sid" => $this->sid]); + $this->common_assertions($resp); + $this->assertArrayHasKey("version", $resp['content']); } - }