diff --git a/phpunit.xml b/phpunit.xml index e89ac6d..3fd23bc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,9 +10,14 @@ stopOnFailure="false" syntaxCheck="false" > + + + ./src + + ./tests/ - \ No newline at end of file + diff --git a/src/Provider/Mollie.php b/src/Provider/Mollie.php index ff7de14..daf21ab 100644 --- a/src/Provider/Mollie.php +++ b/src/Provider/Mollie.php @@ -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. * @@ -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'; } /** @@ -94,7 +130,7 @@ public function getBaseAuthorizationUrl () */ public function getBaseAccessTokenUrl (array $params) { - return static::MOLLIE_API_URL . '/oauth2/tokens'; + return $this->mollieApiUrl . '/oauth2/tokens'; } /** diff --git a/tests/src/Provider/MollieTest.php b/tests/src/Provider/MollieTest.php index 5436430..311e2bb 100644 --- a/tests/src/Provider/MollieTest.php +++ b/tests/src/Provider/MollieTest.php @@ -1,20 +1,30 @@ 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() { @@ -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', @@ -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); @@ -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", @@ -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); @@ -201,4 +214,19 @@ public function testUserData() ); $this->assertEquals('370355724', $array['registrationNumber']); } -} \ No newline at end of file + + 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); + } +}