From 542e777e86cd571ea7a9235fd435f3f347c7752f Mon Sep 17 00:00:00 2001 From: Thiago Cordeiro Date: Wed, 23 Oct 2019 10:28:26 +0200 Subject: [PATCH 1/6] Created setters for web and api url --- phpunit.xml | 7 ++- src/Provider/Mollie.php | 40 +++++++++++- tests/src/Provider/MollieTest.php | 101 +++++++++++++++++++----------- 3 files changed, 110 insertions(+), 38 deletions(-) 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 b7e53c1..6abfe42 100644 --- a/src/Provider/Mollie.php +++ b/src/Provider/Mollie.php @@ -56,6 +56,42 @@ 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; + + /** + * 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. * @@ -65,7 +101,7 @@ class Mollie extends AbstractProvider */ public function getBaseAuthorizationUrl () { - return static::MOLLIE_WEB_URL . '/oauth2/authorize'; + return $this->mollieWebUrl . '/oauth2/authorize'; } /** @@ -78,7 +114,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 6458c29..c66e347 100644 --- a/tests/src/Provider/MollieTest.php +++ b/tests/src/Provider/MollieTest.php @@ -1,19 +1,28 @@ provider = new \Mollie\OAuth2\Client\Provider\Mollie([ - 'clientId' => 'mock_client_id', - 'clientSecret' => 'mock_secret', - 'redirectUri' => 'none', - ]); - } + protected function setUp() + { + $this->provider = new Mollie([ + 'clientId' => self::MOCK_CLIENT_ID, + 'clientSecret' => self::MOCK_SECRET, + 'redirectUri' => self::REDIRECT_URI, + ]); + } public function tearDown() { @@ -24,44 +33,48 @@ public function tearDown() 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); @@ -74,38 +87,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", @@ -162,7 +174,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); @@ -188,4 +200,23 @@ public function testUserData() ); $this->assertEquals('370355724', $array['registrationNumber']); } -} \ No newline at end of file + + public function testWhenDefiningADifferentMollieApiUrlThenUseThisOnApiCalls() + { + $provider = new Mollie(['clientId' => '', 'clientSecret' => '', 'redirectUri' => '']); + + $provider->setMollieApiUrl('https://api.mollie.nl'); + + $this->assertEquals('https://api.mollie.nl/oauth2/tokens', $provider->getBaseAccessTokenUrl([])); + } + + public function testWhenDefiningADifferentMollieWebUrlThenUseThisForAuthorize() + { + $provider = new Mollie(['clientId' => '', 'clientSecret' => '', 'redirectUri' => '']); + + $provider->setMollieWebUrl('https://www.mollie.nl'); + + list($url) = explode('?', $provider->getAuthorizationUrl()); + $this->assertEquals('https://www.mollie.nl/oauth2/authorize', $url); + } +} From 580f7d18424b411c3abc743fff39de7dba861231 Mon Sep 17 00:00:00 2001 From: Thiago Cordeiro Date: Wed, 23 Oct 2019 10:30:36 +0200 Subject: [PATCH 2/6] converted spaces into tabs --- src/Provider/Mollie.php | 62 ++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/Provider/Mollie.php b/src/Provider/Mollie.php index 6abfe42..d232a03 100644 --- a/src/Provider/Mollie.php +++ b/src/Provider/Mollie.php @@ -57,40 +57,40 @@ class Mollie extends AbstractProvider const SCOPE_ONBOARDING_WRITE = 'onboarding.write'; /** - * @var string - */ + * @var string + */ private $mollieApiUrl = self::MOLLIE_API_URL; /** - * @var string - */ - private $mollieWebUrl = self::MOLLIE_WEB_URL; - - /** - * 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; - } + * @var string + */ + private $mollieWebUrl = self::MOLLIE_WEB_URL; + + /** + * 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. From e4160916e7c47e1b59a5b2df8d0c75112dacdfb3 Mon Sep 17 00:00:00 2001 From: Thiago Cordeiro Date: Wed, 23 Oct 2019 14:57:52 +0200 Subject: [PATCH 3/6] Prefixing mollie client id --- tests/src/Provider/MollieTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/src/Provider/MollieTest.php b/tests/src/Provider/MollieTest.php index b277394..bdb63af 100644 --- a/tests/src/Provider/MollieTest.php +++ b/tests/src/Provider/MollieTest.php @@ -9,7 +9,7 @@ class MollieTest extends \PHPUnit_Framework_TestCase { - const MOCK_CLIENT_ID = 'mock_client_id'; + const MOCK_CLIENT_ID = 'app_mock_client_id'; const MOCK_SECRET = 'mock_secret'; const REDIRECT_URI = 'none'; @@ -215,7 +215,7 @@ public function testUserData() public function testWhenDefiningADifferentMollieApiUrlThenUseThisOnApiCalls() { - $provider = new Mollie(['clientId' => '', 'clientSecret' => '', 'redirectUri' => '']); + $provider = new Mollie(['clientId' => self::MOCK_CLIENT_ID, 'clientSecret' => '', 'redirectUri' => '']); $provider->setMollieApiUrl('https://api.mollie.nl'); @@ -224,7 +224,7 @@ public function testWhenDefiningADifferentMollieApiUrlThenUseThisOnApiCalls() public function testWhenDefiningADifferentMollieWebUrlThenUseThisForAuthorize() { - $provider = new Mollie(['clientId' => '', 'clientSecret' => '', 'redirectUri' => '']); + $provider = new Mollie(['clientId' => self::MOCK_CLIENT_ID, 'clientSecret' => '', 'redirectUri' => '']); $provider->setMollieWebUrl('https://www.mollie.nl'); From ca64dc39ecb3ccac95931e48a86e5af254ecc8cb Mon Sep 17 00:00:00 2001 From: Thiago Cordeiro Date: Wed, 23 Oct 2019 16:36:56 +0200 Subject: [PATCH 4/6] Removed setters, passing options on constructor --- src/Provider/Mollie.php | 38 +++++++------------------------ tests/src/Provider/MollieTest.php | 22 ++++++++++-------- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/Provider/Mollie.php b/src/Provider/Mollie.php index 72e2e2b..bfb6c9e 100644 --- a/src/Provider/Mollie.php +++ b/src/Provider/Mollie.php @@ -65,48 +65,26 @@ class Mollie extends AbstractProvider public function __construct(array $options = [], array $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 . "."); } + + $this->mollieApiUrl = (isset($options['mollieApiUrl']) ? $options['mollieApiUrl'] : null) ?: self::MOLLIE_API_URL; + $this->mollieWebUrl = (isset($options['mollieWebUrl']) ? $options['mollieWebUrl'] : null) ?: self::MOLLIE_WEB_URL; + unset($options['mollieApiUrl'], $options['mollieWebUrl']); + + parent::__construct($options, $collaborators); } /** * @var string */ - private $mollieApiUrl = self::MOLLIE_API_URL; + private $mollieApiUrl; /** * @var string */ - private $mollieWebUrl = self::MOLLIE_WEB_URL; - - /** - * 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; - } + private $mollieWebUrl; /** * Returns the base URL for authorizing a client. diff --git a/tests/src/Provider/MollieTest.php b/tests/src/Provider/MollieTest.php index bdb63af..70471cc 100644 --- a/tests/src/Provider/MollieTest.php +++ b/tests/src/Provider/MollieTest.php @@ -13,15 +13,17 @@ class MollieTest extends \PHPUnit_Framework_TestCase 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' => self::MOCK_CLIENT_ID, - 'clientSecret' => self::MOCK_SECRET, - 'redirectUri' => self::REDIRECT_URI, - ]); + $this->provider = new Mollie(self::OPTIONS); } public function tearDown() @@ -35,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', @@ -215,18 +217,18 @@ public function testUserData() public function testWhenDefiningADifferentMollieApiUrlThenUseThisOnApiCalls() { - $provider = new Mollie(['clientId' => self::MOCK_CLIENT_ID, 'clientSecret' => '', 'redirectUri' => '']); + $options = array_merge(['mollieApiUrl' => 'https://api.mollie.nl'], self::OPTIONS); - $provider->setMollieApiUrl('https://api.mollie.nl'); + $provider = new Mollie($options); $this->assertEquals('https://api.mollie.nl/oauth2/tokens', $provider->getBaseAccessTokenUrl([])); } public function testWhenDefiningADifferentMollieWebUrlThenUseThisForAuthorize() { - $provider = new Mollie(['clientId' => self::MOCK_CLIENT_ID, 'clientSecret' => '', 'redirectUri' => '']); + $options = array_merge(['mollieWebUrl' => 'https://www.mollie.nl'], self::OPTIONS); - $provider->setMollieWebUrl('https://www.mollie.nl'); + $provider = new Mollie($options); list($url) = explode('?', $provider->getAuthorizationUrl()); $this->assertEquals('https://www.mollie.nl/oauth2/authorize', $url); From 9a4e7305df27a18abc94fd7cc3ea55a06355abc3 Mon Sep 17 00:00:00 2001 From: Thiago Cordeiro Date: Wed, 23 Oct 2019 16:39:45 +0200 Subject: [PATCH 5/6] Moved properties before constructor --- src/Provider/Mollie.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Provider/Mollie.php b/src/Provider/Mollie.php index bfb6c9e..428bc5a 100644 --- a/src/Provider/Mollie.php +++ b/src/Provider/Mollie.php @@ -63,6 +63,16 @@ class Mollie extends AbstractProvider const SCOPE_ONBOARDING_READ = 'onboarding.read'; const SCOPE_ONBOARDING_WRITE = 'onboarding.write'; + /** + * @var string + */ + private $mollieApiUrl; + + /** + * @var string + */ + private $mollieWebUrl; + public function __construct(array $options = [], array $collaborators = []) { if (isset($options["clientId"]) && strpos($options["clientId"], self::CLIENT_ID_PREFIX) !== 0) { @@ -76,16 +86,6 @@ public function __construct(array $options = [], array $collaborators = []) parent::__construct($options, $collaborators); } - /** - * @var string - */ - private $mollieApiUrl; - - /** - * @var string - */ - private $mollieWebUrl; - /** * Returns the base URL for authorizing a client. * From 60b2cdce6c10c1a91a4219d18efb5064adf9568e Mon Sep 17 00:00:00 2001 From: aydinbahadir Date: Thu, 24 Oct 2019 14:18:19 +0200 Subject: [PATCH 6/6] Putting setters back --- src/Provider/Mollie.php | 36 +++++++++++++++++++++++++------ tests/src/Provider/MollieTest.php | 12 ++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Provider/Mollie.php b/src/Provider/Mollie.php index 428bc5a..daf21ab 100644 --- a/src/Provider/Mollie.php +++ b/src/Provider/Mollie.php @@ -66,25 +66,47 @@ class Mollie extends AbstractProvider /** * @var string */ - private $mollieApiUrl; + private $mollieApiUrl = self::MOLLIE_API_URL; /** * @var string */ - private $mollieWebUrl; + private $mollieWebUrl = self::MOLLIE_WEB_URL; public function __construct(array $options = [], array $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 . "."); } + } - $this->mollieApiUrl = (isset($options['mollieApiUrl']) ? $options['mollieApiUrl'] : null) ?: self::MOLLIE_API_URL; - $this->mollieWebUrl = (isset($options['mollieWebUrl']) ? $options['mollieWebUrl'] : null) ?: self::MOLLIE_WEB_URL; - unset($options['mollieApiUrl'], $options['mollieWebUrl']); + /** + * Define Mollie api URL + * + * @param string $url + * @return Mollie + */ + public function setMollieApiUrl ($url) + { + $this->mollieApiUrl = $url; - parent::__construct($options, $collaborators); - } + 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. diff --git a/tests/src/Provider/MollieTest.php b/tests/src/Provider/MollieTest.php index 70471cc..311e2bb 100644 --- a/tests/src/Provider/MollieTest.php +++ b/tests/src/Provider/MollieTest.php @@ -217,20 +217,16 @@ public function testUserData() public function testWhenDefiningADifferentMollieApiUrlThenUseThisOnApiCalls() { - $options = array_merge(['mollieApiUrl' => 'https://api.mollie.nl'], self::OPTIONS); + $this->provider->setMollieApiUrl('https://api.mollie.nl'); - $provider = new Mollie($options); - - $this->assertEquals('https://api.mollie.nl/oauth2/tokens', $provider->getBaseAccessTokenUrl([])); + $this->assertEquals('https://api.mollie.nl/oauth2/tokens', $this->provider->getBaseAccessTokenUrl([])); } public function testWhenDefiningADifferentMollieWebUrlThenUseThisForAuthorize() { - $options = array_merge(['mollieWebUrl' => 'https://www.mollie.nl'], self::OPTIONS); - - $provider = new Mollie($options); + $this->provider->setMollieWebUrl('https://www.mollie.nl'); - list($url) = explode('?', $provider->getAuthorizationUrl()); + list($url) = explode('?', $this->provider->getAuthorizationUrl()); $this->assertEquals('https://www.mollie.nl/oauth2/authorize', $url); } }