Repository navigation
Expand file tree
/
Copy pathPaginationIntegrationTest.php
More file actions
131 lines (111 loc) · 3.85 KB
/
Copy pathPaginationIntegrationTest.php
File metadata and controls
131 lines (111 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace ZammadAPIClient\Tests\Integration;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use ZammadAPIClient\Endpoints\Tickets\TicketDTO;
use ZammadAPIClient\Endpoints\Tickets\TicketRepository;
use ZammadAPIClient\ZammadClient;
#[Group('integration')]
final class PaginationIntegrationTest extends TestCase
{
use \ZammadAPIClient\Tests\Integration\Traits\CreatesZammadClient;
private static ZammadClient $client;
private static array $ticketIds = [];
/**
* Creates 10 test tickets for pagination testing.
*/
public static function setUpBeforeClass(): void
{
self::$client = self::createZammadClient();
for ($i = 0; $i < 10; $i++) {
self::$ticketIds[] = self::$client->repo(TicketRepository::class)->create(new TicketDTO(
title: 'Page Test ' . uniqid('', true),
group_id: 1,
priority_id: 2,
state_id: 1,
customer_id: 1,
))->id;
}
}
/**
* Verifies the lazy generator streams multiple pages.
*/
public function testAllPaginatesLazily(): void
{
$count = 0;
foreach (self::$client->repo(TicketRepository::class)->all() as $ticket) {
self::assertNotNull($ticket->id);
$count++;
if ($count >= 15) {
break;
}
}
self::assertGreaterThan(0, $count, 'Should stream tickets');
}
/**
* Verifies page navigation via PaginatedList.
*/
public function testPageNavigation(): void
{
$list = self::$client->repo(TicketRepository::class)->list(['expand' => 'true']);
$list->page(1);
$items = [];
foreach ($list as $ticket) {
$items[] = $ticket->id;
}
self::assertGreaterThan(0, count($items), 'First page should have tickets');
if (count($items) >= 100) {
$list->pageNext();
$nextPage = [];
foreach ($list as $ticket) {
$nextPage[] = $ticket->id;
}
self::assertGreaterThan(0, count($nextPage), 'Second page should have tickets');
}
}
/**
* Verifies total_count is null on index endpoints (Zammad API limitation)
* and non-null on search endpoints.
*/
public function testTotalCount(): void
{
$list = self::$client->repo(TicketRepository::class)->list();
$list->page(1);
self::assertNull(
$list->getTotalCount(),
'total_count is not available on index endpoints (Zammad requires full=true)',
);
$searchList = self::$client->repo(TicketRepository::class)->searchList('*');
$searchList->page(1);
self::assertNotNull(
$searchList->getTotalCount(),
'total_count should be returned by search endpoints with with_total_count=true',
);
self::assertGreaterThanOrEqual(
10,
$searchList->getTotalCount(),
'Should have at least the 10 tickets created in setUpBeforeClass',
);
$items = iterator_to_array($searchList);
self::assertNotEmpty($items, 'searchList should return items');
$total = $searchList->getTotalCount();
if ($total > 0) {
$maxPage = (int) ceil($total / 100);
for ($page = 2; $page <= $maxPage; $page++) {
$searchList->page($page);
$pageItems = iterator_to_array($searchList);
self::assertNotEmpty(
$pageItems,
"searchList page {$page}/{$maxPage} should return items (total={$total})",
);
}
}
}
/**
* Test tickets are cleaned up in setUp() / tearDown().
*/
public static function tearDownAfterClass(): void
{
}
}