Files
api/services/nginx/app/tests/bird/NumbersAndFlashCallsApiTest.php
T
Jeppe Bundgaard 5a59562e35 Add Bird API integration with voice call and flash call support
- Implement Bird API client (`bird.php`) for handling HTTP requests to Bird services.
- Add routes for voice and flash call management (`birdVoiceFlashCallsRoute.php`, `birdNumbersRoute.php`).
- Introduce test cases for voice calls, flash calls, and numbers (`VoiceCallsApiTest.php`, `NumbersAndFlashCallsApiTest.php`).
- Include configuration management classes and APIs for enabling the Bird module and managing API keys (`bird_c.php`).
- Provide OpenAPI specifications for flash call endpoints (`bird-flash-calls.md`).
2026-02-19 12:19:36 +01:00

112 lines
5.0 KiB
PHP

<?php
if (!defined('WD')) { define('WD', dirname(__DIR__, 2)); }
require_once WD . '/classes/bird.php';
use classes\bird as BirdClient;
class DummyVar2
{
public function __construct(private mixed $value) {}
public function isTrue(): bool { return $this->value === true || $this->value === 'true' || $this->value === 1 || $this->value === '1'; }
public function getVariableValue(): mixed { return $this->value; }
}
class DummyConfig2
{
public DummyVar2 $enabled;
public DummyVar2 $api_key;
public DummyVar2 $server_url;
public function __construct()
{
$this->enabled = new DummyVar2('true');
$this->api_key = new DummyVar2('test_api_key');
$this->server_url = new DummyVar2('https://example.test');
}
public function getModuleName(): string { return 'bird'; }
}
class FakeBird2 extends BirdClient
{
public array $last = [];
public function __construct()
{
// Bypass DB-backed config classes with our dummy config
$this->config = new DummyConfig2();
}
protected function doHttpRequest(string $method, string $url, array $headers, string $body = ''): array
{
$this->last = compact('method','url','headers','body');
// Return a canned success response
$resp = ['ok' => true, 'id' => 'flash_123'];
return ['status_code' => 200, 'body' => json_encode($resp)];
}
}
function assert_true2($cond, $msg)
{
if ($cond) {
echo "✔ $msg\n";
} else {
echo "✘ $msg\n"; exit(1);
}
}
// Test: list numbers (GET /numbers)
$client = new FakeBird2();
$res = $client->sendGetRequest('/numbers', ['limit' => 10]);
assert_true2(is_object($res) || is_array($res), 'Numbers response is JSON-decodable');
assert_true2(str_starts_with($client->last['url'], 'https://example.test/numbers'), 'Numbers GET URL composed correctly');
assert_true2(str_contains($client->last['url'], 'limit=10'), 'Query string encoded correctly');
assert_true2($client->last['method'] === 'GET', 'HTTP method is GET (numbers)');
assert_true2(in_array('Authorization: Bearer test_api_key', $client->last['headers'], true), 'Authorization header is set (numbers)');
assert_true2(empty($client->last['body']), 'GET body is empty (numbers)');
// Test: create flash call (POST /voice/flash-calls)
$client2 = new FakeBird2();
$payload = [
'to' => '+4511122233',
'from' => '+4599988877',
'code' => '1234',
];
$res2 = $client2->sendPostRequest('/voice/flash-calls', $payload);
assert_true2(is_object($res2) || is_array($res2), 'Flash call response is JSON-decodable');
assert_true2(str_starts_with($client2->last['url'], 'https://example.test/voice/flash-calls'), 'Flash POST URL composed correctly');
assert_true2($client2->last['method'] === 'POST', 'HTTP method is POST (flash)');
assert_true2(in_array('Authorization: Bearer test_api_key', $client2->last['headers'], true), 'Authorization header is set (flash)');
assert_true2(json_decode($client2->last['body'], true)['code'] === '1234', 'Flash POST body encoded correctly');
// Test: list flash calls (GET /voice/flash-calls)
$client3 = new FakeBird2();
$client3->sendGetRequest('/voice/flash-calls', ['page' => 2]);
assert_true2(str_starts_with($client3->last['url'], 'https://example.test/voice/flash-calls'), 'Flash list GET URL composed correctly');
assert_true2(str_contains($client3->last['url'], 'page=2'), 'Flash list query encoded correctly');
assert_true2($client3->last['method'] === 'GET', 'HTTP method is GET (flash list)');
// Test: get flash call by id (GET /voice/flash-calls/{id})
$client4 = new FakeBird2();
$client4->sendGetRequest('/voice/flash-calls/flash_123');
assert_true2(str_ends_with($client4->last['url'], '/voice/flash-calls/flash_123'), 'Flash get-by-id URL composed correctly');
assert_true2($client4->last['method'] === 'GET', 'HTTP method is GET (flash by id)');
// Test: end flash call by id (POST /voice/flash-calls/{id})
$client5 = new FakeBird2();
$payloadEnd = [ 'result' => 'success' ];
$client5->sendPostRequest('/voice/flash-calls/flash_123', $payloadEnd);
assert_true2(str_ends_with($client5->last['url'], '/voice/flash-calls/flash_123'), 'Flash end-by-id URL composed correctly');
assert_true2($client5->last['method'] === 'POST', 'HTTP method is POST (flash end by id)');
assert_true2(json_decode($client5->last['body'], true)['result'] === 'success', 'Flash end-by-id body encoded correctly');
// Test: end flash call by numbers (POST /voice/flash-calls/end)
$client6 = new FakeBird2();
$payloadEndByNums = [ 'from' => '+4599988877', 'to' => '+4511122233' ];
$client6->sendPostRequest('/voice/flash-calls/end', $payloadEndByNums);
assert_true2(str_ends_with($client6->last['url'], '/voice/flash-calls/end'), 'Flash end-by-numbers URL composed correctly');
assert_true2($client6->last['method'] === 'POST', 'HTTP method is POST (flash end by numbers)');
assert_true2(json_decode($client6->last['body'], true)['from'] === '+4599988877', 'Flash end-by-numbers body encoded correctly');
echo "NumbersAndFlashCallsApiTest completed.\n";