Skip to content

Commit

Permalink
Merge pull request #19 from mollie/feature/set-endpoints
Browse files Browse the repository at this point in the history
Endpoints setters
  • Loading branch information
Martijn Smit authored Oct 25, 2019
2 parents d870741 + 60b2cdc commit f626d42
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 40 deletions.
7 changes: 6 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
stopOnFailure="false"
syntaxCheck="false"
>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
42 changes: 39 additions & 3 deletions src/Provider/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,51 @@ class Mollie extends AbstractProvider
const SCOPE_ONBOARDING_READ = 'onboarding.read';
const SCOPE_ONBOARDING_WRITE = 'onboarding.write';

/**
* @var string
*/
private $mollieApiUrl = self::MOLLIE_API_URL;

/**
* @var string
*/
private $mollieWebUrl = self::MOLLIE_WEB_URL;

public function __construct(array $options = [], array $collaborators = [])
{
parent::__construct($options, $collaborators);
parent::__construct($options, $collaborators);

if (isset($options["clientId"]) && strpos($options["clientId"], self::CLIENT_ID_PREFIX) !== 0) {
throw new \DomainException("Mollie needs the client ID to be prefixed with " . self::CLIENT_ID_PREFIX . ".");
}
}

/**
* Define Mollie api URL
*
* @param string $url
* @return Mollie
*/
public function setMollieApiUrl ($url)
{
$this->mollieApiUrl = $url;

return $this;
}

/**
* Define Mollie web URL
*
* @param string $url
* @return Mollie
*/
public function setMollieWebUrl ($url)
{
$this->mollieWebUrl = $url;

return $this;
}

/**
* Returns the base URL for authorizing a client.
*
Expand All @@ -81,7 +117,7 @@ public function __construct(array $options = [], array $collaborators = [])
*/
public function getBaseAuthorizationUrl ()
{
return static::MOLLIE_WEB_URL . '/oauth2/authorize';
return $this->mollieWebUrl . '/oauth2/authorize';
}

/**
Expand All @@ -94,7 +130,7 @@ public function getBaseAuthorizationUrl ()
*/
public function getBaseAccessTokenUrl (array $params)
{
return static::MOLLIE_API_URL . '/oauth2/tokens';
return $this->mollieApiUrl . '/oauth2/tokens';
}

/**
Expand Down
100 changes: 64 additions & 36 deletions tests/src/Provider/MollieTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
<?php namespace Mollie\OAuth2\Client\Test\Provider;

use GuzzleHttp\ClientInterface;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use Mockery as m;
use Mollie\OAuth2\Client\Provider\Mollie;
use Psr\Http\Message\ResponseInterface;

class MollieTest extends \PHPUnit_Framework_TestCase
{
const MOCK_CLIENT_ID = 'app_mock_client_id';
const MOCK_SECRET = 'mock_secret';
const REDIRECT_URI = 'none';

const OPTIONS = [
'clientId' => self::MOCK_CLIENT_ID,
'clientSecret' => self::MOCK_SECRET,
'redirectUri' => self::REDIRECT_URI,
];

protected $provider;

protected function setUp ()
{
$this->provider = new Mollie([
'clientId' => 'app_mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]);
}
protected function setUp()
{
$this->provider = new Mollie(self::OPTIONS);
}

public function tearDown()
{
Expand All @@ -27,7 +37,7 @@ public function testClientIdShouldThrowExceptionWhenNotPrefixed()
$this->expectException(\DomainException::class);
$this->expectExceptionMessage("Mollie needs the client ID to be prefixed with " . Mollie::CLIENT_ID_PREFIX . ".");

$provider = new \Mollie\OAuth2\Client\Provider\Mollie([
new Mollie([
'clientId' => 'not_pefixed_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
Expand All @@ -37,44 +47,48 @@ public function testClientIdShouldThrowExceptionWhenNotPrefixed()
public function testGetBaseAccessTokenUrl()
{
$params = [];

$url = $this->provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertEquals('/oauth2/tokens', $uri['path']);

$this->assertEquals('https://api.mollie.com/oauth2/tokens', $url);
}

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());
$authUrl = $this->provider->getAuthorizationUrl();

list($url, $queryString) = explode('?', $authUrl);
parse_str($queryString, $query);

$this->assertEquals('https://www.mollie.com/oauth2/authorize', $url);
$this->assertEquals([
'state' => $this->provider->getState(),
'client_id' => self::MOCK_CLIENT_ID,
'redirect_uri' => self::REDIRECT_URI,
'scope' => 'organizations.read',
'response_type' => 'code',
'approval_prompt' => 'auto',
], $query);
$this->assertRegExp('/^[a-f0-9]{32}$/i', $this->provider->getState());
}

public function testResourceOwnerDetailsUrl()
{
$token = m::mock(\League\OAuth2\Client\Token\AccessToken::class);
$token = m::mock(AccessToken::class);

$url = $this->provider->getResourceOwnerDetailsUrl($token);
$uri = parse_url($url);

$this->assertEquals('/v2/organizations/me', $uri['path']);
$this->assertEquals('https://api.mollie.com/v2/organizations/me', $url);
}

public function testGetAccessToken()
{
$response = m::mock(\Psr\Http\Message\ResponseInterface::class);
$response = m::mock(ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "token_type":"bearer"}');
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$response->shouldReceive('getStatusCode')->andReturn(200);

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

$this->provider->setHttpClient($client);
Expand All @@ -87,38 +101,37 @@ public function testGetAccessToken()
$this->assertNull($token->getResourceOwnerId());
}

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

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

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

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

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

public function testUserData()
{
$postResponse = m::mock(\Psr\Http\Message\ResponseInterface::class);
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn(
'access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token'
);
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
$postResponse->shouldReceive('getStatusCode')->andReturn(200);

$accountResponse = m::mock(\Psr\Http\Message\ResponseInterface::class);
$accountResponse = m::mock(ResponseInterface::class);
$accountResponse->shouldReceive('getBody')->andReturn(
'{
"resource": "organization",
Expand Down Expand Up @@ -175,7 +188,7 @@ public function testUserData()
$accountResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$accountResponse->shouldReceive('getStatusCode')->andReturn(200);

$client = m::mock(\GuzzleHttp\ClientInterface::class);
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')
->times(2)
->andReturn($postResponse, $accountResponse);
Expand All @@ -201,4 +214,19 @@ public function testUserData()
);
$this->assertEquals('370355724', $array['registrationNumber']);
}
}

public function testWhenDefiningADifferentMollieApiUrlThenUseThisOnApiCalls()
{
$this->provider->setMollieApiUrl('https://api.mollie.nl');

$this->assertEquals('https://api.mollie.nl/oauth2/tokens', $this->provider->getBaseAccessTokenUrl([]));
}

public function testWhenDefiningADifferentMollieWebUrlThenUseThisForAuthorize()
{
$this->provider->setMollieWebUrl('https://www.mollie.nl');

list($url) = explode('?', $this->provider->getAuthorizationUrl());
$this->assertEquals('https://www.mollie.nl/oauth2/authorize', $url);
}
}

0 comments on commit f626d42

Please sign in to comment.