Skip to content

Commit 0569611

Browse files
committed
4268: Updated API tests
1 parent 56c129f commit 0569611

File tree

5 files changed

+150
-22
lines changed

5 files changed

+150
-22
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,20 @@ curl --silent --header "X-Api-Key: api_key_1" "http://$(docker compose port ngin
121121
task fixtures:load:test --yes
122122
task api:test
123123
```
124+
125+
You can pass additional arguments to filter tests, e.g.
126+
127+
``` shell
128+
task api:test -- --filter Event
129+
```
130+
131+
> [!TIP]
132+
> Use Task's [Dry run mode](https://taskfile.dev/usage/#dry-run-mode) (`task --dry`) to see the commands that are
133+
> actually run, e.g.
134+
>
135+
> ``` shell
136+
> $ task --dry api:test -- --filter Event
137+
> task: [compose] docker compose exec phpfpm bin/phpunit --filter Event
138+
> ```
139+
>
140+
> This is useful to tweak a (test) command without changing `Taskfile.yml`.

tests/ApiPlatform/AbstractApiTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ protected function get(array $query, ?string $path = null, bool $authenticated =
2828

2929
return $client->request('GET', $path.(str_contains($path, '?') ? '&' : '?').http_build_query($query));
3030
}
31+
32+
protected static function formatDateTime(string $datetime): string
33+
{
34+
return (new \DateTimeImmutable($datetime))->format(\DateTimeImmutable::ATOM);
35+
}
3136
}

tests/ApiPlatform/EventsFilterTest.php

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ class EventsFilterTest extends AbstractApiTestCase
1313
protected static string $requestPath = '/api/v2/events';
1414

1515
#[DataProvider('getEventsProvider')]
16-
public function testGetEvents(array $query, int $expectedCount): void
16+
public function testGetEvents(array $query, int $expectedCount, ?string $message = null): void
1717
{
18+
$message ??= '';
19+
1820
$response = $this->get($query);
1921

2022
$data = $response->toArray();
21-
$this->assertArrayHasKey('hydra:member', $data);
22-
$this->assertCount($expectedCount, $data['hydra:member']);
23+
$this->assertArrayHasKey('hydra:member', $data, $message);
24+
$this->assertCount($expectedCount, $data['hydra:member'], $message);
2325
}
2426

2527
public static function getEventsProvider(): iterable
@@ -29,6 +31,7 @@ public static function getEventsProvider(): iterable
2931
3,
3032
];
3133

34+
// Test BooleanFilter.
3235
yield [
3336
['publicAccess' => 'true'],
3437
2,
@@ -38,5 +41,89 @@ public static function getEventsProvider(): iterable
3841
['publicAccess' => 'false'],
3942
1,
4043
];
44+
45+
// Test DateRangeFilter.
46+
yield [
47+
['occurrences.start[between]' => implode('..', [
48+
(new \DateTimeImmutable('2001-01-01'))->format(\DateTimeImmutable::ATOM),
49+
(new \DateTimeImmutable('2100-01-01'))->format(\DateTimeImmutable::ATOM),
50+
])],
51+
3,
52+
'Events in 21st century',
53+
];
54+
55+
yield [
56+
['occurrences.start[between]' => implode('..', [
57+
(new \DateTimeImmutable('2001-01-01'))->format(\DateTimeImmutable::ATOM),
58+
(new \DateTimeImmutable('2100-01-01'))->format(\DateTimeImmutable::ATOM),
59+
])],
60+
3,
61+
'Events in 21st century',
62+
];
63+
64+
yield [
65+
['occurrences.start[between]' => static::formatDateTime('2026-01-01').'..'.static::formatDateTime('2026-12-31')],
66+
1,
67+
'Events in 2026',
68+
];
69+
70+
// Test IdFilter.
71+
yield [
72+
['organizer.entityId' => 9],
73+
2,
74+
];
75+
76+
yield [
77+
['organizer.entityId' => 11],
78+
1,
79+
];
80+
81+
// Test MatchFilter.
82+
yield [
83+
['location.name' => 'somewhere'],
84+
1,
85+
'An event somewhere',
86+
];
87+
88+
yield [
89+
['location.name' => 'Another place'],
90+
0,
91+
];
92+
93+
// Test TagFilter.
94+
yield [
95+
['tags' => 'itkdev'],
96+
2,
97+
'Events tagged with "itkdev"',
98+
];
99+
100+
yield [
101+
['tags' => 'itkdevelopment'],
102+
0,
103+
'Events tagged with "itkdevelopment"',
104+
];
105+
106+
// @todo Does tags filtering use the tag slug or name?
107+
// yield [
108+
// ['tags' => 'for børn'],
109+
// 0,
110+
// 'Events tagged with "for børn"',
111+
// ];
112+
//
113+
// yield [
114+
// ['tags' => 'for-boern'],
115+
// 1,
116+
// 'Events tagged with "for-boern"',
117+
// ];
118+
119+
// Combined filters.
120+
yield [
121+
[
122+
'occurrences.start[between]' => static::formatDateTime('2026-01-01').'..'.static::formatDateTime('2026-12-31'),
123+
'tags' => 'itkdev',
124+
],
125+
1,
126+
'Events in 2026 tagged with "itkdev"',
127+
];
41128
}
42129
}

tests/resources/events.json

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
"organizer": {
178178
"created": "2024-03-07T00:31:22+01:00",
179179
"updated": "2024-03-07T00:31:22+01:00",
180-
"entityId": 9,
180+
"entityId": 11,
181181
"name": "ITKDev",
182182
"email": "[email protected]",
183183
"url": "https:\/\/github.com\/itk-dev"
@@ -203,8 +203,8 @@
203203
"occurrences": [
204204
{
205205
"entityId": 12,
206-
"start": "2024-12-08T12:30:00+01:00",
207-
"end": "2024-12-08T14:30:00+01:00",
206+
"start": "2026-12-08T12:30:00+01:00",
207+
"end": "2026-12-08T14:30:00+01:00",
208208
"ticketPriceRange": "Free in December",
209209
"room": "M2-5",
210210
"status": null
@@ -213,8 +213,8 @@
213213
"dailyOccurrences": [
214214
{
215215
"entityId": 12,
216-
"start": "2024-12-08T12:30:00+01:00",
217-
"end": "2024-12-08T14:30:00+01:00",
216+
"start": "2026-12-08T12:30:00+01:00",
217+
"end": "2026-12-08T14:30:00+01:00",
218218
"ticketPriceRange": "Free in December",
219219
"room": "M2-5",
220220
"status": null
@@ -223,7 +223,8 @@
223223
"tags": [
224224
"aros",
225225
"theoceanraceaarhus",
226-
"ITKDev"
226+
"ITKDev",
227+
"For b\u00f8rn"
227228
],
228229
"imageUrls": {
229230
"small": "media\/cache\/resolve\/small\/438d0b1f\/00bae8ff00e64832de73422f17a605ba391edc05b2458a786d4e8a11243e2162.png",
@@ -233,22 +234,22 @@
233234
"created": "2024-03-07T00:31:22+01:00",
234235
"updated": "2024-03-07T00:31:22+01:00",
235236
"location": {
236-
"entityId": 4,
237-
"name": "ITK Development",
237+
"entityId": 5,
238+
"name": "Somewhere",
238239
"image": null,
239-
"url": "https:\/\/itk.aarhus.dk\/om-itk\/afdelinger\/development\/",
240+
"url": "https:\/\/example.com/somewhere",
240241
"telephone": null,
241242
"disabilityAccess": true,
242-
"mail": "[email protected]",
243-
"street": "Hack Kampmanns Plads 2",
244-
"suite": "2.2",
245-
"region": "Jylland",
246-
"city": "Aarhus",
247-
"country": "Danmark",
248-
"postalCode": "8000",
243+
"mail": "somewhere#example.com",
244+
"street": "",
245+
"suite": "",
246+
"region": "",
247+
"city": "",
248+
"country": "",
249+
"postalCode": "",
249250
"coordinates": [
250-
56.1507645,
251-
10.2112699
251+
0.0000000,
252+
0.0000000
252253
]
253254
}
254255
}]

tests/resources/locations.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,22 @@
1616
56.1507645,
1717
10.2112699
1818
]
19-
}]
19+
},{
20+
"entityId": 5,
21+
"name": "Somewhere",
22+
"image": null,
23+
"url": "https:\/\/example.com/somewhere",
24+
"telephone": null,
25+
"disabilityAccess": true,
26+
"mail": "somewhere#example.com",
27+
"street": "",
28+
"suite": "",
29+
"region": "",
30+
"city": "",
31+
"country": "",
32+
"postalCode": "",
33+
"coordinates": [
34+
0.0000000,
35+
0.0000000
36+
]
37+
}]

0 commit comments

Comments
 (0)