*/ public array $calls = []; /** @var array */ private array $responses; public function __construct(array $responses) { parent::__construct('https://unifi.example.com:8443', 'api', 'geheim', 'default'); $this->responses = $responses; } protected function apiRequest($endpoint, $data = null, $method = 'POST') { $this->calls[] = ['endpoint' => $endpoint, 'data' => $data, 'method' => $method]; return array_shift($this->responses); } } /** * Die Auswahl des richtigen Vouchers ist der heikle Teil: der Controller * liefert alle Voucher unsortiert zurück. Wer blind den ersten nimmt, gibt * bei parallelen Anfragen den Code eines anderen Gastes aus. */ class UniFiControllerTest extends TestCase { private function created(int $time = 1000): array { return ['data' => [['create_time' => $time]]]; } public function testPicksOwnVoucherNotAForeignOne(): void { $controller = new RecordedController([ $this->created(), ['data' => [ ['code' => '1111122222', 'note' => 'Fremder Gast', 'create_time' => 5000, '_id' => 'a'], ['code' => '3333344444', 'note' => '2026-09-24_Empfang', 'create_time' => 4000, '_id' => 'b'], ]], ]); $result = $controller->createVoucher('2026-09-24_Empfang', 2, 480); $this->assertSame('3333344444', $result['code'], 'Der Voucher mit passender Notiz gehört zu dieser Anfrage'); $this->assertSame('b', $result['unifi_id']); } public function testPicksNewestWhenNoteAppearsTwice(): void { $controller = new RecordedController([ $this->created(), ['data' => [ ['code' => '1111122222', 'note' => 'Empfang', 'create_time' => 4000, '_id' => 'alt'], ['code' => '3333344444', 'note' => 'Empfang', 'create_time' => 9000, '_id' => 'neu'], ]], ]); $this->assertSame('neu', $controller->createVoucher('Empfang', 1, 60)['unifi_id']); } public function testFallsBackToNewestWhenNoteDoesNotMatch(): void { $controller = new RecordedController([ $this->created(), ['data' => [ ['code' => '1111122222', 'note' => 'Etwas anderes', 'create_time' => 4000, '_id' => 'alt'], ['code' => '3333344444', 'note' => 'Auch anders', 'create_time' => 9000, '_id' => 'neu'], ]], ]); $this->assertSame('neu', $controller->createVoucher('Sonderzeichen …', 1, 60)['unifi_id']); } public function testFormatsCodeInFiveDigitBlocks(): void { $controller = new RecordedController([ $this->created(), ['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]], ]); $result = $controller->createVoucher('Gast', 1, 60); $this->assertSame('12345-67890', $result['formatted_code']); $this->assertSame('1234567890', $result['code'], 'Der rohe Code bleibt unverändert'); } public function testFailsWhenControllerDoesNotConfirmCreation(): void { $controller = new RecordedController([['data' => []]]); $this->expectException(Exception::class); $controller->createVoucher('Gast', 1, 60); } public function testFailsWhenNoVouchersComeBack(): void { $controller = new RecordedController([$this->created(), ['data' => []]]); $this->expectException(Exception::class); $controller->createVoucher('Gast', 1, 60); } public function testSendsQosLimitsOnlyWhenSet(): void { $controller = new RecordedController([ $this->created(), ['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]], ]); $controller->createVoucher('Gast', 3, 240, ['down' => 20000, 'up' => 5000, 'quota_mb' => 1024]); $payload = $controller->calls[0]['data']; $this->assertSame('create-voucher', $payload['cmd']); $this->assertSame(240, $payload['expire']); $this->assertSame(3, $payload['quota']); $this->assertTrue($payload['qos_overwrite']); $this->assertSame(20000, $payload['down']); $this->assertSame(5000, $payload['up']); $this->assertSame(1024, $payload['bytes']); } public function testOmitsQosWhenNotRequested(): void { $controller = new RecordedController([ $this->created(), ['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]], ]); $controller->createVoucher('Gast', 1, 60); $this->assertArrayNotHasKey('qos_overwrite', $controller->calls[0]['data']); $this->assertArrayNotHasKey('down', $controller->calls[0]['data']); } }