-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Backed enum resource unit tests
- Loading branch information
1 parent
53a45d7
commit d859ba1
Showing
2 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6264; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Operation; | ||
use Symfony\Component\Serializer\Attribute\Groups; | ||
|
||
#[ApiResource(normalizationContext: ['groups' => ['get']])] | ||
#[GetCollection(provider: Availability::class.'::getCases')] | ||
#[Get(provider: Availability::class.'::getCase')] | ||
enum Availability: int | ||
{ | ||
case AVAILABLE = 10; | ||
case CANCELLED = 20; | ||
case POSTPONED = 30; | ||
|
||
public static function values(): array | ||
{ | ||
return array_map(static fn (Availability $feature) => $feature->value, self::cases()); | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
#[Groups(['get'])] | ||
public function getValue(): int | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public static function getCases(): array | ||
{ | ||
return self::cases(); | ||
} | ||
|
||
public static function getCase(Operation $operation, array $uriVariables): ?self | ||
{ | ||
return array_reduce(self::cases(), static fn ($c, Availability $case) => $case->name === $uriVariables['id'] ? $case : $c, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Functional; | ||
|
||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6264\Availability; | ||
|
||
final class BackedEnumResourceTest extends ApiTestCase | ||
{ | ||
public static function providerEnums(): iterable | ||
{ | ||
yield [Availability::AVAILABLE]; | ||
yield [Availability::CANCELLED]; | ||
yield [Availability::POSTPONED]; | ||
} | ||
|
||
/** @dataProvider providerEnums */ | ||
public function testItemJson(\BackedEnum $enum): void | ||
{ | ||
self::createClient()->request('GET', '/availabilities/'.$enum->name, ['headers' => ['Accept' => 'application/json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals(['value' => $enum->value]); | ||
} | ||
|
||
/** @dataProvider providerEnums */ | ||
public function testItemJsonHal(\BackedEnum $enum): void | ||
{ | ||
self::createClient()->request('GET', '/availabilities/'.$enum->name, ['headers' => ['Accept' => 'application/hal+json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals([ | ||
'_links' => [ | ||
'self' => [ | ||
'href' => '/availabilities/'.$enum->name, | ||
], | ||
], | ||
'value' => $enum->value, | ||
]); | ||
} | ||
|
||
/** @dataProvider providerEnums */ | ||
public function testItemJsonLd(\BackedEnum $enum): void | ||
{ | ||
self::createClient()->request('GET', '/availabilities/'.$enum->name, ['headers' => ['Accept' => 'application/ld+json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals('{"@context":"\/contexts\/Availability","@id":"\/availabilities\/'.$enum->name.'","@type":"Availability","value":'.$enum->value.'}'); | ||
} | ||
|
||
public function testCollectionJson(): void | ||
{ | ||
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals([ | ||
['value' => 10], | ||
['value' => 20], | ||
['value' => 30], | ||
]); | ||
} | ||
|
||
public function testCollectionHal(): void | ||
{ | ||
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/hal+json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals([ | ||
'_links' => [ | ||
'self' => [ | ||
'href' => '/availabilities', | ||
], | ||
'item' => [ | ||
['href' => '/availabilities/AVAILABLE'], | ||
['href' => '/availabilities/CANCELLED'], | ||
['href' => '/availabilities/POSTPONED'], | ||
], | ||
], | ||
'totalItems' => 3, | ||
'_embedded' => [ | ||
'item' => [ | ||
[ | ||
'_links' => [ | ||
'self' => ['href' => '/availabilities/AVAILABLE'], | ||
], | ||
'value' => 10, | ||
], | ||
[ | ||
'_links' => [ | ||
'self' => ['href' => '/availabilities/CANCELLED'], | ||
], | ||
'value' => 20, | ||
], | ||
[ | ||
'_links' => [ | ||
'self' => ['href' => '/availabilities/POSTPONED'], | ||
], | ||
'value' => 30, | ||
], | ||
], | ||
], | ||
]); | ||
} | ||
|
||
public function testCollectionJsonLd(): void | ||
{ | ||
self::createClient()->request('GET', '/availabilities', ['headers' => ['Accept' => 'application/ld+json']]); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertJsonEquals([ | ||
'@context' => '/contexts/Availability', | ||
'@id' => '/availabilities', | ||
'@type' => 'hydra:Collection', | ||
'hydra:totalItems' => 3, | ||
'hydra:member' => [ | ||
[ | ||
'@id' => '/availabilities/AVAILABLE', | ||
'@type' => 'Availability', | ||
'value' => 10, | ||
], | ||
[ | ||
'@id' => '/availabilities/CANCELLED', | ||
'@type' => 'Availability', | ||
'value' => 20, | ||
], | ||
[ | ||
'@id' => '/availabilities/POSTPONED', | ||
'@type' => 'Availability', | ||
'value' => 30, | ||
], | ||
], | ||
]); | ||
} | ||
} |