monitor/api/tests/Unit/Sync/CurlCodebergClientTest.php
fds2610-bot 857fc88834 Phase 1 der Neuausrichtung: REST-API fuer Tickets/Events/Systeme/KPIs/Agenten
Setzt Phase 1 aus Visualization.md um: Vanilla-PHP-REST-API mit SQLite-Persistenz,
statischen API-Keys pro Agent, Codeberg-Sync fuer Backlog/Done, PHPUnit-Tests und
vollstaendiger API.md-Dokumentation - Vorgaben aus Detailantworten.md (F1-F16)
umgesetzt. Entwicklung/Build/Test laufen komplett in Docker-Compose-Containern
(PHP 8.2 + SQLite + Composer + PHPUnit/pcov), analog zum NC-Gantt-Projekt ist auf
dem Host kein PHP/Composer noetig.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-27 19:38:52 +02:00

121 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
// Stubs fuer die curl_*-Funktionen im Namespace von CurlCodebergClient: PHP loest
// unqualifizierte Funktionsaufrufe zuerst im aktuellen Namespace auf, bevor es auf den
// globalen Namespace zurueckfaellt. Damit lassen sich HTTP-Aufrufe ohne echtes Netzwerk
// und ohne zusaetzliche Mocking-Bibliothek testen.
namespace Monitor\Api\Sync {
final class CurlStub
{
/** @var array<int, array{status: int, body: string|false, error?: string}> */
public static array $queue = [];
/** @var string[] */
public static array $requestedUrls = [];
public static function reset(): void
{
self::$queue = [];
self::$requestedUrls = [];
}
}
function curl_init(string $url = '')
{
CurlStub::$requestedUrls[] = $url;
$handle = new \stdClass();
$handle->response = array_shift(CurlStub::$queue) ?? ['status' => 200, 'body' => '[]'];
return $handle;
}
function curl_setopt_array($handle, array $options): bool
{
return true;
}
function curl_exec($handle)
{
return $handle->response['body'];
}
function curl_getinfo($handle, int $option)
{
return $handle->response['status'];
}
function curl_error($handle): string
{
return $handle->response['error'] ?? '';
}
function curl_close($handle): void
{
}
}
namespace Monitor\Api\Tests\Sync {
use Monitor\Api\Sync\CurlCodebergClient;
use Monitor\Api\Sync\CurlStub;
use PHPUnit\Framework\TestCase;
use RuntimeException;
final class CurlCodebergClientTest extends TestCase
{
protected function setUp(): void
{
CurlStub::reset();
}
public function testListIssuesParsesSinglePage(): void
{
CurlStub::$queue = [
['status' => 200, 'body' => json_encode([
['number' => 1, 'title' => 'A', 'body' => 'desc', 'labels' => [['name' => 'bug', 'color' => 'ff0000']]],
])],
];
$client = new CurlCodebergClient('https://codeberg.org/api/v1');
$issues = $client->listIssues('fds2610', 'monitor', 'open');
self::assertCount(1, $issues);
self::assertSame(1, $issues[0]['number']);
self::assertSame('#ff0000', $issues[0]['labels'][0]['color']);
self::assertStringContainsString('state=open', CurlStub::$requestedUrls[0]);
}
public function testListIssuesStopsWhenPageEmpty(): void
{
CurlStub::$queue = [
['status' => 200, 'body' => '[]'],
];
$client = new CurlCodebergClient('https://codeberg.org/api/v1');
$issues = $client->listIssues('fds2610', 'monitor', 'closed');
self::assertSame([], $issues);
self::assertCount(1, CurlStub::$requestedUrls);
}
public function testListIssuesSendsAuthorizationHeaderWhenTokenGiven(): void
{
CurlStub::$queue = [['status' => 200, 'body' => '[]']];
$client = new CurlCodebergClient('https://codeberg.org/api/v1', 'my-token');
$client->listIssues('fds2610', 'monitor', 'open');
self::assertNotEmpty(CurlStub::$requestedUrls);
}
public function testThrowsOnHttpError(): void
{
CurlStub::$queue = [['status' => 500, 'body' => 'boom']];
$this->expectException(RuntimeException::class);
(new CurlCodebergClient('https://codeberg.org/api/v1'))->listIssues('fds2610', 'monitor', 'open');
}
}
}