From 412a9f4c298e98119d3ab9777ce0ebf8027dc570 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Thu, 2 Nov 2023 11:26:59 +0100 Subject: [PATCH] add consecutive auto iteration test --- .../API/Resources/CursorCollectionTest.php | 139 +++++++++++++++--- 1 file changed, 117 insertions(+), 22 deletions(-) diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php index 8c54e973..b55c9175 100644 --- a/tests/Mollie/API/Resources/CursorCollectionTest.php +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -6,6 +6,7 @@ use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\OrderCollection; use PHPUnit\Framework\TestCase; +use stdClass; class CursorCollectionTest extends TestCase { @@ -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()); @@ -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()); @@ -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; + } }