Skip to content

Commit

Permalink
Add PHPUnit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Oct 12, 2022
1 parent 0d5e5cd commit 5bf34d9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 8 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
"require": {
"php": "^8.0",
"league/oauth2-client": "^2.6",
"league/oauth2-server": "^8.3"
"league/oauth2-server": "^8.3",
"mockery/mockery": "^1.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"pestphp/pest": "^1.20",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9",
"spatie/ray": "^1.28"
},
"autoload": {
Expand Down
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

145 changes: 145 additions & 0 deletions tests/GenesysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

use Mockery;
use League\OAuth2\Client\Token\AccessToken;
use Vormkracht10\OAuth2Genesys\Provider\Genesys;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;

class GenesysTest extends \PHPUnit\Framework\TestCase
{
protected $provider;

protected static function getMethod($name)
{
$class = new ReflectionClass('ChrisHemmings\OAuth2\Client\Provider\DigitalOcean');

$method = $class->getMethod($name);
$method->setAccessible(true);

return $method;
}

protected function setUp(): void
{
$this->provider = new Genesys([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]);
}

public function tearDown(): void
{
Mockery::close();
parent::tearDown();
}

public function testAuthorizationUrl()
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
parse_str($uri['query'], $query);
$this->assertArrayHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_uri', $query);
$this->assertArrayHasKey('state', $query);
$this->assertArrayHasKey('scope', $query);
$this->assertArrayHasKey('response_type', $query);
$this->assertArrayHasKey('approval_prompt', $query);
$this->assertNotNull($this->provider->getState());
}

public function testGetAuthorizationUrl()
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
$this->assertEquals('/oauth/authorize', $uri['path']);
}

public function testGetBaseAccessTokenUrl()
{
$params = [];
$url = $this->provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertEquals('/oauth/token', $uri['path']);
}

public function testGetAccessToken()
{
$testResponse = [
'access_token' => '1a2b3b4c5d6e7f8g9h0i',
'token_type' => 'bearer',
'expires_in' => 2592000,
'refresh_token' => '0i9h8g7f6e5d4c3b2a1',
];
$response = Mockery::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getBody')->andReturn(\json_encode($testResponse));
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$response->shouldReceive('getStatusCode')->andReturn(200);

$client = Mockery::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($response);

$this->provider->setHttpClient($client);

/** @var AccessToken $token */
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);

$this->assertEquals($testResponse['access_token'], $token->getToken());
$this->assertEquals(time() + $testResponse['expires_in'], $token->getExpires());
$this->assertEquals($testResponse['refresh_token'], $token->getRefreshToken());
}

public function testUserData()
{
$response_data = [
'id' => rand(1000, 9999),
];

$postResponse = Mockery::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "token_type":"bearer"}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn(200);
$userResponse = Mockery::mock('Psr\Http\Message\ResponseInterface');
$userResponse->shouldReceive('getBody')->andReturn(json_encode($response_data));
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$userResponse->shouldReceive('getStatusCode')->andReturn(200);

$client = Mockery::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(2)
->andReturn($postResponse, $userResponse);

$this->provider->setHttpClient($client);

$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$user = $this->provider->getResourceOwner($token);

$this->assertEquals($response_data['id'], $user->getId());
}

/**
* @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
*/
public function testExceptionThrownWhenErrorObjectReceived()
{
$message = uniqid();
$status = rand(400, 600);

$postResponse = Mockery::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn(' {"error":"'.$message.'"}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn($status);

$client = Mockery::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);

$this->provider->setHttpClient($client);

$this->expectException(IdentityProviderException::class);

$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);

}
}
1 change: 0 additions & 1 deletion tests/Pest.php

This file was deleted.

0 comments on commit 5bf34d9

Please sign in to comment.