Skip to content

Commit

Permalink
add consecutive auto iteration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Nov 2, 2023
1 parent 94324f6 commit 412a9f4
Showing 1 changed file with 117 additions and 22 deletions.
139 changes: 117 additions & 22 deletions tests/Mollie/API/Resources/CursorCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Mollie\Api\Resources\LazyCollection;
use Mollie\Api\Resources\OrderCollection;
use PHPUnit\Framework\TestCase;
use stdClass;

class CursorCollectionTest extends TestCase
{
Expand All @@ -14,28 +15,28 @@ public function testCanGetNextCollectionResultWhenNextLinkIsAvailable()
$mockedClient = $this->createMock(MollieApiClient::class);
$mockedClient->expects($this->once())
->method('performHttpCallToFullUrl')
->willReturn((object) [
->willReturn($this->arrayToObject([
'count' => 1,
'_links' => (object) [
'_links' => [
'self' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
],
],
'_embedded' => (object) [
'_embedded' => [
'orders' => [
(object) ['id' => 'ord_stTC2WHAuS'],
['id' => 'ord_stTC2WHAuS'],
],
],
]);
]));

$collection = new OrderCollection(
$mockedClient,
1,
(object) [
'next' => (object) [
$this->arrayToObject([
'next' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
],
]
])
);

$this->assertTrue($collection->hasNext());
Expand Down Expand Up @@ -65,28 +66,30 @@ public function testCanGetPreviousCollectionResultWhenPreviousLinkIsAvailable()
$mockedClient = $this->createMock(MollieApiClient::class);
$mockedClient->expects($this->once())
->method('performHttpCallToFullUrl')
->willReturn((object) [
'count' => 1,
'_links' => (object) [
'self' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
->willReturn(
$this->arrayToObject([
'count' => 1,
'_links' => [
'self' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
],
],
],
'_embedded' => (object) [
'orders' => [
(object) ['id' => 'ord_stTC2WHAuS'],
'_embedded' => [
'orders' => [
['id' => 'ord_stTC2WHAuS'],
],
],
],
]);
])
);

$collection = new OrderCollection(
$mockedClient,
1,
(object) [
'previous' => (object) [
$this->arrayToObject([
'previous' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
],
]
])
);

$this->assertTrue($collection->hasPrevious());
Expand Down Expand Up @@ -121,4 +124,96 @@ public function testAutoPaginatorReturnsLazyCollection()

$this->assertInstanceOf(LazyCollection::class, $collection->getAutoIterator());
}

public function testAutoPaginatorCanHandleConsecutiveCalls()
{
$mockedClient = $this->createMock(MollieApiClient::class);
$mockedClient->expects($this->exactly(3))
->method('performHttpCallToFullUrl')
->willReturnOnConsecutiveCalls(
$this->arrayToObject([
'count' => 1,
'_links' => [
'self' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
],
'next' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
],
],
'_embedded' => [
'orders' => [
['id' => 'ord_stTC2WHAuS'],
],
],
]),
$this->arrayToObject([
'count' => 1,
'_links' => [
'self' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuF',
],
'next' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuF',
],
],
'_embedded' => [
'orders' => [
['id' => 'ord_stTC2WHAuF'],
],
],
]),
$this->arrayToObject([
'count' => 1,
'_links' => [
'self' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuB',
]
],
'_embedded' => [
'orders' => [
['id' => 'ord_stTC2WHAuB'],
],
],
])
);

$collection = new OrderCollection(
$mockedClient,
0,
$this->arrayToObject([
'next' => [
'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS',
],
])
);

$orderIds = [];
foreach ($collection->getAutoIterator() as $order) {
$orderIds[] = $order->id;
}

$this->assertEquals(['ord_stTC2WHAuS', 'ord_stTC2WHAuF', 'ord_stTC2WHAuB'], $orderIds);
}

/**
* Convert an array to an object recursively.
*
* @param mixed $data
* @return mixed
*/
private function arrayToObject($data)
{
if (!is_array($data)) {
return $data;
}

$obj = new stdClass();

foreach ($data as $key => $value) {
$obj->$key = $this->arrayToObject($value);
}

return $obj;
}
}

0 comments on commit 412a9f4

Please sign in to comment.