View source
<?php
namespace Auth0\Tests\API\Management;
use Auth0\SDK\API\Management;
use Auth0\Tests\API\ApiTests;
use GuzzleHttp\Exception\ClientException;
use Auth0\SDK\Exception\CoreException;
class ResourceServersTest extends ApiTests {
protected static $api;
protected static $serverIdentifier;
protected static $scopes = [
[
'value' => 'read:test1',
'description' => 'Testing scope',
],
[
'value' => 'read:test2',
'description' => 'Testing scope',
],
];
public static function setUpBeforeClass() {
$env = self::getEnv();
$api = new Management($env['API_TOKEN'], $env['DOMAIN'], [
'timeout' => 30,
]);
self::$api = $api
->resourceServers();
self::$serverIdentifier = 'TEST_PHP_SDK_ID_' . uniqid();
}
public function testThatMethodAndPropertyReturnSameClass() {
$api = new Management(uniqid(), uniqid());
$this
->assertInstanceOf(Management\ResourceServers::class, $api->resource_servers);
$this
->assertInstanceOf(Management\ResourceServers::class, $api
->resourceServers());
$api->resource_servers = null;
$this
->assertInstanceOf(Management\ResourceServers::class, $api
->resourceServers());
}
public function testCreate() {
$create_data = [
'name' => 'TEST_PHP_SDK_CREATE_' . uniqid(),
'token_lifetime' => rand(10000, 20000),
'signing_alg' => 'HS256',
'scopes' => [
self::$scopes[0],
],
];
$response = self::$api
->create(self::$serverIdentifier, $create_data);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertNotEmpty($response);
$this
->assertNotEmpty($response['id']);
$this
->assertEquals(self::$serverIdentifier, $response['identifier']);
$this
->assertEquals($create_data['name'], $response['name']);
$this
->assertEquals($create_data['token_lifetime'], $response['token_lifetime']);
$this
->assertEquals($create_data['signing_alg'], $response['signing_alg']);
$this
->assertEquals($create_data['scopes'], $response['scopes']);
}
public function testGet() {
$response = self::$api
->get(self::$serverIdentifier);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertNotEmpty($response);
$this
->assertEquals(self::$serverIdentifier, $response['identifier']);
}
public function testGetAll() {
$response = self::$api
->getAll();
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertGreaterThanOrEqual(2, count($response));
$found_added = false;
foreach ($response as $server) {
if ($server['identifier'] === self::$serverIdentifier) {
$found_added = true;
break;
}
}
$this
->assertTrue($found_added);
$response_paged = self::$api
->getAll(1, 1);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertNotEmpty($response_paged);
$this
->assertEquals($response[1]['id'], $response_paged[0]['id']);
}
public function testUpdate() {
$update_data = [
'name' => 'TEST_PHP_SDK_UPDATE_' . uniqid(),
'token_lifetime' => rand(20001, 30000),
'signing_alg' => 'RS256',
'scopes' => self::$scopes,
];
$response = self::$api
->update(self::$serverIdentifier, $update_data);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$this
->assertEquals($update_data['name'], $response['name']);
$this
->assertEquals($update_data['token_lifetime'], $response['token_lifetime']);
$this
->assertEquals($update_data['signing_alg'], $response['signing_alg']);
$this
->assertEquals($update_data['scopes'], $response['scopes']);
}
public function testDelete() {
$response = self::$api
->delete(self::$serverIdentifier);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
$get_server_throws_error = false;
try {
self::$api
->get(self::$serverIdentifier);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
} catch (ClientException $e) {
$get_server_throws_error = 404 === $e
->getCode();
}
$this
->assertNull($response);
$this
->assertTrue($get_server_throws_error);
}
public function testExceptions() {
$caught_get_no_id_exception = false;
try {
self::$api
->get(null);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
} catch (CoreException $e) {
$caught_get_no_id_exception = $this
->errorHasString($e, 'Invalid "id" parameter');
}
$this
->assertTrue($caught_get_no_id_exception);
$caught_delete_no_id_exception = false;
try {
self::$api
->delete(null);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
} catch (CoreException $e) {
$caught_delete_no_id_exception = $this
->errorHasString($e, 'Invalid "id" parameter');
}
$this
->assertTrue($caught_delete_no_id_exception);
$caught_update_no_id_exception = false;
try {
self::$api
->update(null, []);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
} catch (CoreException $e) {
$caught_update_no_id_exception = $this
->errorHasString($e, 'Invalid "id" parameter');
}
$this
->assertTrue($caught_update_no_id_exception);
$caught_create_empty_identifier_param_exception = false;
try {
self::$api
->create(null, []);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
} catch (CoreException $e) {
$caught_create_empty_identifier_param_exception = $this
->errorHasString($e, 'Invalid "identifier" field');
}
$this
->assertTrue($caught_create_empty_identifier_param_exception);
$caught_create_invalid_identifier_field_exception = false;
try {
self::$api
->create('identifier', [
'identifier' => 1234,
]);
usleep(AUTH0_PHP_TEST_INTEGRATION_SLEEP);
} catch (CoreException $e) {
$caught_create_invalid_identifier_field_exception = $this
->errorHasString($e, 'Invalid "identifier" field');
}
$this
->assertTrue($caught_create_invalid_identifier_field_exception);
}
}