diff options
Diffstat (limited to 'tests_integration/TogglefavHandlerTest.php')
| -rw-r--r-- | tests_integration/TogglefavHandlerTest.php | 156 |
1 files changed, 44 insertions, 112 deletions
diff --git a/tests_integration/TogglefavHandlerTest.php b/tests_integration/TogglefavHandlerTest.php index c55d3e1..f369e6b 100644 --- a/tests_integration/TogglefavHandlerTest.php +++ b/tests_integration/TogglefavHandlerTest.php @@ -1,95 +1,8 @@ <?php use PHPUnit\Framework\TestCase; -final class TogglefavHandlerTest extends TestCase +final class TogglefavHandlerTest extends ApiTestCase { - private string $apiUrl; - private ?string $cookieFile = null; - - protected function setUp(): void - { - $this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php'; - $this->cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_'); - } - - protected function tearDown(): void - { - if ($this->cookieFile && file_exists($this->cookieFile)) { - unlink($this->cookieFile); - } - } - - /** - * Login and return a cookie file path ready for authenticated requests. - * @return string Path to the cookie jar file. - */ - private function loginAs(string $username, string $password): string - { - $ch = curl_init($this->apiUrl); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => http_build_query([ - 'op' => 'perform-login', - 'user' => $username, - 'password' => $password, - ]), - CURLOPT_COOKIEJAR => $this->cookieFile, - CURLOPT_COOKIEFILE => $this->cookieFile, - CURLOPT_TIMEOUT => 10, - CURLOPT_CONNECTTIMEOUT => 5, - ]); - $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"); - } - - $this->assertEquals(200, $status, "Login failed: $body"); - - $decoded = json_decode($body, true); - $this->assertArrayHasKey('result', $decoded); - $this->assertEquals('ok', $decoded['result']); - - return $this->cookieFile; - } - - /** - * Make an authenticated HTTP POST request and return [status_code, decoded_body]. - * @param array<string, string|int> $data - * @return array<int, mixed> - */ - private function authenticatedPost(string $endpoint, array $data): array - { - $ch = curl_init($endpoint); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => http_build_query($data), - CURLOPT_COOKIEFILE => $this->cookieFile, - CURLOPT_COOKIEJAR => $this->cookieFile, - CURLOPT_TIMEOUT => 10, - CURLOPT_CONNECTTIMEOUT => 5, - ]); - $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 ?? []]; - } - /** * Query the favorites table via PDO to check if a favorite exists. */ @@ -115,20 +28,10 @@ final class TogglefavHandlerTest extends TestCase public function test_togglefav_requires_authentication(): void { - $ch = curl_init($this->apiUrl); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => http_build_query([ - 'op' => 'togglefav', - 'id' => 2, - ]), - CURLOPT_TIMEOUT => 10, - CURLOPT_CONNECTTIMEOUT => 5, + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'togglefav', + 'id' => 2, ]); - $body = curl_exec($ch); - $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); $this->assertEquals(401, $status); } @@ -137,13 +40,21 @@ final class TogglefavHandlerTest extends TestCase public function test_togglefav_adds_favorite(): void { - $this->loginAs('test', 'test'); + // Login + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ]); + $this->assertEquals(200, $status); + $this->assertEquals('ok', $body['result'] ?? null); + $bookId = 2; // Adventures of Huckleberry Finn // Ensure clean state $this->removeFavorite($bookId); - [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'togglefav', 'id' => $bookId, ]); @@ -158,18 +69,25 @@ final class TogglefavHandlerTest extends TestCase public function test_togglefav_removes_favorite(): void { - $this->loginAs('test', 'test'); + // Login + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ]); + $this->assertEquals(200, $status); + $bookId = 3; // The Adventures of Sherlock Holmes // Ensure favorite exists $this->removeFavorite($bookId); - $this->authenticatedPost($this->apiUrl, [ + $this->post($this->apiUrl, [ 'op' => 'togglefav', 'id' => $bookId, ]); // Toggle off - [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'togglefav', 'id' => $bookId, ]); @@ -184,22 +102,29 @@ final class TogglefavHandlerTest extends TestCase public function test_togglefav_re_adds_after_removal(): void { - $this->loginAs('test', 'test'); + // Login + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ]); + $this->assertEquals(200, $status); + $bookId = 4; // Metamorphosis // Ensure favorite exists, then remove it $this->removeFavorite($bookId); - $this->authenticatedPost($this->apiUrl, [ + $this->post($this->apiUrl, [ 'op' => 'togglefav', 'id' => $bookId, ]); - $this->authenticatedPost($this->apiUrl, [ + $this->post($this->apiUrl, [ 'op' => 'togglefav', 'id' => $bookId, ]); // Toggle back on - [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'togglefav', 'id' => $bookId, ]); @@ -214,13 +139,20 @@ final class TogglefavHandlerTest extends TestCase public function test_togglefav_with_zero_id(): void { - $this->loginAs('test', 'test'); + // Login + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ]); + $this->assertEquals(200, $status); + $bookId = 0; // Ensure clean state $this->removeFavorite($bookId); - [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'togglefav', 'id' => $bookId, ]); |
