Skip to content

Commit

Permalink
Add dynamic calls via property (#121)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Jubeki authored May 11, 2020
1 parent ec96b7b commit daf0ab0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 21 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
19 changes: 19 additions & 0 deletions src/Wrappers/MollieApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}
99 changes: 78 additions & 21 deletions tests/Wrappers/MollieApiWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}

0 comments on commit daf0ab0

Please sign in to comment.