diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php new file mode 100644 index 00000000..2eec4c3c --- /dev/null +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -0,0 +1,112 @@ +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', + ], + ], + '_embedded' => (object) [ + 'orders' => [ + (object) ['id' => 'ord_stTC2WHAuS'], + ], + ], + ]); + + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [ + 'next' => (object) [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ] + ); + + $this->assertTrue($collection->hasNext()); + + $nextPage = $collection->next(); + + $this->assertEquals('ord_stTC2WHAuS', $nextPage[0]->id); + + $this->assertFalse($nextPage->hasNext()); + } + + public function testWillReturnNullIfNoNextResultIsAvailable() + { + $mockedClient = $this->createMock(MollieApiClient::class); + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [] + ); + + $this->assertFalse($collection->hasNext()); + $this->assertNull($collection->next()); + } + + 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', + ], + ], + '_embedded' => (object) [ + 'orders' => [ + (object) ['id' => 'ord_stTC2WHAuS'], + ], + ], + ]); + + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [ + 'previous' => (object) [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ] + ); + + $this->assertTrue($collection->hasPrevious()); + + $previousPage = $collection->previous(); + + $this->assertEquals('ord_stTC2WHAuS', $previousPage[0]->id); + + $this->assertFalse($previousPage->hasPrevious()); + } + + public function testWillReturnNullIfNoPreviousResultIsAvailable() + { + $mockedClient = $this->createMock(MollieApiClient::class); + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [] + ); + + $this->assertFalse($collection->hasPrevious()); + $this->assertNull($collection->previous()); + } +}