added pagination to tags

This commit is contained in:
Artem Bondarenko 2020-11-02 22:13:12 +02:00 committed by David Garcia
parent 1044a648f0
commit 6d9a7c42fd
4 changed files with 74 additions and 4 deletions

View File

@ -29,6 +29,8 @@ use Psr\Http\Message\ResponseInterface;
*/ */
class Tag extends HttpApi class Tag extends HttpApi
{ {
use Pagination;
/** /**
* Returns a list of tags. * Returns a list of tags.

View File

@ -40,6 +40,12 @@ final class IndexResponse implements ApiResponse, PagingProvider
$model = new self(); $model = new self();
$model->items = $items; $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']; $model->paging = $data['paging'];
return $model; return $model;

View File

@ -13,6 +13,11 @@ namespace Mailgun\Tests\Api;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Mailgun\Api\Tag; 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 <tobias.nyholm@gmail.com> * @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -26,17 +31,72 @@ class TagTest extends TestCase
public function testIndex() 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 = [ $data = [
'limit' => 10, 'limit' => 10,
]; ];
$api = $this->getApiMock(); $api = $this->getApiMock(null, null, new ModelHydrator());
$api->expects($this->once()) $api->expects($this->once())
->method('httpGet') ->method('httpGet')
->with('/v3/domain/tags', $data) ->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() public function testShow()

View File

@ -39,7 +39,7 @@ class IndexResponseTest extends BaseModelTest
"first": "first":
"https://url_to_first_page", "https://url_to_first_page",
"last": "last":
"https://url_to_last_page" "http://url_to_last_page"
} }
} }
JSON; JSON;
@ -49,5 +49,7 @@ JSON;
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
$tag = $tags[0]; $tag = $tags[0];
$this->assertEquals('red', $tag->getTag()); $this->assertEquals('red', $tag->getTag());
// Check if http url was changed to https
$this->assertEquals('https://url_to_last_page', $model->getLastUrl());
} }
} }