Skip to content

Commit

Permalink
test: Backed enum resource unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Apr 4, 2024
1 parent 53a45d7 commit d859ba1
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue6264/Availability.php
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);
}
}
145 changes: 145 additions & 0 deletions tests/Functional/BackedEnumResourceTest.php
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,
],
],
]);
}
}

0 comments on commit d859ba1

Please sign in to comment.