ttrss/tests/integration/ApiTest.php

131 lines
3.3 KiB
PHP
Raw Normal View History

<?php
use PHPUnit\Framework\TestCase;
/** @group integration */
final class ApiTest extends TestCase {
/** @var string */
private $api_url;
2023-10-29 06:42:53 +00:00
/** @var string */
private $sid;
function __construct() {
2023-10-29 05:58:13 +00:00
$this->api_url = getenv('API_URL');
parent::__construct();
}
2023-10-29 05:58:13 +00:00
/** @param array<mixed> $payload
* @return array<mixed>
*/
function api(array $payload) : ?array {
$ch = curl_init($this->api_url);
2023-10-29 07:46:01 +00:00
if ($this->sid)
$payload["sid"] = $this->sid;
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));
2023-10-29 06:42:53 +00:00
$resp = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
2023-10-29 06:42:53 +00:00
if ($status != 200) {
print("error: failed with HTTP status: $status");
return null;
}
curl_close($ch);
2023-10-29 06:42:53 +00:00
return json_decode($resp, true);
}
2023-10-29 07:46:01 +00:00
public function setUp(): void {
$this->test_login();
}
2023-10-29 06:42:53 +00:00
/** @param array<mixed> $resp */
public function common_assertions(array $resp) : void {
$this->assertArrayHasKey("content", $resp);
$this->assertArrayNotHasKey("error", $resp['content'], $resp['content']['error'] ?? '');
}
2023-10-29 05:58:13 +00:00
public function test_login() : void {
2023-10-29 06:42:53 +00:00
$resp = $this->api(["op" => "login", "user" => "test", "password" => "test"]);
$this->common_assertions($resp);
2023-10-29 06:42:53 +00:00
$this->assertArrayHasKey("session_id", $resp['content']);
$this->sid = $resp['content']['session_id'];
}
2023-10-29 05:58:13 +00:00
public function test_getVersion() : void {
2023-10-29 07:46:01 +00:00
$resp = $this->api(["op" => "getVersion"]);
$this->common_assertions($resp);
$this->assertArrayHasKey("version", $resp['content']);
}
2023-10-29 07:46:01 +00:00
public function test_getUnread() : void {
$resp = $this->api(["op" => "getUnread"]);
$this->common_assertions($resp);
$this->assertArrayHasKey("unread", $resp['content']);
}
2023-10-29 07:46:01 +00:00
public function test_subscribeToFeed() : void {
$resp = $this->api(["op" => "subscribeToFeed", "feed_url" => "https://tt-rss.org/feeds/tt-rss.xml"]);
2023-10-29 06:42:53 +00:00
$this->common_assertions($resp);
2023-10-29 07:46:01 +00:00
$this->assertArrayHasKey("feed_id", $resp['content']['status']);
}
2023-10-29 07:46:01 +00:00
public function test_getCounters() : void {
$resp = $this->api(["op" => "getCounters"]);
$this->common_assertions($resp);
foreach ($resp['content'] as $ctr) {
$this->assertIsArray($ctr);
foreach (["id", "counter"] as $k) {
$this->assertArrayHasKey($k, $ctr);
$this->assertNotNull($ctr[$k]);
}
}
}
public function test_getFeedTree() : void {
$resp = $this->api(["op" => "getFeedTree"]);
$this->assertArrayHasKey('categories', $resp['content']);
$this->assertArrayHasKey('items', $resp['content']['categories']);
foreach ($resp['content']['categories']['items'] as $cat) {
foreach (["id", "bare_id", "name", "items"] as $k) {
$this->assertArrayHasKey($k, $cat);
}
foreach ($cat['items'] as $feed) {
$this->assertIsArray($feed);
foreach (["id", "name", "unread", "bare_id"] as $k) {
$this->assertArrayHasKey($k, $feed);
$this->assertNotNull($feed[$k]);
}
}
}
}
public function test_getHeadlines() : void {
foreach (["0", "-1", "-2", "-3", "-4", "-6"] as $feed_id) {
$resp = $this->api(["op" => "getHeadlines", "feed_id" => $feed_id]);
$this->assertIsArray($resp);
}
}
}