Skip to content

Commit

Permalink
Meetings API logic changes (#419)
Browse files Browse the repository at this point in the history
* logic for creating long term room

* backwards compatibility shim
  • Loading branch information
SecondeJK authored Jul 13, 2023
1 parent 2664ea6 commit b88a1fe
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 35 deletions.
29 changes: 25 additions & 4 deletions src/Meetings/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use GuzzleHttp\Psr7\MultipartStream;
use Laminas\Diactoros\Request;
use Psr\Http\Client\ClientExceptionInterface;
use Vonage\Client\APIClient;
use Vonage\Client\APIResource;
use Vonage\Client\Exception\Exception;
use Vonage\Client\Exception\NotFound;
use Vonage\Entity\Filter\KeyValueFilter;
use Vonage\Entity\Hydrator\ArrayHydrator;
Expand Down Expand Up @@ -36,13 +38,32 @@ public function getRoom(string $id): Room
return $room;
}

public function createRoom(string $displayName): Room
/**
*
* Creates a room. Originally this was a string with the display name
* So there is backwards compatibility cases to cover
*
* @param $room string|Room
*
* @return Room
* @throws ClientExceptionInterface
* @throws Exception
*/
public function createRoom(Room|string $room): Room
{
if (is_string($room)) {
trigger_error(
'Passing a display name string to createRoom is deprecated, please use a Room object',
E_USER_DEPRECATED
);
$roomEntity = new Room();
$roomEntity->fromArray(['display_name' => $room]);
$room = $roomEntity;
}

$this->api->setBaseUri('/rooms');

$response = $this->api->create([
'display_name' => $displayName
]);
$response = $this->api->create($room->toArray());

$room = new Room();
$room->fromArray($response);
Expand Down
18 changes: 13 additions & 5 deletions src/Meetings/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@

class Room implements ArrayHydrateInterface
{
private array $data;
protected array $data;

public function fromArray(array $data): void
public function fromArray(array $data): static
{
if (!isset($data['display_name'])) {
throw new \InvalidArgumentException('A room object must contain a display_name');
}

$this->data = $data;

return $this;
}

public function toArray(): array
{
return $this->data;
return array_filter($this->data, static function ($value) {
return $value !== '';
});
}

public function __get($name)
public function __get($value)
{
return $this->data[$name];
return $this->data[$value];
}
}
46 changes: 42 additions & 4 deletions test/Meetings/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ public function testWillCreateRoom(): void
return true;
}))->willReturn($this->getResponse('create-room-success', 201));

$response = $this->meetingsClient->createRoom('test-room');
$room = new Room();
$room->fromArray(['display_name' => 'test-room']);

$response = $this->meetingsClient->createRoom($room);
$this->assertInstanceOf(Room::class, $response);

$this->assertEquals('test-room', $response->display_name);
$this->assertEquals('instant', $response->type);
}

public function testClientWillHandleUnauthorizedRequests(): void
public function testWillCreateLongTermRoom(): void
{
$this->vonageClient->send(Argument::that(function (RequestInterface $request) {
$this->assertEquals('POST', $request->getMethod());
Expand All @@ -134,11 +138,42 @@ public function testClientWillHandleUnauthorizedRequests(): void

$this->assertRequestJsonBodyContains('display_name', 'test-room', $request);
return true;
}))->willReturn($this->getResponse('create-long-term-room-success', 201));

$room = new Room();
$room->fromArray([
'display_name' => 'test-room',
'type' => 'long_term',
'expires_at' => '2023-01-30T00:47:04+0000'
]);

$response = $this->meetingsClient->createRoom($room);
$this->assertInstanceOf(Room::class, $response);

$this->assertEquals('test-room', $response->display_name);
$this->assertEquals('long_term', $response->type);
}

public function testClientWillHandleUnauthorizedRequests(): void
{
$this->vonageClient->send(Argument::that(function (RequestInterface $request) {
$this->assertEquals('POST', $request->getMethod());

$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals('https://api-eu.vonage.com/meetings/rooms', $uriString);

$this->assertRequestJsonBodyContains('display_name', 'something', $request);
return true;
}))->willReturn($this->getResponse('empty', 403));

$this->expectException(Client\Exception\Credentials::class);
$this->expectExceptionMessage('You are not authorised to perform this request');
$response = $this->meetingsClient->createRoom('test-room');

$room = new Room();
$room->fromArray(['display_name' => 'something']);

$response = $this->meetingsClient->createRoom($room);
}

public function testClientWillHandleNotFoundResponse(): void
Expand Down Expand Up @@ -173,7 +208,10 @@ public function testClientWillHandleValidationError(): void
$this->expectException(Validation::class);
$this->expectExceptionMessage('The request data was invalid');

$response = $this->meetingsClient->createRoom('test-room');
$room = new Room();
$room->fromArray(['display_name' => 'test-room']);

$response = $this->meetingsClient->createRoom($room);
}

public function testWillGetRoomDetails(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"display_name": "test-room",
"metadata": "Welcome to my custom room",
"type": "long_term",
"recording_options": {
"auto_record": false,
"record_only_owner": false
},
"theme_id": "ef2b46f3-8ebb-437e-a671-272e4990fbc8",
"join_approval_level": "none",
"initial_join_options": {
"microphone_state": "on"
},
"callback_urls": {
"rooms_callback_url": "https://example.com/rooms",
"sessions_callback_url": "https://example.com/sessions",
"recordings_callback_url": "https://example.com/recordings"
},
"available_features": {
"is_recording_available": true,
"is_chat_available": true,
"is_whiteboard_available": true,
"is_locale_switcher_available": true,
"is_captions_available": false
},
"ui_settings": {
"language": "es"
}
}
35 changes: 13 additions & 22 deletions test/Meetings/Fixtures/Responses/create-room-success.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
{
"id": "abc123",
"display_name": "test-room",
"metadata": "abc123",
"type": "abc123",
"metadata": "Welcome to my custom room",
"type": "instant",
"recording_options": {
"auto_record": false,
"record_only_owner": false
},
"meeting_code": "123456789",
"is_available": false,
"theme_id": "abc123",
"created_at": "abc123",
"expires_at": "abc123",
"expire_after_use": false,
"join_approval_level": "abc123",
"theme_id": "ef2b46f3-8ebb-437e-a671-272e4990fbc8",
"join_approval_level": "none",
"initial_join_options": {
"microphone_state": "default"
"microphone_state": "on"
},
"callback_urls": {
"rooms_callback_url": "https://example.com",
"sessions_callback_url": "https://example.com",
"recordings_callback_url": "https://example.com"
"rooms_callback_url": "https://example.com/rooms",
"sessions_callback_url": "https://example.com/sessions",
"recordings_callback_url": "https://example.com/recordings"
},
"available_features": {
"is_recording_available": true,
"is_chat_available": true,
"is_whiteboard_available": true
"is_whiteboard_available": true,
"is_locale_switcher_available": true,
"is_captions_available": false
},
"_links": {
"guest_url": {
"href": "https://meetings.vonage.com/123456789"
},
"host_url": {
"href": "https://meetings.vonage.com/123456789?participant_token=xyz"
}
"ui_settings": {
"language": "es"
}
}

0 comments on commit b88a1fe

Please sign in to comment.