Skip to content

Commit

Permalink
Merge pull request #337 from opentok/dev
Browse files Browse the repository at this point in the history
Main to Dev
  • Loading branch information
SecondeJK committed Aug 14, 2023
2 parents f1e40f8 + 989f990 commit 859d6d0
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 31 deletions.
24 changes: 16 additions & 8 deletions src/OpenTok/Util/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,18 @@ public static function validateAutoArchiveResolution($archiveResolution)

public static function validateLayoutClassListItem($layoutClassList)
{
if (!is_array($layoutClassList)) {
throw new InvalidArgumentException('Each element in the streamClassArray must have a layoutClassList array.');
}

if (!is_string($layoutClassList['id'])) {
throw new InvalidArgumentException('Each element in the streamClassArray must have an id string.');
}

if (!is_array($layoutClassList)) {
throw new InvalidArgumentException('Each element in the streamClassArray must have a layoutClassList array.');
if (!isset($layoutClassList['layoutClassList'])) {
throw new InvalidArgumentException('layoutClassList not set in array');
}

if (!is_array($layoutClassList['layoutClassList'])) {
throw new InvalidArgumentException('Each element in the layoutClassList array must be a string (defining class names).');
}
Expand All @@ -454,16 +459,19 @@ public static function validateDTMFDigits(string $digits): void
throw new InvalidArgumentException('DTMF digits can only support 0-9, p, #, and * characters');
}

// Helpers

// credit: http://stackoverflow.com/a/173479
public static function isAssoc($arr): bool
{
if (array() === $arr) {
return false;
if (!function_exists('array_is_list')) {
function array_is_list(array $arr): bool
{
if ($arr === []) {
return true;
}
return array_keys($arr) === range(0, count($arr) - 1);
}
}

return array_keys($arr) !== range(0, count($arr) - 1);
return !array_is_list($arr);
}

protected static function decodeSessionId($sessionId)
Expand Down
9 changes: 9 additions & 0 deletions tests/OpenTokTest/LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OpenTokTest;

use OpenTok\Layout;
use OpenTok\Util\Validators;
use PHPStan\Testing\TestCase;

class LayoutTest extends TestCase
Expand All @@ -21,6 +22,14 @@ public function testStylesheetIsNotInSerializedArrayIfNotCustom()
}
}

public function testWillValidateLayout(): void
{
$this->expectException(\InvalidArgumentException::class);
$object = ['bestFit' => true];

Validators::validateLayout($object);
}

public function testStylesheetIsInSerializedArrayIfCustom()
{
$layout = Layout::createCustom(['stylesheet' => 'foo']);
Expand Down
162 changes: 139 additions & 23 deletions tests/OpenTokTest/Validators/ValidatorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OpenTokTest\Validators;

use OpenTok\Exception\InvalidArgumentException;
use OpenTok\Util\Client;
use OpenTok\Util\Validators;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -66,34 +67,34 @@ public function testWillPassCorrectForceMutePayload(): void
'streamId1',
'streamId2'
],
'active' => true
'active' => true
];

Validators::validateForceMuteAllOptions($options);
}

public function testIsAssocWithValidArray(): void
public function testIsAssocWithIndexedArray(): void
{
$haystack = [
'one' => '1',
'two' => '2',
'three' => '3',
'four' => '4'
];
$array = [1, 2, 3, 4];
$this->assertFalse(Validators::isAssoc($array));
}

$this->assertTrue(Validators::isAssoc($haystack));
public function testIsAssocWithAssociativeArray(): void
{
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$this->assertTrue(Validators::isAssoc($array));
}

public function testIsAssocWithInvalidArray(): void
public function testIsAssocWithMixedKeysArray(): void
{
$haystack = [
'one',
'two',
'three',
'four'
];
$array = [1, 'a' => 2, 3];
$this->assertTrue(Validators::isAssoc($array));
}

$this->assertFalse(Validators::isAssoc($haystack));
public function testIsAssocWithEmptyArray(): void
{
$array = [];
$this->assertFalse(Validators::isAssoc($array));
}

public function testWillFailWhenStreamIdsAreNotCorrect(): void
Expand All @@ -105,7 +106,7 @@ public function testWillFailWhenStreamIdsAreNotCorrect(): void
3536,
'streamId2'
],
'active' => true
'active' => true
];

Validators::validateForceMuteAllOptions($options);
Expand All @@ -120,7 +121,7 @@ public function testWillFailWhenActiveIsNotBool(): void
'streamId1',
'streamId2'
],
'active' => 'true'
'active' => 'true'
];

Validators::validateForceMuteAllOptions($options);
Expand All @@ -132,7 +133,7 @@ public function testWillFailWhenStreamIdsIsNotArray(): void

$options = [
'excludedStreams' => 'streamIdOne',
'active' => false
'active' => false
];

Validators::validateForceMuteAllOptions($options);
Expand All @@ -142,7 +143,7 @@ public function testWillValidateWebsocketConfiguration(): void
{
$this->expectNotToPerformAssertions();
$websocketConfig = [
'uri' => 'ws://valid-websocket',
'uri' => 'ws://valid-websocket',
'streams' => [
'525503c7-913e-43a1-84b4-31b2e9fe668b',
'14026813-4f50-4a5a-9b72-fea25430916d'
Expand All @@ -162,14 +163,14 @@ public function testWillThrowExceptionOnInvalidWebsocketConfiguration(): void
]
];
Validators::validateWebsocketOptions($websocketConfig);
}
}

/**
* @dataProvider resolutionProvider
*/
public function testValidResolutions($resolution, $isValid): void
{
if (!$isValid) {
if ( ! $isValid) {
$this->expectException(InvalidArgumentException::class);
} else {
$this->expectNotToPerformAssertions();
Expand All @@ -178,6 +179,121 @@ public function testValidResolutions($resolution, $isValid): void
Validators::validateResolution($resolution);
}

public function testValidLayoutClassListItemErrorOnString(): void
{
$input = 'example_id';
$this->expectException(\InvalidArgumentException::class);
Validators::validateLayoutClassListItem($input);
}

public function testValidLayoutClassListItem(): void
{
$layoutClassList = [
'id' => 'example_id',
'layoutClassList' => ['class1', 'class2']
];

$this->assertNull(Validators::validateLayoutClassListItem($layoutClassList));
}

public function testInvalidIdType(): void
{
$this->expectException(InvalidArgumentException::class);
$layoutClassList = [
'id' => 123,
'layoutClassList' => ['class1', 'class2']
];

Validators::validateLayoutClassListItem($layoutClassList);
}

public function testMissingLayoutClassList(): void
{
$this->expectException(InvalidArgumentException::class);
$layoutClassList = [
'id' => 'example_id'
];

Validators::validateLayoutClassListItem($layoutClassList);
}

public function testInvalidLayoutClassListType(): void
{
$this->expectException(InvalidArgumentException::class);
$layoutClassList = [
'id' => 'example_id',
'layoutClassList' => 'invalid_class'
];

Validators::validateLayoutClassListItem($layoutClassList);
}
public function testValidateClient(): void
{
$client = new Client();
Validators::validateClient($client);

// No exception, which was the test so fake a pass
$this->assertTrue(true);
}

public function testExceptionOnInvalidClient(): void
{
$this->expectException(\InvalidArgumentException::class);
$client = new \stdClass();
Validators::validateClient($client);
}

public function testThrowsErrorOnInvalidStreamMode(): void
{
$this->expectException(\InvalidArgumentException::class);
$streamMode = ['auto'];
Validators::validateHasStreamMode($streamMode);
}

public function testValidateSignalPayload(): void
{
$validPayload = ['type' => 'signal_type', 'data' => 'signal_data'];
$this->assertNull(Validators::validateSignalPayload($validPayload));

$invalidDataPayload = ['type' => 'signal_type', 'data' => 123];
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Signal Payload cannot be null:");
Validators::validateSignalPayload($invalidDataPayload);

$invalidTypePayload = ['type' => null, 'data' => 'signal_data'];
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Signal Payload cannot be null:");
Validators::validateSignalPayload($invalidTypePayload);

// Invalid payload: both type and data are null
$invalidBothPayload = ['type' => null, 'data' => null];
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Signal Payload cannot be null:");
Validators::validateSignalPayload($invalidBothPayload);
}

/**
* @dataProvider connectionIdProvider
*/
public function testConnectionId($input, $expectException): void
{
if ($expectException) {
$this->expectException(\InvalidArgumentException::class);
}

Validators::validateConnectionId($input);
$this->assertTrue(true);
}

public function connectionIdProvider(): array
{
return [
[['this' => 'is not a string'], true],
['', true],
['valid_connection_string', false]
];
}

public function resolutionProvider(): array
{
return [
Expand Down

0 comments on commit 859d6d0

Please sign in to comment.