1
|
|
|
<?php |
2
|
|
|
namespace ElevenLabs\Api\Service\Pagination\Provider; |
3
|
|
|
|
4
|
|
|
use ElevenLabs\Api\Definition\ResponseDefinition; |
5
|
|
|
use ElevenLabs\Api\Service\Pagination\Pagination; |
6
|
|
|
use ElevenLabs\Api\Service\Pagination\PaginationLinks; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
class PaginationHeaderTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** @test */ |
13
|
|
|
public function itShouldSupportPaginationHeader() |
14
|
|
|
{ |
15
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
16
|
|
|
$response->getHeaderLine('X-Page')->willReturn('1'); |
17
|
|
|
$response->getHeaderLine('X-Per-Page')->willReturn('10'); |
18
|
|
|
$response->getHeaderLine('X-Total-Items')->willReturn('100'); |
19
|
|
|
$response->getHeaderLine('X-Total-Pages')->willReturn('10'); |
20
|
|
|
|
21
|
|
|
$definition = $this->prophesize(ResponseDefinition::class); |
22
|
|
|
$provider = new PaginationHeader(); |
23
|
|
|
|
24
|
|
|
assertThat($provider->supportPagination([], $response->reveal(), $definition->reveal()), isTrue()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** @test */ |
28
|
|
|
public function itProvidePaginationUsingResponseHeaders() |
29
|
|
|
{ |
30
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
31
|
|
|
$response->hasHeader('Link')->willReturn(false); |
32
|
|
|
$response->getHeaderLine('X-Page')->willReturn('1'); |
33
|
|
|
$response->getHeaderLine('X-Per-Page')->willReturn('10'); |
34
|
|
|
$response->getHeaderLine('X-Total-Items')->willReturn('100'); |
35
|
|
|
$response->getHeaderLine('X-Total-Pages')->willReturn('10'); |
36
|
|
|
|
37
|
|
|
$data = []; |
38
|
|
|
|
39
|
|
|
$definition = $this->prophesize(ResponseDefinition::class); |
40
|
|
|
|
41
|
|
|
$provider = new PaginationHeader(); |
42
|
|
|
$pagination = $provider->getPagination($data, $response->reveal(), $definition->reveal()); |
43
|
|
|
|
44
|
|
|
assertThat($pagination, isInstanceOf(Pagination::class)); |
45
|
|
|
assertThat($pagination->getPage(), self::equalTo(1)); |
46
|
|
|
assertThat($pagination->getPerPage(), self::equalTo(10)); |
47
|
|
|
assertThat($pagination->getTotalItems(), self::equalTo(100)); |
48
|
|
|
assertThat($pagination->getTotalPages(), self::equalTo(10)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @test */ |
52
|
|
|
public function itAllowPaginationHeaderKeyOverride() |
53
|
|
|
{ |
54
|
|
|
$config = [ |
55
|
|
|
'page' => 'X-Pagination-Page', |
56
|
|
|
'perPage' => 'X-Pagination-Per-Page', |
57
|
|
|
'totalItems' => 'X-Pagination-Total-Items', |
58
|
|
|
'totalPages' => 'X-Pagination-Total-Pages', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$data = []; |
62
|
|
|
|
63
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
64
|
|
|
$response->hasHeader('Link')->willReturn(false); |
65
|
|
|
$response->getHeaderLine('X-Pagination-Page')->willReturn('1'); |
66
|
|
|
$response->getHeaderLine('X-Pagination-Per-Page')->willReturn('10'); |
67
|
|
|
$response->getHeaderLine('X-Pagination-Total-Items')->willReturn('100'); |
68
|
|
|
$response->getHeaderLine('X-Pagination-Total-Pages')->willReturn('10'); |
69
|
|
|
|
70
|
|
|
$definition = $this->prophesize(ResponseDefinition::class); |
71
|
|
|
|
72
|
|
|
$provider = new PaginationHeader($config); |
73
|
|
|
$pagination = $provider->getPagination($data, $response->reveal(), $definition->reveal()); |
74
|
|
|
|
75
|
|
|
assertThat($pagination, isInstanceOf(Pagination::class)); |
76
|
|
|
assertThat($pagination->getPage(), self::equalTo(1)); |
77
|
|
|
assertThat($pagination->getPerPage(), self::equalTo(10)); |
78
|
|
|
assertThat($pagination->getTotalItems(), self::equalTo(100)); |
79
|
|
|
assertThat($pagination->getTotalPages(), self::equalTo(10)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** @test */ |
83
|
|
|
public function itCanProvidePaginationLinks() |
84
|
|
|
{ |
85
|
|
|
$linkHeader = [ |
86
|
|
|
'<http://domain.tld?page=1>; rel="first"', |
87
|
|
|
'<http://domain.tld?page=10>; rel="last"', |
88
|
|
|
'<http://domain.tld?page=4>; rel="next"', |
89
|
|
|
'<http://domain.tld?page=2>; rel="prev"', |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
93
|
|
|
$response->getHeaderLine('X-Page')->willReturn('1'); |
94
|
|
|
$response->getHeaderLine('X-Per-Page')->willReturn('10'); |
95
|
|
|
$response->getHeaderLine('X-Total-Items')->willReturn('100'); |
96
|
|
|
$response->getHeaderLine('X-Total-Pages')->willReturn('10'); |
97
|
|
|
$response->hasHeader('Link')->willReturn(true); |
98
|
|
|
$response->getHeader('Link')->willReturn($linkHeader); |
99
|
|
|
|
100
|
|
|
$data = []; |
101
|
|
|
|
102
|
|
|
$definition = $this->prophesize(ResponseDefinition::class); |
103
|
|
|
|
104
|
|
|
$provider = new PaginationHeader(); |
105
|
|
|
$pagination = $provider->getPagination($data, $response->reveal(), $definition->reveal()); |
106
|
|
|
|
107
|
|
|
$paginationLinks = $pagination->getLinks(); |
108
|
|
|
|
109
|
|
|
assertThat($paginationLinks, isInstanceOf(PaginationLinks::class)); |
110
|
|
|
assertThat($paginationLinks->getFirst(), equalTo('http://domain.tld?page=1')); |
111
|
|
|
assertThat($paginationLinks->getLast(), equalTo('http://domain.tld?page=10')); |
112
|
|
|
assertThat($paginationLinks->getNext(), equalTo('http://domain.tld?page=4')); |
113
|
|
|
assertThat($paginationLinks->getPrev(), equalTo('http://domain.tld?page=2')); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|