Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Aug 9, 2018
1 parent eb65bb5 commit 6107947
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
18 changes: 10 additions & 8 deletions src/API/Helpers/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class RequestBuilder
*
* @var string
*/
protected $returnType = 'body';
protected $returnType;

/**
* RequestBuilder constructor.
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/API/Management.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions tests/API/Management/AuthApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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']);
}
Expand Down
60 changes: 58 additions & 2 deletions tests/RequestBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace Auth0\Tests;
namespace Auth0\Tests\API;

use Auth0\SDK\API\Helpers\RequestBuilder;
use Auth0\SDK\API\Management;
use Auth0\SDK\Exception\CoreException;

class RequestBuilderTest extends \PHPUnit_Framework_TestCase
class RequestBuilderTest extends ApiTests
{

public function testUrl()
Expand Down Expand Up @@ -109,4 +111,58 @@ public function testgGetGuzzleOptionsWithBasePath()
$this->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 );
}
}

0 comments on commit 6107947

Please sign in to comment.