Skip to content

Commit cb41845

Browse files
test: Backed enum resource unit tests
1 parent 53a45d7 commit cb41845

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-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: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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('{"@context":"\/contexts\/Availability","@id":"\/availabilities\/'.$enum->name.'","@type":"Availability","value":'.$enum->value.'}');
77+
}
78+
79+
public function testCollectionJson(): void
80+
{
81+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/json']]);
82+
83+
$this->assertResponseIsSuccessful();
84+
$this->assertJsonEquals([
85+
['value' => 10],
86+
['value' => 20],
87+
['value' => 30],
88+
]);
89+
}
90+
91+
public function testCollectionJsonApi(): void
92+
{
93+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/vnd.api+json']]);
94+
95+
$this->assertResponseIsSuccessful();
96+
$this->assertJsonEquals([
97+
'links' => [
98+
'self' => '/availabilities',
99+
],
100+
'meta' => [
101+
'totalItems' => 3,
102+
],
103+
'data' => [
104+
[
105+
'id' => '/availabilities/AVAILABLE',
106+
'type' => 'Availability',
107+
'attributes' => [
108+
'value' => 10,
109+
],
110+
],
111+
[
112+
'id' => '/availabilities/CANCELLED',
113+
'type' => 'Availability',
114+
'attributes' => [
115+
'value' => 20,
116+
],
117+
],
118+
[
119+
'id' => '/availabilities/POSTPONED',
120+
'type' => 'Availability',
121+
'attributes' => [
122+
'value' => 30,
123+
],
124+
],
125+
],
126+
]);
127+
}
128+
129+
public function testCollectionHal(): void
130+
{
131+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/hal+json']]);
132+
133+
$this->assertResponseIsSuccessful();
134+
$this->assertJsonEquals([
135+
'_links' => [
136+
'self' => [
137+
'href' => '/availabilities',
138+
],
139+
'item' => [
140+
['href' => '/availabilities/AVAILABLE'],
141+
['href' => '/availabilities/CANCELLED'],
142+
['href' => '/availabilities/POSTPONED'],
143+
],
144+
],
145+
'totalItems' => 3,
146+
'_embedded' => [
147+
'item' => [
148+
[
149+
'_links' => [
150+
'self' => ['href' => '/availabilities/AVAILABLE'],
151+
],
152+
'value' => 10,
153+
],
154+
[
155+
'_links' => [
156+
'self' => ['href' => '/availabilities/CANCELLED'],
157+
],
158+
'value' => 20,
159+
],
160+
[
161+
'_links' => [
162+
'self' => ['href' => '/availabilities/POSTPONED'],
163+
],
164+
'value' => 30,
165+
],
166+
],
167+
],
168+
]);
169+
}
170+
171+
public function testCollectionJsonLd(): void
172+
{
173+
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/ld+json']]);
174+
175+
$this->assertResponseIsSuccessful();
176+
$this->assertJsonEquals([
177+
'@context' => '/contexts/Availability',
178+
'@id' => '/availabilities',
179+
'@type' => 'hydra:Collection',
180+
'hydra:totalItems' => 3,
181+
'hydra:member' => [
182+
[
183+
'@id' => '/availabilities/AVAILABLE',
184+
'@type' => 'Availability',
185+
'value' => 10,
186+
],
187+
[
188+
'@id' => '/availabilities/CANCELLED',
189+
'@type' => 'Availability',
190+
'value' => 20,
191+
],
192+
[
193+
'@id' => '/availabilities/POSTPONED',
194+
'@type' => 'Availability',
195+
'value' => 30,
196+
],
197+
],
198+
]);
199+
}
200+
}

0 commit comments

Comments
 (0)