feat(cloud): repository interfaces + in-memory test doubles
This commit is contained in:
parent
6fbe4015dc
commit
67b9999a31
11 changed files with 179 additions and 0 deletions
8
cloud/src/Repository/AuditRepository.php
Normal file
8
cloud/src/Repository/AuditRepository.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface AuditRepository
|
||||||
|
{
|
||||||
|
public function append(string $actor, string $action, string $target, array $before, array $after): void;
|
||||||
|
}
|
||||||
10
cloud/src/Repository/DeviceRepository.php
Normal file
10
cloud/src/Repository/DeviceRepository.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface DeviceRepository
|
||||||
|
{
|
||||||
|
public function secretFor(string $deviceId): ?string;
|
||||||
|
public function touchHeartbeat(string $deviceId, array $meta): void;
|
||||||
|
public function configVersion(string $deviceId): int;
|
||||||
|
}
|
||||||
11
cloud/src/Repository/EventRepository.php
Normal file
11
cloud/src/Repository/EventRepository.php
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface EventRepository
|
||||||
|
{
|
||||||
|
public function insert(array $event): int;
|
||||||
|
public function recent(int $limit, ?string $severity): array;
|
||||||
|
public function find(int $id): ?array;
|
||||||
|
public function acknowledge(int $id, string $user): bool;
|
||||||
|
}
|
||||||
9
cloud/src/Repository/NonceRepository.php
Normal file
9
cloud/src/Repository/NonceRepository.php
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface NonceRepository
|
||||||
|
{
|
||||||
|
public function seen(string $nonce): bool;
|
||||||
|
public function remember(string $nonce, int $expiresAt): void;
|
||||||
|
}
|
||||||
9
cloud/src/Repository/RateLimiterRepository.php
Normal file
9
cloud/src/Repository/RateLimiterRepository.php
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface RateLimiterRepository
|
||||||
|
{
|
||||||
|
/** 해당 window(분)의 key 카운트를 1 증가시키고, 증가 후 카운트를 반환한다. */
|
||||||
|
public function hit(string $key, int $windowStart): int;
|
||||||
|
}
|
||||||
10
cloud/src/Repository/SnapshotRepository.php
Normal file
10
cloud/src/Repository/SnapshotRepository.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface SnapshotRepository
|
||||||
|
{
|
||||||
|
public function add(int $eventId, string $fileUuid, string $contentHash): void;
|
||||||
|
public function forEvent(int $eventId): array;
|
||||||
|
public function pathByUuid(string $uuid): ?string;
|
||||||
|
}
|
||||||
8
cloud/src/Repository/ThresholdRepository.php
Normal file
8
cloud/src/Repository/ThresholdRepository.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface ThresholdRepository
|
||||||
|
{
|
||||||
|
public function forDevice(string $deviceId): array;
|
||||||
|
}
|
||||||
10
cloud/src/Repository/UserRepository.php
Normal file
10
cloud/src/Repository/UserRepository.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface UserRepository
|
||||||
|
{
|
||||||
|
public function byUsername(string $username): ?array;
|
||||||
|
public function recordFailure(int $userId, int $lockUntil): void;
|
||||||
|
public function clearFailures(int $userId): void;
|
||||||
|
}
|
||||||
11
cloud/src/Repository/ZoneRepository.php
Normal file
11
cloud/src/Repository/ZoneRepository.php
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
interface ZoneRepository
|
||||||
|
{
|
||||||
|
public function forDevice(string $deviceId): array;
|
||||||
|
public function forCamera(string $cameraId): array;
|
||||||
|
public function save(array $zone): string;
|
||||||
|
public function delete(string $zoneId): bool;
|
||||||
|
}
|
||||||
32
cloud/tests/RepositoryContractTest.php
Normal file
32
cloud/tests/RepositoryContractTest.php
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Tests;
|
||||||
|
require_once __DIR__ . '/Support/InMemoryRepositories.php';
|
||||||
|
use App\Tests\Support\{InMemoryNonceRepository, InMemoryEventRepository, InMemoryRateLimiter};
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class RepositoryContractTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testNonceRememberThenSeen(): void
|
||||||
|
{
|
||||||
|
$r = new InMemoryNonceRepository();
|
||||||
|
$this->assertFalse($r->seen('n1'));
|
||||||
|
$r->remember('n1', 999);
|
||||||
|
$this->assertTrue($r->seen('n1'));
|
||||||
|
}
|
||||||
|
public function testEventInsertAndRecent(): void
|
||||||
|
{
|
||||||
|
$r = new InMemoryEventRepository();
|
||||||
|
$id = $r->insert(['severity' => 'high', 'event_type' => 'fall']);
|
||||||
|
$this->assertSame(1, $id);
|
||||||
|
$this->assertCount(1, $r->recent(10, null));
|
||||||
|
$this->assertCount(0, $r->recent(10, 'critical'));
|
||||||
|
}
|
||||||
|
public function testRateLimiterIncrements(): void
|
||||||
|
{
|
||||||
|
$r = new InMemoryRateLimiter();
|
||||||
|
$this->assertSame(1, $r->hit('dev', 100));
|
||||||
|
$this->assertSame(2, $r->hit('dev', 100));
|
||||||
|
$this->assertSame(1, $r->hit('dev', 160));
|
||||||
|
}
|
||||||
|
}
|
||||||
61
cloud/tests/Support/InMemoryRepositories.php
Normal file
61
cloud/tests/Support/InMemoryRepositories.php
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace App\Tests\Support;
|
||||||
|
use App\Repository\{DeviceRepository, NonceRepository, RateLimiterRepository,
|
||||||
|
EventRepository, SnapshotRepository, UserRepository, ZoneRepository,
|
||||||
|
ThresholdRepository, AuditRepository};
|
||||||
|
|
||||||
|
final class InMemoryDeviceRepository implements DeviceRepository {
|
||||||
|
public array $heartbeats = [];
|
||||||
|
public function __construct(private array $secrets = [], private array $versions = []) {}
|
||||||
|
public function secretFor(string $d): ?string { return $this->secrets[$d] ?? null; }
|
||||||
|
public function touchHeartbeat(string $d, array $m): void { $this->heartbeats[$d] = $m; }
|
||||||
|
public function configVersion(string $d): int { return $this->versions[$d] ?? 1; }
|
||||||
|
}
|
||||||
|
final class InMemoryNonceRepository implements NonceRepository {
|
||||||
|
public array $store = [];
|
||||||
|
public function seen(string $n): bool { return isset($this->store[$n]); }
|
||||||
|
public function remember(string $n, int $exp): void { $this->store[$n] = $exp; }
|
||||||
|
}
|
||||||
|
final class InMemoryRateLimiter implements RateLimiterRepository {
|
||||||
|
public array $counts = [];
|
||||||
|
public function hit(string $k, int $w): int { $key = "$k:$w"; return $this->counts[$key] = ($this->counts[$key] ?? 0) + 1; }
|
||||||
|
}
|
||||||
|
final class InMemoryEventRepository implements EventRepository {
|
||||||
|
public array $rows = []; private int $seq = 0;
|
||||||
|
public function insert(array $e): int { $id = ++$this->seq; $this->rows[$id] = $e + ['event_id' => $id, 'acknowledged_by' => null]; return $id; }
|
||||||
|
public function recent(int $limit, ?string $sev): array {
|
||||||
|
$r = array_reverse($this->rows, true);
|
||||||
|
if ($sev !== null) $r = array_filter($r, fn($x) => $x['severity'] === $sev);
|
||||||
|
return array_slice(array_values($r), 0, $limit);
|
||||||
|
}
|
||||||
|
public function find(int $id): ?array { return $this->rows[$id] ?? null; }
|
||||||
|
public function acknowledge(int $id, string $u): bool { if (!isset($this->rows[$id])) return false; $this->rows[$id]['acknowledged_by'] = $u; return true; }
|
||||||
|
}
|
||||||
|
final class InMemorySnapshotRepository implements SnapshotRepository {
|
||||||
|
public array $rows = []; public array $paths = [];
|
||||||
|
public function add(int $e, string $uuid, string $hash): void { $this->rows[$e][] = ['file_uuid' => $uuid, 'content_hash' => $hash]; $this->paths[$uuid] = "/tmp/$uuid.jpg"; }
|
||||||
|
public function forEvent(int $e): array { return $this->rows[$e] ?? []; }
|
||||||
|
public function pathByUuid(string $u): ?string { return $this->paths[$u] ?? null; }
|
||||||
|
}
|
||||||
|
final class InMemoryUserRepository implements UserRepository {
|
||||||
|
public function __construct(public array $users = []) {}
|
||||||
|
public function byUsername(string $u): ?array { return $this->users[$u] ?? null; }
|
||||||
|
public function recordFailure(int $id, int $lock): void { foreach ($this->users as &$row) if ($row['id'] === $id) { $row['failed_attempts'] = ($row['failed_attempts'] ?? 0) + 1; $row['locked_until'] = $lock; } }
|
||||||
|
public function clearFailures(int $id): void { foreach ($this->users as &$row) if ($row['id'] === $id) { $row['failed_attempts'] = 0; $row['locked_until'] = 0; } }
|
||||||
|
}
|
||||||
|
final class InMemoryZoneRepository implements ZoneRepository {
|
||||||
|
public array $zones = []; private int $seq = 0;
|
||||||
|
public function forDevice(string $d): array { return array_values(array_filter($this->zones, fn($z) => ($z['device_id'] ?? '') === $d)); }
|
||||||
|
public function forCamera(string $c): array { return array_values(array_filter($this->zones, fn($z) => $z['camera_id'] === $c)); }
|
||||||
|
public function save(array $z): string { $id = $z['zone_id'] ?? ('z' . (++$this->seq)); $this->zones[$id] = $z + ['zone_id' => $id]; return $id; }
|
||||||
|
public function delete(string $id): bool { if (!isset($this->zones[$id])) return false; unset($this->zones[$id]); return true; }
|
||||||
|
}
|
||||||
|
final class InMemoryThresholdRepository implements ThresholdRepository {
|
||||||
|
public function __construct(private array $data = []) {}
|
||||||
|
public function forDevice(string $d): array { return $this->data[$d] ?? []; }
|
||||||
|
}
|
||||||
|
final class InMemoryAuditRepository implements AuditRepository {
|
||||||
|
public array $log = [];
|
||||||
|
public function append(string $a, string $act, string $t, array $b, array $af): void { $this->log[] = compact('a', 'act', 't', 'b', 'af'); }
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue