From 6d9a7c42fd491131d118669342c27c48c82888d7 Mon Sep 17 00:00:00 2001 From: Artem Bondarenko Date: Mon, 2 Nov 2020 22:13:12 +0200 Subject: [PATCH] added pagination to tags --- src/Api/Tag.php | 2 + src/Model/Tag/IndexResponse.php | 6 +++ tests/Api/TagTest.php | 66 +++++++++++++++++++++++++-- tests/Model/Tag/IndexResponseTest.php | 4 +- 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/Api/Tag.php b/src/Api/Tag.php index a94a6e6..9f1c448 100644 --- a/src/Api/Tag.php +++ b/src/Api/Tag.php @@ -29,6 +29,8 @@ use Psr\Http\Message\ResponseInterface; */ class Tag extends HttpApi { + use Pagination; + /** * Returns a list of tags. diff --git a/src/Model/Tag/IndexResponse.php b/src/Model/Tag/IndexResponse.php index 31e75dd..2bc7cb8 100644 --- a/src/Model/Tag/IndexResponse.php +++ b/src/Model/Tag/IndexResponse.php @@ -40,6 +40,12 @@ final class IndexResponse implements ApiResponse, PagingProvider $model = new self(); $model->items = $items; + + // Fix http urls that is coming from server + $data['paging'] = array_map(function(string $url) { + return str_replace('http://', 'https://', $url); + }, $data['paging']); + $model->paging = $data['paging']; return $model; diff --git a/tests/Api/TagTest.php b/tests/Api/TagTest.php index e9f3582..9ee43f4 100644 --- a/tests/Api/TagTest.php +++ b/tests/Api/TagTest.php @@ -13,6 +13,11 @@ namespace Mailgun\Tests\Api; use GuzzleHttp\Psr7\Response; use Mailgun\Api\Tag; +use Mailgun\Hydrator\ModelHydrator; +use Mailgun\Model\PaginationResponse; +use Mailgun\Model\Stats\TotalResponse; +use Mailgun\Model\Stats\TotalResponseItem; +use Mailgun\Model\Tag\IndexResponse; /** * @author Tobias Nyholm @@ -26,17 +31,72 @@ class TagTest extends TestCase public function testIndex() { + $jsonResponse = '{ + "items": [ + { + "tag": "Tag1", + "description": "", + "first-seen": "2020-11-02T00:00:00Z", + "last-seen": "2020-11-02T19:14:07.747Z" + }, + { + "tag": "Tag10", + "description": "", + "first-seen": "2020-11-02T00:00:00Z", + "last-seen": "2020-11-02T19:12:06.408Z" + }, + { + "tag": "Tag11", + "description": "", + "first-seen": "2020-11-02T00:00:00Z", + "last-seen": "2020-11-02T19:12:06.105Z" + }, + { + "tag": "Tag2", + "description": "", + "first-seen": "2020-11-02T00:00:00Z", + "last-seen": "2020-11-02T19:14:09.111Z" + }, + { + "tag": "Tag3", + "description": "", + "first-seen": "2020-11-02T00:00:00Z", + "last-seen": "2020-11-02T19:14:08.772Z" + }, + { + "tag": "Tag9", + "description": "", + "first-seen": "2020-11-02T00:00:00Z", + "last-seen": "2020-11-02T19:12:06.214Z" + } + ], + "paging": { + "previous": "http:\/\/api.mailgun.net\/v3\/sandbox152fb160643f41f9b09b52f7b5e370ec.mailgun.org\/tags?limit=10&page=prev&tag=", + "first": "http:\/\/api.mailgun.net\/v3\/sandbox152fb160643f41f9b09b52f7b5e370ec.mailgun.org\/tags?limit=10&page=first&tag=", + "next": "http:\/\/api.mailgun.net\/v3\/sandbox152fb160643f41f9b09b52f7b5e370ec.mailgun.org\/tags?limit=10&page=next&tag=", + "last": "http:\/\/api.mailgun.net\/v3\/sandbox152fb160643f41f9b09b52f7b5e370ec.mailgun.org\/tags?limit=10&page=last&tag=" + } +}'; + $data = [ 'limit' => 10, ]; - $api = $this->getApiMock(); + $api = $this->getApiMock(null, null, new ModelHydrator()); $api->expects($this->once()) ->method('httpGet') ->with('/v3/domain/tags', $data) - ->willReturn(new Response()); + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], $jsonResponse)); - $api->index('domain', 10); + $tags = $api->index('domain', 10); + + $this->assertInstanceOf(IndexResponse::class, $tags); + $this->assertCount(6, $tags->getItems()); + $this->assertContainsOnlyInstancesOf(\Mailgun\Model\Tag\Tag::class, $tags->getItems()); + $this->assertTrue(method_exists($api, 'nextPage')); + $this->assertTrue(method_exists($api, 'previousPage')); + $this->assertTrue(method_exists($api, 'firstPage')); + $this->assertTrue(method_exists($api, 'lastPage')); } public function testShow() diff --git a/tests/Model/Tag/IndexResponseTest.php b/tests/Model/Tag/IndexResponseTest.php index 58313b2..f2efc28 100644 --- a/tests/Model/Tag/IndexResponseTest.php +++ b/tests/Model/Tag/IndexResponseTest.php @@ -39,7 +39,7 @@ class IndexResponseTest extends BaseModelTest "first": "https://url_to_first_page", "last": - "https://url_to_last_page" + "http://url_to_last_page" } } JSON; @@ -49,5 +49,7 @@ JSON; $this->assertCount(2, $tags); $tag = $tags[0]; $this->assertEquals('red', $tag->getTag()); + // Check if http url was changed to https + $this->assertEquals('https://url_to_last_page', $model->getLastUrl()); } }