From daf0ab0c899f8615a825d98b73a78a01ed75c642 Mon Sep 17 00:00:00 2001 From: Julius Kiekbusch Date: Mon, 11 May 2020 19:45:38 +0200 Subject: [PATCH] Add dynamic calls via property (#121) * Add dynamic calls via property * Add Property Reference Test * Add dynamic property calls to docs * Add unknown method and unknown property * Fix expectException * Update unknown property message --- README.md | 9 +++ src/Wrappers/MollieApiWrapper.php | 19 +++++ tests/Wrappers/MollieApiWrapperTest.php | 99 +++++++++++++++++++------ 3 files changed, 106 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index aa4935e..5dba97d 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,15 @@ if ($payment->isPaid()) } ``` +instead of using any method i.e. +```php +Mollie::api()->payments(); +``` +you could also access it like in the core api via the property +```php +Mollie::api()->payments; +``` + ### Global helper method For your convencience we've added the global `mollie()` helper function. It's an easy shortcut to the `Mollie::api()` facade accessor. diff --git a/src/Wrappers/MollieApiWrapper.php b/src/Wrappers/MollieApiWrapper.php index 6f1d4a7..004bb4b 100644 --- a/src/Wrappers/MollieApiWrapper.php +++ b/src/Wrappers/MollieApiWrapper.php @@ -247,4 +247,23 @@ public function wallets() { return $this->client->wallets; } + + /** + * Handle dynamic property calls. + * + * @param string $property + * @return mixed + */ + public function __get($property) + { + if (method_exists($this, $property)) { + return call_user_func([$this, $property]); + } + + $message = '%s has no property or method "%s".'; + + throw new \Error( + sprintf($message, static::class, $property) + ); + } } diff --git a/tests/Wrappers/MollieApiWrapperTest.php b/tests/Wrappers/MollieApiWrapperTest.php index fb7637d..e9304f3 100644 --- a/tests/Wrappers/MollieApiWrapperTest.php +++ b/tests/Wrappers/MollieApiWrapperTest.php @@ -20,6 +20,26 @@ class MollieApiWrapperTest extends TestCase */ protected $api; + protected $endpoints = [ + 'chargebacks', + 'customers', + 'customerPayments', + 'invoices', + 'mandates', + 'methods', + 'mandates', + 'onboarding', + 'orders', + 'organizations', + 'permissions', + 'payments', + 'profiles', + 'refunds', + 'settlements', + 'subscriptions', + 'wallets', + ]; + /** * @before */ @@ -81,37 +101,58 @@ public function testSetBadToken() public function testWrappedEndpoints() { - $endpoints = [ - 'chargebacks', - 'customers', - 'customerPayments', - 'invoices', - 'mandates', - 'methods', - 'mandates', - 'onboarding', - 'orders', - 'organizations', - 'permissions', - 'payments', - 'profiles', - 'refunds', - 'settlements', - 'subscriptions', - 'wallets', - ]; - $client = $this->app[MollieApiClient::class]; $wrapper = new MollieApiWrapper( $this->app['config'], $client ); - foreach ($endpoints as $endpoint) { + 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. @@ -127,4 +168,20 @@ 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); + } }