Skip to content

Commit ad6d402

Browse files
test: unit tests for backed enum resources
1 parent 76af4ef commit ad6d402

File tree

2 files changed

+261
-0
lines changed

2 files changed

+261
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6264;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\Operation;
20+
use Symfony\Component\Serializer\Attribute\Groups;
21+
22+
#[ApiResource(normalizationContext: ['groups' => ['get']])]
23+
#[GetCollection(provider: Availability::class.'::getCases')]
24+
#[Get(provider: Availability::class.'::getCase')]
25+
enum Availability: int
26+
{
27+
case AVAILABLE = 10;
28+
case CANCELLED = 20;
29+
case POSTPONED = 30;
30+
31+
public static function values(): array
32+
{
33+
return array_map(static fn (Availability $feature) => $feature->value, self::cases());
34+
}
35+
36+
public function getId(): string
37+
{
38+
return $this->name;
39+
}
40+
41+
#[Groups(['get'])]
42+
public function getValue(): int
43+
{
44+
return $this->value;
45+
}
46+
47+
public static function getCases(): array
48+
{
49+
return self::cases();
50+
}
51+
52+
public static function getCase(Operation $operation, array $uriVariables): ?self
53+
{
54+
return array_reduce(self::cases(), static fn ($c, Availability $case) => $case->name === $uriVariables['id'] ? $case : $c, null);
55+
}
56+
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Functional;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6264\Availability;
18+
19+
final class BackedEnumResourceTest extends ApiTestCase
20+
{
21+
public static function providerEnums(): iterable
22+
{
23+
yield [Availability::AVAILABLE];
24+
yield [Availability::CANCELLED];
25+
yield [Availability::POSTPONED];
26+
}
27+
28+
/** @dataProvider providerEnums */
29+
public function testItemJson(\BackedEnum $enum): void
30+
{
31+
self::createClient()->request('GET', '/availabilities/'.$enum->name, ['headers' => ['Accept' => 'application/json']]);
32+
33+
$this->assertResponseIsSuccessful();
34+
$this->assertJsonEquals(['value' => $enum->value]);
35+
}
36+
37+
/** @dataProvider providerEnums */
38+
public function testItemJsonApi(\BackedEnum $enum): void
39+
{
40+
self::createClient()->request('GET', '/availabilities/'.$enum->name, ['headers' => ['Accept' => 'application/vnd.api+json']]);
41+
42+
$this->assertResponseIsSuccessful();
43+
$this->assertJsonEquals([
44+
'data' => [
45+
'id' => '/availabilities/'.$enum->name,
46+
'type' => 'Availability',
47+
'attributes' => [
48+
'value' => $enum->value,
49+
],
50+
],
51+
]);
52+
}
53+
54+
/** @dataProvider providerEnums */
55+
public function testItemJsonHal(\BackedEnum $enum): void
56+
{
57+
self::createClient()->request('GET', '/availabilities/'.$enum->name, ['headers' => ['Accept' => 'application/hal+json']]);
58+
59+
$this->assertResponseIsSuccessful();
60+
$this->assertJsonEquals([
61+
'_links' => [
62+
'self' => [
63+
'href' => '/availabilities/'.$enum->name,
64+
],
65+
],
66+
'value' => $enum->value,
67+
]);
68+
}
69+
70+
/** @dataProvider providerEnums */
71+
public function testItemJsonLd(\BackedEnum $enum): void
72+
{
73+
self::createClient()->request('GET', '/availabilities/'.$enum->name, ['headers' => ['Accept' => 'application/ld+json']]);
74+
75+
$this->assertResponseIsSuccessful();
76+
$this->assertJsonEquals([
77+
'@context' => '/contexts/Availability',
78+
'@id' => '/availabilities/'.$enum->name,
79+
'@type' => 'Availability',
80+
'value' => $enum->value,
81+
]);
82+
}
83+
84+
public function testCollectionJson(): void
85+
{
86+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/json']]);
87+
88+
$this->assertResponseIsSuccessful();
89+
$this->assertJsonEquals([
90+
['value' => 10],
91+
['value' => 20],
92+
['value' => 30],
93+
]);
94+
}
95+
96+
public function testCollectionJsonApi(): void
97+
{
98+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/vnd.api+json']]);
99+
100+
$this->assertResponseIsSuccessful();
101+
$this->assertJsonEquals([
102+
'links' => [
103+
'self' => '/availabilities',
104+
],
105+
'meta' => [
106+
'totalItems' => 3,
107+
],
108+
'data' => [
109+
[
110+
'id' => '/availabilities/AVAILABLE',
111+
'type' => 'Availability',
112+
'attributes' => [
113+
'value' => 10,
114+
],
115+
],
116+
[
117+
'id' => '/availabilities/CANCELLED',
118+
'type' => 'Availability',
119+
'attributes' => [
120+
'value' => 20,
121+
],
122+
],
123+
[
124+
'id' => '/availabilities/POSTPONED',
125+
'type' => 'Availability',
126+
'attributes' => [
127+
'value' => 30,
128+
],
129+
],
130+
],
131+
]);
132+
}
133+
134+
public function testCollectionHal(): void
135+
{
136+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/hal+json']]);
137+
138+
$this->assertResponseIsSuccessful();
139+
$this->assertJsonEquals([
140+
'_links' => [
141+
'self' => [
142+
'href' => '/availabilities',
143+
],
144+
'item' => [
145+
['href' => '/availabilities/AVAILABLE'],
146+
['href' => '/availabilities/CANCELLED'],
147+
['href' => '/availabilities/POSTPONED'],
148+
],
149+
],
150+
'totalItems' => 3,
151+
'_embedded' => [
152+
'item' => [
153+
[
154+
'_links' => [
155+
'self' => ['href' => '/availabilities/AVAILABLE'],
156+
],
157+
'value' => 10,
158+
],
159+
[
160+
'_links' => [
161+
'self' => ['href' => '/availabilities/CANCELLED'],
162+
],
163+
'value' => 20,
164+
],
165+
[
166+
'_links' => [
167+
'self' => ['href' => '/availabilities/POSTPONED'],
168+
],
169+
'value' => 30,
170+
],
171+
],
172+
],
173+
]);
174+
}
175+
176+
public function testCollectionJsonLd(): void
177+
{
178+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/ld+json']]);
179+
180+
$this->assertResponseIsSuccessful();
181+
$this->assertJsonEquals([
182+
'@context' => '/contexts/Availability',
183+
'@id' => '/availabilities',
184+
'@type' => 'hydra:Collection',
185+
'hydra:totalItems' => 3,
186+
'hydra:member' => [
187+
[
188+
'@id' => '/availabilities/AVAILABLE',
189+
'@type' => 'Availability',
190+
'value' => 10,
191+
],
192+
[
193+
'@id' => '/availabilities/CANCELLED',
194+
'@type' => 'Availability',
195+
'value' => 20,
196+
],
197+
[
198+
'@id' => '/availabilities/POSTPONED',
199+
'@type' => 'Availability',
200+
'value' => 30,
201+
],
202+
],
203+
]);
204+
}
205+
}

0 commit comments

Comments
 (0)