Skip to content

Commit

Permalink
test: resource with enum properties schema
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Apr 19, 2024
1 parent 0547aa7 commit 37672db
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/ResourceWithEnumProperty.php
@@ -0,0 +1,58 @@
<?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;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Tests\Fixtures\TestBundle\Enum\GenderTypeEnum;

#[ApiResource()]
#[Get(
provider: self::class.'::providerItem',
)]
#[GetCollection(
provider: self::class.'::providerCollection',
)]
class ResourceWithEnumProperty
{
public int $id = 1;

public ?BackedEnumIntegerResource $intEnum = null;

/** @var BackedEnumStringResource[] */
public array $stringEnum = [];

public ?GenderTypeEnum $gender = null;

/** @var GenderTypeEnum[] */
public array $genders = [];

public static function providerItem(Operation $operation, array $uriVariables): self
{
$self = new self();
$self->intEnum = BackedEnumIntegerResource::Yes;
$self->stringEnum = [BackedEnumIntegerResource::Maybe, BackedEnumIntegerResource::No];

Check failure on line 47 in tests/Fixtures/TestBundle/ApiResource/ResourceWithEnumProperty.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ResourceWithEnumProperty::$stringEnum (array<ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumStringResource>) does not accept array<int, ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumIntegerResource::Maybe|ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumIntegerResource::No>.
$self->gender = GenderTypeEnum::FEMALE;
$self->genders = [GenderTypeEnum::FEMALE, GenderTypeEnum::MALE];

return $self;
}

public static function providerCollection(Operation $operation, array $uriVariables): array
{
return [self::providerItem($operation, $uriVariables)];
}
}
45 changes: 45 additions & 0 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Expand Up @@ -287,4 +287,49 @@ public function testBackedEnumExamplesAreNotLost(): void
// openapiContext
$this->assertArrayNotHasKey('example', $properties['cardinal']);
}

public function testResourceWithEnumPropertiesSchema(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ResourceWithEnumProperty', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['ResourceWithEnumProperty.jsonld']['properties'];

$this->assertSame(
[
'type' => ['string', 'null'],
'format' => 'iri-reference',
'example' => 'https://example.com/',
],
$properties['intEnum']
);
$this->assertSame(
[
'type' => 'array',
'items' => [
'type' => 'string',
'format' => 'iri-reference',
'example' => 'https://example.com/',
],
],
$properties['stringEnum']
);
$this->assertSame(
[
'type' => ['string', 'null'],
'enum' => ['male', 'female', null],
],
$properties['gender']
);
$this->assertSame(
[
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => ['male', 'female'],
],
],
$properties['genders']
);
}
}

0 comments on commit 37672db

Please sign in to comment.