127 lines
5.9 KiB
PHP
127 lines
5.9 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 DummyVar2 $workplaceId;
|
|
public function __construct()
|
|
{
|
|
$this->enabled = new DummyVar2('true');
|
|
$this->api_key = new DummyVar2('test_api_key');
|
|
$this->server_url = new DummyVar2('https://example.test');
|
|
$this->workplaceId = new DummyVar2('test_workspace_id');
|
|
}
|
|
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 /workspaces/{ws}/numbers)
|
|
$client = new FakeBird2();
|
|
$res = $client->listNumbers('test_workspace_id', ['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/workspaces/test_workspace_id/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: AccessKey test_api_key', $client->last['headers'], true), 'Authorization header is set (numbers)');
|
|
assert_true2(empty($client->last['body']), 'GET body is empty (numbers)');
|
|
|
|
// Test: get number (GET /workspaces/{ws}/numbers/{id})
|
|
$clientNum = new FakeBird2();
|
|
$resNum = $clientNum->getNumber('test_workspace_id', 'num_123');
|
|
assert_true2(str_ends_with($clientNum->last['url'], '/workspaces/test_workspace_id/numbers/num_123'), 'Get number URL composed correctly');
|
|
assert_true2($clientNum->last['method'] === 'GET', 'HTTP method is GET (get number)');
|
|
|
|
// Test: delete number (DELETE /workspaces/{ws}/numbers/{id})
|
|
$clientDel = new FakeBird2();
|
|
$resDel = $clientDel->deleteNumber('test_workspace_id', 'num_123');
|
|
assert_true2(str_ends_with($clientDel->last['url'], '/workspaces/test_workspace_id/numbers/num_123'), 'Delete number URL composed correctly');
|
|
assert_true2($clientDel->last['method'] === 'DELETE', 'HTTP method is DELETE (delete number)');
|
|
|
|
// 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: AccessKey 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";
|
|
|