-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathRequestControllerTest.php
117 lines (93 loc) · 3.26 KB
/
RequestControllerTest.php
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
<?php
class RequestControllerTest extends TestCase
{
public function testPagination()
{
// Prevent throttling
$this->withoutMiddleware();
$number = 175;
$tokenId = $this->json('POST', 'token')->json()['uuid'];
for ($i = 0; $i < $number; $i++) {
$this->call('GET', $tokenId);
}
$requests = $this->json('GET', "token/$tokenId/requests?per_page=40&page=1");
$requests->assertJson([
'total' => $number,
'per_page' => 40,
'current_page' => 1,
'is_last_page' => false,
'from' => 1,
'to' => 40,
]);
$requests = $this->json('GET', "token/$tokenId/requests?per_page=40&page=2");
$requests->assertJson([
'current_page' => 2,
'is_last_page' => false,
'from' => 41,
'to' => 80,
]);
$requests = $this->json('GET', "token/$tokenId/requests?per_page=40&page=3");
$requests->assertJson([
'current_page' => 3,
'is_last_page' => false,
'from' => 81,
'to' => 120,
]);
$requests = $this->json('GET', "token/$tokenId/requests?per_page=40&page=5");
$requests->assertJson([
'current_page' => 5,
'is_last_page' => true,
'from' => 161,
'to' => 175,
]);
$requests = $this->json('GET', "token/$tokenId/requests?per_page=40&page=6");
$requests->assertExactJson([
'data' => [],
'total' => $number,
'per_page' => 40,
'current_page' => 6,
'is_last_page' => true,
'from' => 201,
'to' => 175,
]);
}
public function testSorting() {
// Prevent throttling
$this->withoutMiddleware();
$number = 175;
$tokenId = $this->json('POST', 'token')->json()['uuid'];
for ($i = 0; $i < $number; $i++) {
$this->call('GET', $tokenId);
}
// Test newest
$requests = $this->json('GET', "token/$tokenId/requests?sorting=newest");
$requests->assertJson([
'total' => $number,
'per_page' => 50,
'current_page' => 1,
'is_last_page' => false,
'from' => 1,
'to' => 50,
]);
$data = $requests->json()['data'];
$timestamps = array_column($data, 'created_at');
$sortedDescTimestamps = $timestamps;
rsort($sortedDescTimestamps);
$this->assertSame($sortedDescTimestamps, $timestamps, "The 'created_at' field is not sorted in descending order.");
// Test oldest
$requests = $this->json('GET', "token/$tokenId/requests?sorting=oldest");
$requests->assertJson([
'total' => $number,
'per_page' => 50,
'current_page' => 1,
'is_last_page' => false,
'from' => 1,
'to' => 50,
]);
$data = $requests->json()['data'];
$timestamps = array_column($data, 'created_at');
$sortedAscTimestamps = $timestamps;
sort($sortedAscTimestamps);
$this->assertSame($sortedAscTimestamps, $timestamps,'The "created_at" field is not sorted in ascending order.');
}
}