From 6107947e19f50d577c0e0ead0c26fc33a8e29d8c Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Tue, 7 Aug 2018 08:59:05 -0700 Subject: [PATCH] Add tests --- src/API/Helpers/RequestBuilder.php | 18 +++++---- src/API/Management.php | 4 +- tests/API/Management/AuthApiTest.php | 9 ++++- tests/RequestBuilderTest.php | 60 +++++++++++++++++++++++++++- 4 files changed, 77 insertions(+), 14 deletions(-) diff --git a/src/API/Helpers/RequestBuilder.php b/src/API/Helpers/RequestBuilder.php index 33627c10..0dfa9abf 100644 --- a/src/API/Helpers/RequestBuilder.php +++ b/src/API/Helpers/RequestBuilder.php @@ -98,7 +98,7 @@ class RequestBuilder * * @var string */ - protected $returnType = 'body'; + protected $returnType; /** * RequestBuilder constructor. @@ -119,20 +119,18 @@ public function __construct(array $config) $this->path = $config['path']; } - if (array_key_exists('returnType', $config)) { - $this->setReturnType( $config['returnType'] ); - } + $this->setReturnType( isset( $config['returnType'] ) ? $config['returnType'] : null ); } /** * Magic method to overload method calls to paths. * - * @param string $name Method invoked. - * @param array $arguments Arguments to add to the path. + * @param string $name Method invoked. + * @param array|null $arguments Arguments to add to the path. * * @return RequestBuilder */ - public function __call($name, array $arguments) + public function __call($name, $arguments) { $argument = null; @@ -412,8 +410,12 @@ public function withDictParams($params) */ public function setReturnType($type) { + if ( empty( $type ) ) { + $type = 'body'; + } + if (! in_array($type, $this->returnTypes)) { - throw new CoreException('Invalid return type'); + throw new CoreException('Invalid returnType'); } $this->returnType = $type; diff --git a/src/API/Management.php b/src/API/Management.php index beebf39d..671552b2 100644 --- a/src/API/Management.php +++ b/src/API/Management.php @@ -163,9 +163,9 @@ class Management * @param string $token * @param string $domain * @param array $guzzleOptions - * @param string $returnType + * @param string|null $returnType */ - public function __construct($token, $domain, $guzzleOptions = [], $returnType = 'body') + public function __construct($token, $domain, $guzzleOptions = [], $returnType = null) { $this->token = $token; $this->domain = $domain; diff --git a/tests/API/Management/AuthApiTest.php b/tests/API/Management/AuthApiTest.php index f4131153..064aa4af 100644 --- a/tests/API/Management/AuthApiTest.php +++ b/tests/API/Management/AuthApiTest.php @@ -35,7 +35,12 @@ public function testAuthorizeWithRO() $api = new Authentication($env['DOMAIN'], $env['APP_CLIENT_ID']); - $response = $api->authorize_with_ro('auth@test.com', '123456', 'openid', 'Username-Password-Authentication'); + $response = $api->authorize_with_ro( + 'auth@test.com', + '123456', + 'openid email', + 'Username-Password-Authentication' + ); $this->assertArrayHasKey('id_token', $response); $this->assertArrayHasKey('access_token', $response); @@ -46,7 +51,7 @@ public function testAuthorizeWithRO() $this->assertArrayHasKey('email', $userinfo); $this->assertArrayHasKey('email_verified', $userinfo); - $this->assertArrayHasKey('user_id', $userinfo); + $this->assertArrayHasKey('sub', $userinfo); $this->assertEquals('german@auth0.com', $userinfo['email']); $this->assertEquals('auth0|58adc60b82b0ca0774643eef', $userinfo['user_id']); } diff --git a/tests/RequestBuilderTest.php b/tests/RequestBuilderTest.php index 90fb5fcb..6dad9455 100644 --- a/tests/RequestBuilderTest.php +++ b/tests/RequestBuilderTest.php @@ -1,9 +1,11 @@ assertArrayHasKey('base_uri', $options); $this->assertEquals('www.domain.com/api', $options['base_uri']); } + + /** + * Test that the return type is set properly and returns the correct result. + */ + public function testReturnType() { + $env = self::getEnvStatic(); + $token = self::getTokenStatic($env, ['tenant_settings' => ['actions' => ['read']]]); + + // Test default return type matches "body". + $api = new Management($token, $env['DOMAIN'], []); + $results_default = $api->tenants->get(); + $this->assertTrue( is_array( $results_default ) ); + + $api = new Management($token, $env['DOMAIN'], [], 'body'); + $results_body = $api->tenants->get(); + $this->assertEquals( $results_default, $results_body ); + + // Test that "headers" return type contains expected keys. + $api = new Management($token, $env['DOMAIN'], [], 'headers'); + $results_headers = $api->tenants->get(); + $this->assertArrayHasKey( 'x-ratelimit-limit', $results_headers ); + $this->assertArrayHasKey( 'x-ratelimit-remaining', $results_headers ); + $this->assertArrayHasKey( 'x-ratelimit-reset', $results_headers ); + + // Test that "object" return type returns the correct object type. + $api = new Management($token, $env['DOMAIN'], [], 'object'); + $results_object = $api->tenants->get(); + $this->assertInstanceOf( 'GuzzleHttp\Psr7\Response', $results_object ); + + // Test that "object" return type returns the correct object type. + $api = new Management($token, $env['DOMAIN'], [], 'statusCode'); + $results_code = $api->tenants->get(); + $this->assertEquals( 200, $results_code ); + + // Test that "object" return type returns the correct object type. + $api = new Management($token, $env['DOMAIN'], [], 'reasonPhrase'); + $results_reason = $api->tenants->get(); + $this->assertEquals( 'OK', $results_reason ); + + // Test that "object" return type returns the correct object type. + $api = new Management($token, $env['DOMAIN'], [], 'protocolVersion'); + $results_ver = $api->tenants->get(); + $this->assertEquals( '1.1', $results_ver ); + + // Test that an invalid return type throws an error. + $caught_return_type_error = false; + try { + $api = new Management($token, $env['DOMAIN'], [], '__invalid_return_type__'); + $api->tenants->get(); + } catch ( CoreException $e ) { + $caught_return_type_error = $this->errorHasString( $e, 'Invalid returnType' ); + } + $this->assertTrue( $caught_return_type_error ); + } }