Skip to content

Commit 9682727

Browse files
smoghe-bwclaude
andcommitted
Add BRTC model unit tests and strengthen API/BXML test assertions
- Create BrtcModelTest with coverage for EndpointEvent, Device, Page, Link, and ErrorObject models - Assert token and devices fields on create endpoint response - Assert devices field on get endpoint response - Assert Link rel/href fields individually on create, list, get responses - Assert Page model on list endpoints response - Assert creationTimestamp, expirationTimestamp, tag on listed endpoints - Add BXML test for Connect verb eventCallbackUrl attribute Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8e3edc2 commit 9682727

3 files changed

Lines changed: 152 additions & 0 deletions

File tree

tests/ApiTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ public function testCreateListGetDeleteEndpoint() {
266266
$createResp = $brtcClient->createEndpoint($accountId, $createReq)->getResult();
267267
$this->assertInstanceOf(BandwidthLib\BRTC\Models\CreateEndpointResponse::class, $createResp);
268268
$this->assertIsArray($createResp->links);
269+
$this->assertNotEmpty($createResp->links);
270+
$this->assertInstanceOf(BandwidthLib\BRTC\Models\Link::class, $createResp->links[0]);
271+
$this->assertIsString($createResp->links[0]->rel);
272+
$this->assertIsString($createResp->links[0]->href);
269273
$this->assertNotNull($createResp->data);
270274
$this->assertInstanceOf(BandwidthLib\BRTC\Models\CreateEndpointResponseData::class, $createResp->data);
271275
$this->assertNotNull($createResp->data->endpointId);
@@ -275,6 +279,9 @@ public function testCreateListGetDeleteEndpoint() {
275279
$this->assertNotNull($createResp->data->creationTimestamp);
276280
$this->assertNotNull($createResp->data->expirationTimestamp);
277281
$this->assertEquals('php-sdk-test', $createResp->data->tag);
282+
$this->assertNotNull($createResp->data->token);
283+
$this->assertIsString($createResp->data->token);
284+
$this->assertIsArray($createResp->data->devices);
278285
$this->assertIsArray($createResp->errors);
279286

280287
$endpointId = $createResp->data->endpointId;
@@ -283,20 +290,33 @@ public function testCreateListGetDeleteEndpoint() {
283290
$listResp = $brtcClient->listEndpoints($accountId)->getResult();
284291
$this->assertInstanceOf(BandwidthLib\BRTC\Models\ListEndpointsResponse::class, $listResp);
285292
$this->assertIsArray($listResp->links);
293+
$this->assertNotEmpty($listResp->links);
294+
$this->assertInstanceOf(BandwidthLib\BRTC\Models\Link::class, $listResp->links[0]);
295+
$this->assertIsString($listResp->links[0]->rel);
296+
$this->assertIsString($listResp->links[0]->href);
297+
$this->assertNotNull($listResp->page);
298+
$this->assertInstanceOf(BandwidthLib\BRTC\Models\Page::class, $listResp->page);
286299
$this->assertIsArray($listResp->data);
287300
$this->assertNotEmpty($listResp->data);
288301
$this->assertIsArray($listResp->errors);
289302
$this->assertInstanceOf(BandwidthLib\BRTC\Models\Endpoints::class, $listResp->data[0]);
290303
$this->assertNotNull($listResp->data[0]->endpointId);
291304
$this->assertNotNull($listResp->data[0]->type);
292305
$this->assertNotNull($listResp->data[0]->status);
306+
$this->assertNotNull($listResp->data[0]->creationTimestamp);
307+
$this->assertNotNull($listResp->data[0]->expirationTimestamp);
308+
$this->assertNotNull($listResp->data[0]->tag);
293309
$ids = array_map(fn($ep) => $ep->endpointId, $listResp->data);
294310
$this->assertContains($endpointId, $ids, 'Created endpoint should be in list');
295311

296312
// Get endpoint
297313
$getResp = $brtcClient->getEndpoint($accountId, $endpointId)->getResult();
298314
$this->assertInstanceOf(BandwidthLib\BRTC\Models\EndpointResponse::class, $getResp);
299315
$this->assertIsArray($getResp->links);
316+
$this->assertNotEmpty($getResp->links);
317+
$this->assertInstanceOf(BandwidthLib\BRTC\Models\Link::class, $getResp->links[0]);
318+
$this->assertIsString($getResp->links[0]->rel);
319+
$this->assertIsString($getResp->links[0]->href);
300320
$this->assertNotNull($getResp->data);
301321
$this->assertInstanceOf(BandwidthLib\BRTC\Models\Endpoint::class, $getResp->data);
302322
$this->assertEquals($endpointId, $getResp->data->endpointId);
@@ -305,6 +325,7 @@ public function testCreateListGetDeleteEndpoint() {
305325
$this->assertNotNull($getResp->data->creationTimestamp);
306326
$this->assertNotNull($getResp->data->expirationTimestamp);
307327
$this->assertEquals('php-sdk-test', $getResp->data->tag);
328+
$this->assertIsArray($getResp->data->devices);
308329
$this->assertIsArray($getResp->errors);
309330

310331
// Delete endpoint

tests/BrtcModelTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/**
4+
* BrtcModelTest.php
5+
*
6+
* Unit tests for BRTC model classes
7+
*
8+
* @copyright Bandwidth INC
9+
*/
10+
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class BrtcModelTest extends TestCase
14+
{
15+
public function testEndpointEventModel() {
16+
$device = new BandwidthLib\BRTC\Models\Device('dev-123', 'Test Device', 'ACTIVE', '2025-01-01T00:00:00Z');
17+
18+
$event = new BandwidthLib\BRTC\Models\EndpointEvent(
19+
'ep-123',
20+
'WEBRTC',
21+
'ACTIVE',
22+
'2025-01-01T00:00:00Z',
23+
'2025-01-02T00:00:00Z',
24+
'test-tag',
25+
'2025-01-01T01:00:00Z',
26+
'DEVICE_CONNECTED',
27+
$device
28+
);
29+
30+
$this->assertEquals('ep-123', $event->endpointId);
31+
$this->assertEquals('WEBRTC', $event->type);
32+
$this->assertEquals('ACTIVE', $event->status);
33+
$this->assertEquals('2025-01-01T00:00:00Z', $event->creationTimestamp);
34+
$this->assertEquals('2025-01-02T00:00:00Z', $event->expirationTimestamp);
35+
$this->assertEquals('test-tag', $event->tag);
36+
$this->assertEquals('2025-01-01T01:00:00Z', $event->eventTime);
37+
$this->assertEquals('DEVICE_CONNECTED', $event->eventType);
38+
$this->assertInstanceOf(BandwidthLib\BRTC\Models\Device::class, $event->device);
39+
$this->assertEquals('dev-123', $event->device->deviceId);
40+
41+
$json = $event->jsonSerialize();
42+
$this->assertEquals('ep-123', $json['endpointId']);
43+
$this->assertEquals('DEVICE_CONNECTED', $json['eventType']);
44+
$this->assertEquals('2025-01-01T01:00:00Z', $json['eventTime']);
45+
}
46+
47+
public function testEndpointEventModelDefaults() {
48+
$event = new BandwidthLib\BRTC\Models\EndpointEvent();
49+
$this->assertNull($event->endpointId);
50+
$this->assertNull($event->type);
51+
$this->assertNull($event->device);
52+
$this->assertNull($event->eventType);
53+
}
54+
55+
public function testDeviceModel() {
56+
$device = new BandwidthLib\BRTC\Models\Device('dev-456', 'My Phone', 'ACTIVE', '2025-01-01T00:00:00Z');
57+
58+
$this->assertEquals('dev-456', $device->deviceId);
59+
$this->assertEquals('My Phone', $device->deviceName);
60+
$this->assertEquals('ACTIVE', $device->status);
61+
$this->assertEquals('2025-01-01T00:00:00Z', $device->creationTimestamp);
62+
63+
$json = $device->jsonSerialize();
64+
$this->assertEquals('dev-456', $json['deviceId']);
65+
$this->assertEquals('My Phone', $json['deviceName']);
66+
$this->assertEquals('ACTIVE', $json['status']);
67+
$this->assertEquals('2025-01-01T00:00:00Z', $json['creationTimestamp']);
68+
}
69+
70+
public function testDeviceModelDefaults() {
71+
$device = new BandwidthLib\BRTC\Models\Device();
72+
$this->assertNull($device->deviceId);
73+
$this->assertNull($device->deviceName);
74+
$this->assertNull($device->status);
75+
$this->assertNull($device->creationTimestamp);
76+
}
77+
78+
public function testPageModel() {
79+
$page = new BandwidthLib\BRTC\Models\Page(20, 100, 5, 1);
80+
81+
$this->assertEquals(20, $page->pageSize);
82+
$this->assertEquals(100, $page->totalElements);
83+
$this->assertEquals(5, $page->totalPages);
84+
$this->assertEquals(1, $page->pageNumber);
85+
86+
$json = $page->jsonSerialize();
87+
$this->assertEquals(20, $json['pageSize']);
88+
$this->assertEquals(100, $json['totalElements']);
89+
$this->assertEquals(5, $json['totalPages']);
90+
$this->assertEquals(1, $json['pageNumber']);
91+
}
92+
93+
public function testPageModelDefaults() {
94+
$page = new BandwidthLib\BRTC\Models\Page();
95+
$this->assertNull($page->pageSize);
96+
$this->assertNull($page->totalElements);
97+
}
98+
99+
public function testLinkModel() {
100+
$link = new BandwidthLib\BRTC\Models\Link('self', 'https://api.bandwidth.com/endpoints');
101+
102+
$this->assertEquals('self', $link->rel);
103+
$this->assertEquals('https://api.bandwidth.com/endpoints', $link->href);
104+
105+
$json = $link->jsonSerialize();
106+
$this->assertEquals('self', $json['rel']);
107+
$this->assertEquals('https://api.bandwidth.com/endpoints', $json['href']);
108+
}
109+
110+
public function testErrorObjectModel() {
111+
$error = new BandwidthLib\BRTC\Models\ErrorObject('VALIDATION_ERROR', 'Field is required');
112+
113+
$this->assertEquals('VALIDATION_ERROR', $error->type);
114+
$this->assertEquals('Field is required', $error->description);
115+
116+
$json = $error->jsonSerialize();
117+
$this->assertEquals('VALIDATION_ERROR', $json['type']);
118+
$this->assertEquals('Field is required', $json['description']);
119+
}
120+
}

tests/BxmlTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,15 @@ public function testConnectAndEndpoint() {
554554
$responseXml = $response->toBxml();
555555
$this->assertEquals($expectedXml, $responseXml);
556556
}
557+
558+
public function testConnectWithEventCallbackUrl() {
559+
$endpoint = new BandwidthLib\Voice\Bxml\Endpoint("endpoint-789");
560+
$connect = new BandwidthLib\Voice\Bxml\Connect([$endpoint]);
561+
$connect->eventCallbackUrl("https://example.com/events");
562+
$response = new BandwidthLib\Voice\Bxml\Response();
563+
$response->addVerb($connect);
564+
$expectedXml = '<?xml version="1.0" encoding="UTF-8"?><Response><Connect eventCallbackUrl="https://example.com/events"><Endpoint>endpoint-789</Endpoint></Connect></Response>';
565+
$responseXml = $response->toBxml();
566+
$this->assertEquals($expectedXml, $responseXml);
567+
}
557568
}

0 commit comments

Comments
 (0)