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