Skip to content

Commit

Permalink
fix: accessing related resource collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Feb 18, 2025
1 parent 174098f commit 593a308
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 54 deletions.
12 changes: 10 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ parameters:
path: examples/capabilities/get-capability.php

-
message: "#^Variable \\$mollie might not be defined\\.$#"
message: '#^Variable \$mollie might not be defined\.$#'
identifier: variable.undefined
count: 1
path: examples/capabilities/list-capabilities.php

-
message: "#^Variable \\$mollie might not be defined\\.$#"
message: '#^Variable \$mollie might not be defined\.$#'
identifier: variable.undefined
count: 1
path: examples/captures/create-capture.php

Expand Down Expand Up @@ -292,6 +294,12 @@ parameters:
count: 5
path: src/Http/Data/DataCollection.php

-
message: '#^Unsafe usage of new static\(\)\.$#'
identifier: new.static
count: 1
path: src/Resources/ResourceCollection.php

-
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with true will always evaluate to true\.$#'
identifier: method.alreadyNarrowedType
Expand Down
5 changes: 4 additions & 1 deletion src/Contracts/IsResponseAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ interface IsResponseAware extends ViableResponse
{
public function getResponse(): Response;

public function setResponse(Response $response): self;
/**
* @return $this
*/
public function setResponse(Response $response);
}
24 changes: 9 additions & 15 deletions src/Fake/ResourceResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
use Mollie\Api\Exceptions\LogicException;
use Mollie\Api\Resources\BaseResource;
use Mollie\Api\Traits\ForwardsCalls;
use ReflectionClass;

/**
* Builder for creating mock responses for Mollie API resources.
*
* @method self embed(string $collectionClass) Embed a collection of resources into the response
* @method self add(array $data) Add a resource to the embedded response
* @method self addMany(array $data) Add multiple resources to the embedded response
*/
class ResourceResponseBuilder
{
Expand Down Expand Up @@ -49,7 +52,12 @@ public function with(array $data): self
*/
public function create(): MockResponse
{
$data = $this->data;
$reflection = new ReflectionClass($this->resourceClass);
$baseName = strtolower($reflection->getShortName());

$sampleContents = json_decode(FakeResponseLoader::load($baseName), true);

$data = array_merge(['_links' => $sampleContents['_links']], $this->data);
$data['_embedded'] = [];

foreach ($this->embeddedBuilders as $key => $builder) {
Expand All @@ -63,20 +71,6 @@ public function create(): MockResponse
unset($data['_embedded']);
}

// add standard links
if (empty($data['_links'])) {
$data['_links'] = [
'self' => [
'href' => '...',
'type' => 'application/hal+json',
],
'documentation' => [
'href' => '...',
'type' => 'text/html',
],
];
}

return new MockResponse($data);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Client extends BaseResource implements EmbeddedResourcesContract
public $_links;

/**
* @var \stdClass[]
* @var \stdClass
*/
public $_embedded;

Expand Down
10 changes: 5 additions & 5 deletions src/Resources/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class Payment extends BaseResource implements EmbeddedResourcesContract
public $_links;

/**
* @var \stdClass[]
* @var \stdClass
*/
public $_embedded;

Expand Down Expand Up @@ -594,7 +594,7 @@ public function hasSplitPayments(): bool
public function refunds(): RefundCollection
{
if (! isset($this->_links->refunds->href)) {
return new RefundCollection($this->connector, $this->response);
return RefundCollection::withResponse($this->response, $this->connector);
}

return $this
Expand Down Expand Up @@ -636,7 +636,7 @@ public function listRefunds(array $parameters = []): RefundCollection
public function captures(): CaptureCollection
{
if (! isset($this->_links->captures->href)) {
return new CaptureCollection($this->connector, $this->response);
return CaptureCollection::withResponse($this->response, $this->connector);
}

return $this
Expand Down Expand Up @@ -667,7 +667,7 @@ public function getCapture($captureId, array $parameters = []): Capture
public function chargebacks(): ChargebackCollection
{
if (! isset($this->_links->chargebacks->href)) {
return new ChargebackCollection($this->connector, $this->response);
return ChargebackCollection::withResponse($this->response, $this->connector);
}

return $this
Expand Down Expand Up @@ -720,7 +720,7 @@ public function update(): Payment
$this->redirectUrl,
$this->cancelUrl,
$this->webhookUrl,
new Metadata($this->metadata),
new Metadata((array) $this->metadata),
$this->method,
$this->locale,
$this->restrictPaymentMethodsToCountry,
Expand Down
30 changes: 9 additions & 21 deletions src/Resources/PaymentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Mollie\Api\Resources;

use Mollie\Api\Traits\HasMode;

/**
* @property \Mollie\Api\MollieApiClient $connector
*/
class PaymentLink extends BaseResource
{
use HasMode;

/**
* Id of the payment link (on the Mollie platform).
*
Expand Down Expand Up @@ -136,12 +140,10 @@ public function getCheckoutUrl(): ?string
*/
public function update(): ?PaymentLink
{
$body = $this->withTestmode([
return $this->connector->paymentLinks->update($this->id, $this->withMode([
'description' => $this->description,
'archived' => $this->archived,
]);

return $this->connector->paymentLinks->update($this->id, $body);
]));
}

/**
Expand All @@ -153,11 +155,9 @@ public function update(): ?PaymentLink
*/
public function archive()
{
$data = $this->withTestmode([
return $this->connector->paymentLinks->update($this->id, $this->withMode([
'archived' => true,
]);

return $this->connector->paymentLinks->update($this->id, $data);
]));
}

/**
Expand All @@ -171,19 +171,7 @@ public function payments(?string $from = null, ?int $limit = null, array $filter
$this,
$from,
$limit,
$this->withTestmode($filters)
$this->withMode($filters)
);
}

/**
* Apply the preset options.
*
* @return array
*/
private function withTestmode(array $options)
{
return array_merge([
'testmode' => $this->mode === 'test',
], $options);
}
}
8 changes: 4 additions & 4 deletions src/Resources/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function update(): ?Profile
public function chargebacks(): ChargebackCollection
{
if (! isset($this->_links->chargebacks->href)) {
return new ChargebackCollection($this->connector, $this->response);
return ChargebackCollection::withResponse($this->response, $this->connector);
}

return $this
Expand All @@ -140,7 +140,7 @@ public function chargebacks(): ChargebackCollection
public function methods(): MethodCollection
{
if (! isset($this->_links->methods->href)) {
return new MethodCollection($this->connector, $this->response);
return MethodCollection::withResponse($this->response, $this->connector);
}

return $this
Expand Down Expand Up @@ -178,7 +178,7 @@ public function disableMethod(string $methodId): void
public function payments(): PaymentCollection
{
if (! isset($this->_links->payments->href)) {
return new PaymentCollection($this->connector, $this->response);
return PaymentCollection::withResponse($this->response, $this->connector);
}

return $this
Expand All @@ -194,7 +194,7 @@ public function payments(): PaymentCollection
public function refunds(): RefundCollection
{
if (! isset($this->_links->refunds->href)) {
return new RefundCollection($this->connector, $this->response);
return RefundCollection::withResponse($this->response, $this->connector);
}

return $this
Expand Down
13 changes: 13 additions & 0 deletions src/Resources/ResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Mollie\Api\Resources;

use Mollie\Api\Contracts\Connector;
use Mollie\Api\Http\Response;

abstract class ResourceCollection extends BaseCollection
{
/**
Expand All @@ -17,4 +20,14 @@ public static function getResourceClass(): string

return static::$resource;
}

/**
* @return $this
*/
public static function withResponse(Response $response, Connector $connector, $items = [], ?\stdClass $_links = null): self
{
$collection = new static($connector, $items, $_links);

return $collection->setResponse($response);
}
}
2 changes: 1 addition & 1 deletion src/Resources/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function cancel(): ?Subscription
public function payments(): PaymentCollection
{
if (! isset($this->_links->payments->href)) {
return new PaymentCollection($this->connector, $this->response);
return PaymentCollection::withResponse($this->response, $this->connector);
}

return $this
Expand Down
5 changes: 4 additions & 1 deletion src/Traits/HasResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public function getResponse(): Response
return $this->response;
}

public function setResponse(Response $response): self
/**
* @return $this
*/
public function setResponse(Response $response)
{
$this->response = $response;

Expand Down
1 change: 1 addition & 0 deletions tests/Fake/ResourceResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public function it_throws_an_exception_when_calling_undefined_methods()
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Method undefinedMethod does not exist');

/** @phpstan-ignore-next-line */
$builder->undefinedMethod();
}

Expand Down
4 changes: 1 addition & 3 deletions tests/Resources/ResourceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public function embedded_collections_are_type_casted()
);

$this->assertInstanceOf(Payment::class, $payment);
/** @phpstan-ignore-next-line */
$this->assertInstanceOf(RefundCollection::class, $payment->_embedded->refunds);
$this->assertInstanceOf(RefundCollection::class, $payment->refunds());
}

/** @test */
Expand Down Expand Up @@ -121,7 +120,6 @@ public function embedded_resources_are_type_casted()
);

$this->assertInstanceOf(Client::class, $client);
/** @phpstan-ignore-next-line */
$this->assertInstanceOf(Onboarding::class, $client->_embedded->onboarding);
}

Expand Down

0 comments on commit 593a308

Please sign in to comment.