diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 0000000..a9d0346 --- /dev/null +++ b/UPGRADE.md @@ -0,0 +1,21 @@ +![Mollie](https://www.mollie.nl/files/Mollie-Logo-Style-Small.png) + +# Migrating from Laravel-Mollie v2.x to v3 + +### Step 1: Update composer dependencies + +Update `composer.json` to match this: + +```json +"require": { + "mollie/laravel-mollie": "^3.0" +} +``` + +Then run `composer update mollie/laravel-mollie`. + +### Step 2: + + +## Stuck? +Feel free to open an [issue](https://github.com/mollie/laravel-mollie/issues). diff --git a/src/Facades/Mollie.php b/src/Facades/Mollie.php index c8dd2cb..aa96cbe 100644 --- a/src/Facades/Mollie.php +++ b/src/Facades/Mollie.php @@ -35,13 +35,62 @@ namespace Mollie\Laravel\Facades; use Illuminate\Support\Facades\Facade; -use Mollie\Laravel\MollieManager; -use Mollie\Laravel\Wrappers\MollieApiWrapper; +use Mollie\Api\MollieApiClient; /** * (Facade) Class Mollie. * - * @method static MollieApiWrapper api() + * @method \Mollie\Api\Endpoints\BalanceEndpoint balanceReports() + * @method \Mollie\Api\Endpoints\BalanceEndpoint balances() + * @method \Mollie\Api\Endpoints\BalanceTransactionEndpoint balanceTransactions() + * @method \Mollie\Api\Endpoints\ChargebackEndpoint chargebacks() + * @method \Mollie\Api\Endpoints\ClientLinkEndpoint clientLinks() + * @method \Mollie\Api\Endpoints\ClientEndpoint clients() + * @method \Mollie\Api\Endpoints\CustomerPaymentEndpoint customerPayments() + * @method \Mollie\Api\Endpoints\CustomerEndpoint customers() + * @method \Mollie\Api\Endpoints\InvoiceEndpoint invoices() + * @method \Mollie\Api\Endpoints\OnboardingEndpoint onboarding() + * @method \Mollie\Api\Endpoints\OrderLineEndpoint orderLines() + * @method \Mollie\Api\Endpoints\OrderPaymentEndpoint orderPayments() + * @method \Mollie\Api\Endpoints\OrderRefundEndpoint orderRefunds() + * @method \Mollie\Api\Endpoints\OrderEndpoint orders() + * @method \Mollie\Api\Endpoints\OrganizationPartnerEndpoint organizationPartners() + * @method \Mollie\Api\Endpoints\OrganizationEndpoint organizations() + * @method \Mollie\Api\Endpoints\PaymentCaptureEndpoint paymentCaptures() + * @method \Mollie\Api\Endpoints\PaymentChargebackEndpoint paymentChargebacks() + * @method \Mollie\Api\Endpoints\PaymentLinkEndpoint paymentLinks() + * @method \Mollie\Api\Endpoints\PaymentRefundEndpoint paymentRefunds() + * @method \Mollie\Api\Endpoints\PaymentRouteEndpoint paymentRoutes() + * @method \Mollie\Api\Endpoints\PaymentEndpoint payments() + * @method \Mollie\Api\Endpoints\PermissionEndpoint permissions() + * @method \Mollie\Api\Endpoints\ProfileMethodEndpoint profileMethods() + * @method \Mollie\Api\Endpoints\ProfileEndpoint profiles() + * @method \Mollie\Api\Endpoints\MandateEndpoint mandates() + * @method \Mollie\Api\Endpoints\MethodEndpoint methods() + * @method \Mollie\Api\Endpoints\RefundEndpoint refunds() + * @method \Mollie\Api\Endpoints\SettlementCaptureEndpoint settlementCaptures() + * @method \Mollie\Api\Endpoints\SettlementChargebackEndpoint settlementChargebacks() + * @method \Mollie\Api\Endpoints\SettlementPaymentEndpoint settlementPayments() + * @method \Mollie\Api\Endpoints\SettlementRefundEndpoint settlementRefunds() + * @method \Mollie\Api\Endpoints\SettlementEndpoint settlements() + * @method \Mollie\Api\Endpoints\ShipmentEndpoint shipments() + * @method \Mollie\Api\Endpoints\SubscriptionEndpoint subscriptions() + * @method \Mollie\Api\Endpoints\TerminalEndpoint terminals() + * @method \Mollie\Api\Endpoints\WalletEndpoint wallets() + * @method MollieApiClient setApiEndpoint(string $url) + * @method string getApiEndpoint() + * @method string getVersionStrings() + * @method MollieApiClient setApiKey(string $apiKey) + * @method MollieApiClient setAccessToken(string $accessToken) + * @method ?bool usesOAuth() + * @method MollieApiClient addVersionString(string $versionString) + * @method void enableDebugging() + * @method void disableDebugging() + * @method MollieApiClient setIdempotencyKey(string $key) + * @method string getIdempotencyKey() + * @method MollieApiClient resetIdempotencyKey() + * @method MollieApiClient setIdempotencyKeyGenerator(\Mollie\Api\Idempotency\IdempotencyKeyGeneratorContract $generator) + * @method MollieApiClient clearIdempotencyKeyGenerator() */ class Mollie extends Facade { @@ -52,6 +101,6 @@ class Mollie extends Facade */ protected static function getFacadeAccessor() { - return MollieManager::class; + return MollieApiClient::class; } } diff --git a/src/MollieConnectProvider.php b/src/MollieConnectProvider.php index dd35e3a..6ccdfad 100644 --- a/src/MollieConnectProvider.php +++ b/src/MollieConnectProvider.php @@ -1,4 +1,5 @@ buildAuthUrlFromBase(static::MOLLIE_WEB_URL.'/oauth2/authorize', $state); + return $this->buildAuthUrlFromBase(static::MOLLIE_WEB_URL . '/oauth2/authorize', $state); } /** @@ -89,66 +89,7 @@ protected function getAuthUrl($state) */ protected function getTokenUrl() { - return static::MOLLIE_API_URL.'/oauth2/tokens'; - } - - /** - * Get the access token for the given code. - * - * @param string $code - * @return string - */ - public function getAccessToken($code) - { - $response = $this->getHttpClient()->post($this->getTokenUrl(), [ - 'headers' => ['Authorization' => 'Basic '.base64_encode($this->clientId.':'.$this->clientSecret)], - 'form_params' => $this->getTokenFields($code), - ]); - - return $this->parseAccessToken($response->getBody()); - } - - /** - * Get the access token with a refresh token. - * - * @param string $refresh_token - * @return array - */ - public function getRefreshTokenResponse($refresh_token) - { - $response = $this->getHttpClient()->post($this->getTokenUrl(), [ - 'headers' => ['Accept' => 'application/json'], - 'form_params' => $this->getRefreshTokenFields($refresh_token), - ]); - - return json_decode($response->getBody(), true); - } - - /** - * Get the refresh tokenfields with a refresh token. - * - * @param string $refresh_token - * @return array - */ - protected function getRefreshTokenFields($refresh_token) - { - return [ - 'client_id' => $this->clientId, - 'client_secret' => $this->clientSecret, - 'grant_type' => 'refresh_token', - 'refresh_token' => $refresh_token, - ]; - } - - /** - * Get the POST fields for the token request. - * - * @param string $code - * @return array - */ - public function getTokenFields($code) - { - return Arr::add(parent::getTokenFields($code), 'grant_type', 'authorization_code'); + return static::MOLLIE_API_URL . '/oauth2/tokens'; } /** @@ -159,8 +100,8 @@ public function getTokenFields($code) */ protected function getUserByToken($token) { - $response = $this->getHttpClient()->get(static::MOLLIE_API_URL.'/v2/organizations/me', [ - 'headers' => ['Authorization' => 'Bearer '.$token], + $response = $this->getHttpClient()->get(static::MOLLIE_API_URL . '/v2/organizations/me', [ + 'headers' => ['Authorization' => 'Bearer ' . $token], ]); return json_decode($response->getBody(), true); diff --git a/src/MollieLaravelHttpClientAdapter.php b/src/MollieLaravelHttpClientAdapter.php index cbd87ef..d52ffb3 100644 --- a/src/MollieLaravelHttpClientAdapter.php +++ b/src/MollieLaravelHttpClientAdapter.php @@ -9,8 +9,6 @@ class MollieLaravelHttpClientAdapter implements MollieHttpAdapterInterface { - private const HTTP_NO_CONTENT = 204; - public function send($httpMethod, $url, $headers, $httpBody): ?object { $contentType = $headers['Content-Type'] ?? 'application/json'; @@ -20,19 +18,17 @@ public function send($httpMethod, $url, $headers, $httpBody): ?object ->withHeaders($headers) ->send($httpMethod, $url); - return $this->parseResponseBody($response); + return match (true) { + $response->noContent() => null, + $response->failed() => throw ApiException::createFromResponse($response->toPsrResponse(), null), + empty($response->body()) => throw new ApiException("Mollie response body is empty."), + default => $this->parseResponseBody($response), + }; } private function parseResponseBody(Response $response): ?object { $body = $response->body(); - if (empty($body)) { - if ($response->status() === self::HTTP_NO_CONTENT) { - return null; - } - - throw new ApiException('No response body found.'); - } $object = @json_decode($body); @@ -40,10 +36,6 @@ private function parseResponseBody(Response $response): ?object throw new ApiException("Unable to decode Mollie response: '{$body}'."); } - if ($response->status() >= 400) { - throw ApiException::createFromResponse($response->toPsrResponse(), null); - } - return $object; } diff --git a/src/MollieManager.php b/src/MollieManager.php deleted file mode 100644 index e5f47a9..0000000 --- a/src/MollieManager.php +++ /dev/null @@ -1,67 +0,0 @@ - - * @copyright Mollie B.V. - * - * @link https://www.mollie.com - */ - -namespace Mollie\Laravel; - -use Illuminate\Contracts\Container\Container; -use Mollie\Laravel\Wrappers\MollieApiWrapper; - -/** - * Class MollieManager. - */ -class MollieManager -{ - /** - * @var Container - */ - protected $app; - - /** - * MollieManager constructor. - * - * @return void - */ - public function __construct(Container $app) - { - $this->app = $app; - } - - /** - * @return mixed - */ - public function api() - { - return $this->app->make(MollieApiWrapper::class); - } -} diff --git a/src/MollieServiceProvider.php b/src/MollieServiceProvider.php index 6a2620e..db9dce0 100644 --- a/src/MollieServiceProvider.php +++ b/src/MollieServiceProvider.php @@ -37,7 +37,6 @@ use Illuminate\Contracts\Container\Container; use Illuminate\Support\ServiceProvider; use Mollie\Api\MollieApiClient; -use Mollie\Laravel\Wrappers\MollieApiWrapper; /** * Class MollieServiceProvider. @@ -98,13 +97,17 @@ protected function extendSocialite() */ public function register() { - $this->app->singleton(MollieManager::class); - $this->app->singleton(MollieApiWrapper::class); $this->app->singleton( MollieApiClient::class, - function () { - return (new MollieApiClient(new MollieLaravelHttpClientAdapter)) + function (Container $app) { + $client = (new MollieApiClient(new MollieLaravelHttpClientAdapter)) ->addVersionString('MollieLaravel/' . self::PACKAGE_VERSION); + + if (!empty($apiKey = $app['config']['mollie.key'])) { + $client->setApiKey($apiKey); + } + + return $client; } ); } diff --git a/src/Wrappers/MollieApiWrapper.php b/src/Wrappers/MollieApiWrapper.php deleted file mode 100644 index a71b748..0000000 --- a/src/Wrappers/MollieApiWrapper.php +++ /dev/null @@ -1,247 +0,0 @@ - - * @copyright Mollie B.V. - * - * @link https://www.mollie.com - */ - -namespace Mollie\Laravel\Wrappers; - -use Illuminate\Contracts\Config\Repository; -use Illuminate\Support\Traits\ForwardsCalls; -use Mollie\Api\MollieApiClient; - -/** - * Class MollieApiWrapper. - * - * @property-read \Mollie\Api\Endpoints\BalanceEndpoint $balanceReports - * @property-read \Mollie\Api\Endpoints\BalanceEndpoint $balances - * @property-read \Mollie\Api\Endpoints\BalanceTransactionEndpoint $balanceTransactions - * @property-read \Mollie\Api\Endpoints\ChargebackEndpoint $chargebacks - * @property-read \Mollie\Api\Endpoints\ClientLinkEndpoint $clientLinks - * @property-read \Mollie\Api\Endpoints\ClientEndpoint $clients - * @property-read \Mollie\Api\Endpoints\CustomerPaymentEndpoint $customerPayments - * @property-read \Mollie\Api\Endpoints\CustomerEndpoint $customers - * @property-read \Mollie\Api\Endpoints\InvoiceEndpoint $invoices - * @property-read \Mollie\Api\Endpoints\OnboardingEndpoint $onboarding - * @property-read \Mollie\Api\Endpoints\OrderLineEndpoint $orderLines - * @property-read \Mollie\Api\Endpoints\OrderPaymentEndpoint $orderPayments - * @property-read \Mollie\Api\Endpoints\OrderRefundEndpoint $orderRefunds - * @property-read \Mollie\Api\Endpoints\OrderEndpoint $orders - * @property-read \Mollie\Api\Endpoints\OrganizationPartnerEndpoint $organizationPartners - * @property-read \Mollie\Api\Endpoints\OrganizationEndpoint $organizations - * @property-read \Mollie\Api\Endpoints\PaymentCaptureEndpoint $paymentCaptures - * @property-read \Mollie\Api\Endpoints\PaymentChargebackEndpoint $paymentChargebacks - * @property-read \Mollie\Api\Endpoints\PaymentLinkEndpoint $paymentLinks - * @property-read \Mollie\Api\Endpoints\PaymentRefundEndpoint $paymentRefunds - * @property-read \Mollie\Api\Endpoints\PaymentRouteEndpoint $paymentRoutes - * @property-read \Mollie\Api\Endpoints\PaymentEndpoint $payments - * @property-read \Mollie\Api\Endpoints\PermissionEndpoint $permissions - * @property-read \Mollie\Api\Endpoints\ProfileMethodEndpoint $profileMethods - * @property-read \Mollie\Api\Endpoints\ProfileEndpoint $profiles - * @property-read \Mollie\Api\Endpoints\MandateEndpoint $mandates - * @property-read \Mollie\Api\Endpoints\MethodEndpoint $methods - * @property-read \Mollie\Api\Endpoints\RefundEndpoint $refunds - * @property-read \Mollie\Api\Endpoints\SettlementCaptureEndpoint $settlementCaptures - * @property-read \Mollie\Api\Endpoints\SettlementChargebackEndpoint $settlementChargebacks - * @property-read \Mollie\Api\Endpoints\SettlementPaymentEndpoint $settlementPayments - * @property-read \Mollie\Api\Endpoints\SettlementRefundEndpoint $settlementRefunds - * @property-read \Mollie\Api\Endpoints\SettlementEndpoint $settlements - * @property-read \Mollie\Api\Endpoints\ShipmentEndpoint $shipments - * @property-read \Mollie\Api\Endpoints\SubscriptionEndpoint $subscriptions - * @property-read \Mollie\Api\Endpoints\TerminalEndpoint $terminals - * @property-read \Mollie\Api\Endpoints\WalletEndpoint $wallets - * @method \Mollie\Api\Endpoints\BalanceEndpoint balanceReports() - * @method \Mollie\Api\Endpoints\BalanceEndpoint balances() - * @method \Mollie\Api\Endpoints\BalanceTransactionEndpoint balanceTransactions() - * @method \Mollie\Api\Endpoints\ChargebackEndpoint chargebacks() - * @method \Mollie\Api\Endpoints\ClientLinkEndpoint clientLinks() - * @method \Mollie\Api\Endpoints\ClientEndpoint clients() - * @method \Mollie\Api\Endpoints\CustomerPaymentEndpoint customerPayments() - * @method \Mollie\Api\Endpoints\CustomerEndpoint customers() - * @method \Mollie\Api\Endpoints\InvoiceEndpoint invoices() - * @method \Mollie\Api\Endpoints\OnboardingEndpoint onboarding() - * @method \Mollie\Api\Endpoints\OrderLineEndpoint orderLines() - * @method \Mollie\Api\Endpoints\OrderPaymentEndpoint orderPayments() - * @method \Mollie\Api\Endpoints\OrderRefundEndpoint orderRefunds() - * @method \Mollie\Api\Endpoints\OrderEndpoint orders() - * @method \Mollie\Api\Endpoints\OrganizationPartnerEndpoint organizationPartners() - * @method \Mollie\Api\Endpoints\OrganizationEndpoint organizations() - * @method \Mollie\Api\Endpoints\PaymentCaptureEndpoint paymentCaptures() - * @method \Mollie\Api\Endpoints\PaymentChargebackEndpoint paymentChargebacks() - * @method \Mollie\Api\Endpoints\PaymentLinkEndpoint paymentLinks() - * @method \Mollie\Api\Endpoints\PaymentRefundEndpoint paymentRefunds() - * @method \Mollie\Api\Endpoints\PaymentRouteEndpoint paymentRoutes() - * @method \Mollie\Api\Endpoints\PaymentEndpoint payments() - * @method \Mollie\Api\Endpoints\PermissionEndpoint permissions() - * @method \Mollie\Api\Endpoints\ProfileMethodEndpoint profileMethods() - * @method \Mollie\Api\Endpoints\ProfileEndpoint profiles() - * @method \Mollie\Api\Endpoints\MandateEndpoint mandates() - * @method \Mollie\Api\Endpoints\MethodEndpoint methods() - * @method \Mollie\Api\Endpoints\RefundEndpoint refunds() - * @method \Mollie\Api\Endpoints\SettlementCaptureEndpoint settlementCaptures() - * @method \Mollie\Api\Endpoints\SettlementChargebackEndpoint settlementChargebacks() - * @method \Mollie\Api\Endpoints\SettlementPaymentEndpoint settlementPayments() - * @method \Mollie\Api\Endpoints\SettlementRefundEndpoint settlementRefunds() - * @method \Mollie\Api\Endpoints\SettlementEndpoint settlements() - * @method \Mollie\Api\Endpoints\ShipmentEndpoint shipments() - * @method \Mollie\Api\Endpoints\SubscriptionEndpoint subscriptions() - * @method \Mollie\Api\Endpoints\TerminalEndpoint terminals() - * @method \Mollie\Api\Endpoints\WalletEndpoint wallets() - * @method MollieApiWrapper setApiEndpoint(string $url) - * @method string getApiEndpoint() - * @method string getVersionStrings() - * @method MollieApiWrapper setApiKey(string $apiKey) - * @method MollieApiWrapper setAccessToken(string $accessToken) - * @method ?bool usesOAuth() - * @method MollieApiWrapper addVersionString(string $versionString) - * @method void enableDebugging() - * @method void disableDebugging() - * @method MollieApiWrapper setIdempotencyKey(string $key) - * @method string getIdempotencyKey() - * @method MollieApiWrapper resetIdempotencyKey() - * @method MollieApiWrapper setIdempotencyKeyGenerator(\Mollie\Api\Idempotency\IdempotencyKeyGeneratorContract $generator) - * @method MollieApiWrapper clearIdempotencyKeyGenerator() - */ -class MollieApiWrapper -{ - use ForwardsCalls; - - /** - * @var Repository - */ - protected $config; - - /** - * @var MollieApiClient - */ - protected $client; - - protected static $supportedClientEndpoints = [ - 'balanceReports', - 'balances', - 'balanceTransactions', - 'chargebacks', - 'clientLinks', - 'clients', - 'customerPayments', - 'customers', - 'invoices', - 'onboarding', - 'orderLines', - 'orderPayments', - 'orderRefunds', - 'orders', - 'organizationPartners', - 'organizations', - 'paymentCaptures', - 'paymentChargebacks', - 'paymentLinks', - 'paymentRefunds', - 'paymentRoutes', - 'payments', - 'permissions', - 'profileMethods', - 'profiles', - 'mandates', - 'methods', - 'refunds', - 'settlementCaptures', - 'settlementChargebacks', - 'settlementPayments', - 'settlementRefunds', - 'settlements', - 'shipments', - 'subscriptions', - 'terminals', - 'wallets', - ]; - - /** - * MollieApiWrapper constructor. - * - * @return void - * - * @throws \Mollie\Api\Exceptions\ApiException - */ - public function __construct(Repository $config, MollieApiClient $client) - { - $this->config = $config; - $this->client = $client; - - $key = $this->config->get('mollie.key'); - - if (!empty($key)) { - $this->client->setApiKey($key); - } - } - - /** - * Handle dynamic property calls. - * - * @param string $property - * @return mixed - */ - public function __get($property) - { - if ($this->endpointPropertyExists($property)) { - return $this->client->{$property}; - } - - $message = '%s has no endpoint "%s".'; - - throw new \Error( - sprintf($message, static::class, $property) - ); - } - - /** - * Handle dynamic method calls into the Mollie API client. - * - * @param string $method - * @param array $parameters - * @return mixed - * - * @throws \Mollie\Api\Exceptions\ApiException - */ - public function __call($method, $parameters) - { - if ($this->endpointPropertyExists($method)) { - return $this->client->{$method}; - } - - return $this->forwardDecoratedCallTo($this->client, $method, $parameters); - } - - private function endpointPropertyExists(string $property): bool - { - return in_array($property, static::$supportedClientEndpoints); - } -} diff --git a/tests/Facades/MollieTest.php b/tests/Facades/MollieTest.php deleted file mode 100644 index 76e9b36..0000000 --- a/tests/Facades/MollieTest.php +++ /dev/null @@ -1,46 +0,0 @@ -assertInstanceOf( + MollieLaravelHttpClientAdapter::class, + $this->getUnaccessiblePropertyValue('httpClient') + ); + } + + public function testApiKeyIsSetOnResolvingApiClient() + { + config(['mollie.key' => 'test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxyz']); + + $this->assertEquals( + 'test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxyz', + $this->getUnaccessiblePropertyValue('apiKey') + ); + } + + public function testDoesNotSetApiKeyIfKeyIsEmpty() + { + config(['mollie.key' => '']); + + $this->assertEquals( + null, + $this->getUnaccessiblePropertyValue('apiKey') + ); + } + + private function getUnaccessiblePropertyValue(string $propertyName): mixed + { + $resolvedInstance = resolve(MollieApiClient::class); + + $reflection = new ReflectionClass($resolvedInstance); + $property = $reflection->getProperty($propertyName); + $property->setAccessible(true); + + return $property->getValue($resolvedInstance); + } +} diff --git a/tests/MollieConnectProviderTest.php b/tests/MollieConnectProviderTest.php deleted file mode 100644 index b1a3cf7..0000000 --- a/tests/MollieConnectProviderTest.php +++ /dev/null @@ -1,73 +0,0 @@ -markTestSkipped('Laravel Socialite must be present.'); - } - } - - public function testRedirectGeneratesTheProperSymfonyRedirectResponse() - { - $request = Request::create('foo'); - $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface')); - $session->shouldReceive('put')->once(); - $provider = new MollieConnectProvider($request, 'client_id', 'client_secret', 'redirect'); - $response = $provider->redirect(); - $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); - $this->assertStringStartsWith( - 'https://www.mollie.com/oauth2/authorize?client_id=client_id&redirect_uri=redirect&scope=organizations.read&response_type=code&state=', - $response->getTargetUrl() - ); - } - - public function testExceptionIsThrownIfStateIsInvalid() - { - $this->expectException(\Laravel\Socialite\Two\InvalidStateException::class); - $request = Request::create('foo', 'GET', ['state' => str_repeat('B', 40), 'code' => 'code']); - $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface')); - $session->shouldReceive('pull')->once()->with('state')->andReturn(str_repeat('A', 40)); - $provider = new MollieConnectProvider($request, 'client_id', 'client_secret', 'redirect'); - $user = $provider->user(); - } - - public function testExceptionIsThrownIfStateIsNotSet() - { - $this->expectException(\Laravel\Socialite\Two\InvalidStateException::class); - $request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']); - $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface')); - $session->shouldReceive('pull')->once()->with('state'); - $provider = new MollieConnectProvider($request, 'client_id', 'client_secret', 'redirect'); - $user = $provider->user(); - } - - public function testGetTokenFields() - { - $request = Request::create('foo'); - $provider = new MollieConnectProvider($request, 'client_id', 'client_secret', 'redirect'); - $this->assertEquals( - [ - 'client_id' => 'client_id', - 'client_secret' => 'client_secret', - 'code' => 'dummy_code', - 'redirect_uri' => 'redirect', - 'grant_type' => 'authorization_code', - ], - $provider->getTokenFields('dummy_code') - ); - } -} diff --git a/tests/Wrappers/MollieApiLaravelClientTest.php b/tests/MollieLaravelHttpClientAdapterTest.php similarity index 89% rename from tests/Wrappers/MollieApiLaravelClientTest.php rename to tests/MollieLaravelHttpClientAdapterTest.php index 60b730f..f0bbecc 100644 --- a/tests/Wrappers/MollieApiLaravelClientTest.php +++ b/tests/MollieLaravelHttpClientAdapterTest.php @@ -1,6 +1,6 @@ Http::response(json_encode($payment)), ]); - $client->setApiKey('test_nawruSyCR7UE84EhtVmMmDGdRswBqj'); $returnedPayment = $client->payments->create([ 'redirectUrl' => 'https://google.com/redirect', 'description' => 'test', @@ -53,7 +52,6 @@ public function testGetRequest() 'https://api.mollie.com/v2/payments/'.$payment->id => Http::response(json_encode($payment)), ]); - $client->setApiKey('test_nawruSyCR7UE84EhtVmMmDGdRswBqj'); $returnedPayment = $client->payments->get($payment->id); $this->assertEquals($payment->id, $returnedPayment->id); diff --git a/tests/MollieManagerTest.php b/tests/MollieManagerTest.php deleted file mode 100644 index b1ab634..0000000 --- a/tests/MollieManagerTest.php +++ /dev/null @@ -1,37 +0,0 @@ -setUpManagerInstance(); - $this->assertInstanceOf(MollieManager::class, $this->manager); - } - - public function testApiMethod() - { - $this->setUpManagerInstance(); - $this->assertInstanceOf(MollieApiWrapper::class, $this->manager->api()); - $this->assertSame($this->app['mollie.api'], $this->manager->api()); - } - - public function setUpManagerInstance() - { - $this->manager = new MollieManager($this->app); - } -} diff --git a/tests/MollieServiceProviderTest.php b/tests/MollieServiceProviderTest.php deleted file mode 100644 index 2fdffa0..0000000 --- a/tests/MollieServiceProviderTest.php +++ /dev/null @@ -1,31 +0,0 @@ -assertIsInjectable(MollieManager::class); - } - - public function testMollieApiWrapperIsInjectable() - { - $this->assertIsInjectable(MollieApiWrapper::class); - } - - public function testMollieApiClientIsInjectable() - { - $this->assertIsInjectable(MollieApiClient::class); - } -} diff --git a/tests/TempHelpers/FacadeTrait.php b/tests/TempHelpers/FacadeTrait.php deleted file mode 100644 index 2640d6f..0000000 --- a/tests/TempHelpers/FacadeTrait.php +++ /dev/null @@ -1,76 +0,0 @@ -getFacadeClass(); - $reflection = new ReflectionClass($class); - $facade = new ReflectionClass(Facade::class); - - $msg = "Expected class '$class' to be a facade."; - - $this->assertTrue($reflection->isSubclassOf($facade), $msg); - } - - public function testFacadeAccessor() - { - $accessor = $this->getFacadeAccessor(); - $class = $this->getFacadeClass(); - $reflection = new ReflectionClass($class); - $method = $reflection->getMethod('getFacadeAccessor'); - $method->setAccessible(true); - - $msg = "Expected class '$class' to have an accessor of '$accessor'."; - - $this->assertSame($accessor, $method->invoke(null), $msg); - } - - public function testFacadeRoot() - { - $root = $this->getFacadeRoot(); - $class = $this->getFacadeClass(); - $reflection = new ReflectionClass($class); - $method = $reflection->getMethod('getFacadeRoot'); - $method->setAccessible(true); - - $msg = "Expected class '$class' to have a root of '$root'."; - - $this->assertInstanceOf($root, $method->invoke(null), $msg); - } -} diff --git a/tests/TempHelpers/LaravelTrait.php b/tests/TempHelpers/LaravelTrait.php deleted file mode 100644 index e27a49c..0000000 --- a/tests/TempHelpers/LaravelTrait.php +++ /dev/null @@ -1,67 +0,0 @@ -makeInjectableClass($name); - $this->assertInstanceOf($name, $class->getInjectedObject()); - } catch (Exception $e) { - $injectable = false; - if ($msg = $e->getMessage()) { - $message .= " $msg"; - } - } - - $this->assertTrue($injectable, $message); - } - - /** - * Register and make a stub class to inject into. - * - * - * @return object - */ - protected function makeInjectableClass(string $name) - { - do { - $class = 'testBenchStub'.Str::random(); - } while (class_exists($class)); - - eval(" - class $class - { - protected \$object; - - public function __construct(\\$name \$object) - { - \$this->object = \$object; - } - - public function getInjectedObject() - { - return \$this->object; - } - } - "); - - return $this->app->make($class); - } -} diff --git a/tests/TempHelpers/MockeryTrait.php b/tests/TempHelpers/MockeryTrait.php deleted file mode 100644 index 51f6598..0000000 --- a/tests/TempHelpers/MockeryTrait.php +++ /dev/null @@ -1,30 +0,0 @@ -addToAssertionCount($container->mockery_getExpectationCount()); - } - - Mockery::close(); - } - } -} diff --git a/tests/TempHelpers/ServiceProviderTrait.php b/tests/TempHelpers/ServiceProviderTrait.php deleted file mode 100644 index 20624a4..0000000 --- a/tests/TempHelpers/ServiceProviderTrait.php +++ /dev/null @@ -1,44 +0,0 @@ -getServiceProviderClass($this->app); - - $reflection = new ReflectionClass($class); - - $provider = new ReflectionClass(ServiceProvider::class); - - $msg = "Expected class '$class' to be a service provider."; - - $this->assertTrue($reflection->isSubclassOf($provider), $msg); - } - - public function testProvides() - { - $class = $this->getServiceProviderClass($this->app); - $reflection = new ReflectionClass($class); - - $method = $reflection->getMethod('provides'); - $method->setAccessible(true); - - $msg = "Expected class '$class' to provide a valid list of services."; - - $this->assertIsArray($method->invoke(new $class($this->app)), $msg); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index a51058a..0f9dbe4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,18 +10,6 @@ */ abstract class TestCase extends \Orchestra\Testbench\TestCase { - use LaravelTrait; - - /** - * Get the service provider class. - * - * @return string - */ - protected function getServiceProviderClass() - { - return MollieServiceProvider::class; - } - /** * Get package providers. * diff --git a/tests/Wrappers/MollieApiWrapperTest.php b/tests/Wrappers/MollieApiWrapperTest.php deleted file mode 100644 index 37fd851..0000000 --- a/tests/Wrappers/MollieApiWrapperTest.php +++ /dev/null @@ -1,230 +0,0 @@ -api = $this->getMockBuilder(MollieApiClient::class) - ->disableOriginalConstructor() - ->getMock(); - } - - public function testConstruct() - { - $wrapper = new MollieApiWrapper($this->app['config'], $this->app[MollieApiClient::class]); - $this->assertInstanceOf(MollieApiWrapper::class, $wrapper); - } - - public function testApiEndpoint() - { - $this->api->expects($this->once())->method('setApiEndpoint'); - $this->api->expects($this->once())->method('getApiEndpoint')->willReturn('/test'); - - $wrapper = new MollieApiWrapper($this->app['config'], $this->api); - - $wrapper->setApiEndpoint('/test'); - $this->assertSame('/test', $wrapper->getApiEndpoint()); - } - - public function testSetGoodApiKey() - { - $this->api->expects($this->atLeastOnce())->method('setApiKey')->with('test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); - - $wrapper = new MollieApiWrapper($this->app['config'], $this->api); - $wrapper->setApiKey('test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); - } - - public function testSetBadApiKey() - { - $this->expectException(\Mollie\Api\Exceptions\ApiException::class); - $this->expectExceptionMessage("Invalid API key: 'live_'. An API key must start with 'test_' or 'live_' and must be at least 30 characters long."); - $wrapper = new MollieApiWrapper($this->app['config'], $this->app[MollieApiClient::class]); - $wrapper->setApiKey('live_'); - } - - public function testDoesNotSetKeyWhenEmpty() - { - config(['mollie.key' => '']); - $this->assertEmpty($this->app['config']['mollie']['key']); - $this->api->expects($this->never())->method('setApiKey'); - - $wrapper = new MollieApiWrapper($this->app['config'], $this->api); - } - - public function testSetGoodToken() - { - $this->api->expects($this->once())->method('setAccessToken')->with('access_xxx'); - - $wrapper = new MollieApiWrapper($this->app['config'], $this->api); - $wrapper->setAccessToken('access_xxx'); - } - - public function testSetBadToken() - { - $this->expectException(\Mollie\Api\Exceptions\ApiException::class); - $this->expectExceptionMessage("Invalid OAuth access token: 'BAD'. An access token must start with 'access_'."); - $wrapper = new MollieApiWrapper($this->app['config'], $this->app[MollieApiClient::class]); - $wrapper->setAccessToken('BAD'); - } - - public function testEnableDebugging() - { - $this->api->expects($this->once())->method('enableDebugging'); - - $wrapper = new MollieApiWrapper($this->app['config'], $this->api); - $wrapper->enableDebugging(); - } - - public function testDisableDebugging() - { - $this->api->expects($this->once())->method('disableDebugging'); - - $wrapper = new MollieApiWrapper($this->app['config'], $this->api); - $wrapper->disableDebugging(); - } - - public function testWrappedEndpoints() - { - $client = $this->app[MollieApiClient::class]; - $wrapper = new MollieApiWrapper( - $this->app['config'], - $client - ); - - foreach ($this->endpoints as $endpoint) { - $this->assertWrappedEndpoint($client, $wrapper, $endpoint); - } - } - - public function testWrappedPropertyEndpoints() - { - $client = $this->app[MollieApiClient::class]; - $wrapper = new MollieApiWrapper( - $this->app['config'], - $client - ); - - foreach ($this->endpoints as $endpoint) { - $this->assertWrappedPropertyEndpoint($client, $wrapper, $endpoint); - } - } - - public function testUnknownWrappedEndpoint() - { - $client = $this->app[MollieApiClient::class]; - $wrapper = new MollieApiWrapper( - $this->app['config'], - $client - ); - - $this->expectException(\Error::class); - $this->expectExceptionMessage('Call to undefined method Mollie\Laravel\Wrappers\MollieApiWrapper::unknown()'); - - $wrapper->unknown(); - } - - public function testUnknownWrappedPropertyEndpoint() - { - $client = $this->app[MollieApiClient::class]; - $wrapper = new MollieApiWrapper( - $this->app['config'], - $client - ); - - $this->expectException(\Error::class); - $this->expectExceptionMessage('Mollie\Laravel\Wrappers\MollieApiWrapper has no property or method "unknown".'); - - $wrapper->unknown; - } - - /** - * Asserts that the referenced wrapper method matches the client attribute - * I.e. $wrapper->payments() returns the same as $client->payments. - * - * @param MollieApiClient $client - * @param MollieApiWrapper $wrapper - * @param string $reference - * @return null - * - * @throws ExpectationFailedException - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - */ - protected function assertWrappedEndpoint($client, $wrapper, $reference) - { - $this->assertEquals($client->$reference, $wrapper->$reference()); - } - - /** - * Asserts that the referenced wrapper property matches the client attribute - * I.e. $wrapper->payments returns the same as $client->payments. - * - * @param MollieApiClient $client - * @param MollieApiWrapper $wrapper - * @param string $reference - * @return null - * - * @throws ExpectationFailedException - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - */ - protected function assertWrappedPropertyEndpoint($client, $wrapper, $reference) - { - $this->assertEquals($client->$reference, $wrapper->$reference); - } -}