Files
api/services/nginx/app/tests/bird/VoiceCallsApiTest.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

80 lines
2.7 KiB
PHP

<?php
if (!defined('WD')) { define('WD', dirname(__DIR__, 2)); }
require_once WD . '/classes/bird.php';
use classes\bird as BirdClient;
class DummyVar
{
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 DummyConfig
{
public DummyVar $enabled;
public DummyVar $api_key;
public DummyVar $server_url;
public function __construct()
{
$this->enabled = new DummyVar('true');
$this->api_key = new DummyVar('test_api_key');
$this->server_url = new DummyVar('https://example.test');
}
public function getModuleName(): string { return 'bird'; }
}
class FakeBird extends BirdClient
{
public array $last = [];
public function __construct()
{
// Bypass DB-backed config classes with our dummy config
$this->config = new DummyConfig();
}
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 = ['id' => 'call_123', 'status' => 'queued'];
return ['status_code' => 200, 'body' => json_encode($resp)];
}
}
function assert_true($cond, $msg)
{
if ($cond) {
echo "✔ $msg\n";
} else {
echo "✘ $msg\n"; exit(1);
}
}
// Test: create call (POST /voice/calls)
$client = new FakeBird();
$payload = [
'to' => '+4511122233',
'from' => '+4599988877',
'tts' => [ 'message' => 'Hello from test' ],
];
$res = $client->sendPostRequest('/voice/calls', $payload);
assert_true(is_object($res) || is_array($res), 'Response is JSON-decodable');
assert_true(str_starts_with($client->last['url'], 'https://example.test/voice/calls'), 'POST URL composed correctly');
assert_true($client->last['method'] === 'POST', 'HTTP method is POST');
assert_true(in_array('Authorization: Bearer test_api_key', $client->last['headers'], true), 'Authorization header is set');
assert_true(json_decode($client->last['body'], true)['to'] === '+4511122233', 'POST body encoded correctly');
// Test: get call (GET /voice/calls/{id})
$client2 = new FakeBird();
$res2 = $client2->sendGetRequest('/voice/calls/call_123');
assert_true(str_starts_with($client2->last['url'], 'https://example.test/voice/calls/call_123'), 'GET URL composed correctly');
assert_true($client2->last['method'] === 'GET', 'HTTP method is GET');
assert_true(empty($client2->last['body']), 'GET body is empty');
echo "VoiceCallsApiTest completed.\n";