generated from vormkracht10/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.