Skip to content

Commit

Permalink
Refactor enum fixtures into separate namespace (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Jan 4, 2023
1 parent 3b13725 commit 09356f4
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 96 deletions.
8 changes: 4 additions & 4 deletions tests/Analysers/TokenScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ public function scanCases(): iterable

if (\PHP_VERSION_ID >= 80100) {
yield 'enum' => [
'PHP/StatusEnum.php',
'PHP/Enums/StatusEnum.php',
[
'OpenApi\\Tests\\Fixtures\\PHP\\StatusEnum' => [
'OpenApi\\Tests\\Fixtures\\PHP\\Enums\\StatusEnum' => [
'uses' => ['Schema' => 'OpenApi\\Attributes\\Schema'],
'interfaces' => [],
'enums' => [],
Expand All @@ -337,9 +337,9 @@ public function scanCases(): iterable
];

yield 'enum-backed' => [
'PHP/StatusEnumBacked.php',
'PHP/Enums/StatusEnumBacked.php',
[
'OpenApi\\Tests\\Fixtures\\PHP\\StatusEnumBacked' => [
'OpenApi\\Tests\\Fixtures\\PHP\\Enums\\StatusEnumBacked' => [
'uses' => ['Schema' => 'OpenApi\\Attributes\\Schema'],
'interfaces' => [],
'enums' => [],
Expand Down
108 changes: 108 additions & 0 deletions tests/Fixtures/PHP/Enums/ReferencesEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\PHP\Enums;

use OpenApi\Annotations as OA;
use OpenApi\Attributes\Items;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

#[Schema()]
class ReferencesEnum
{
#[Property(
title: 'statusEnum',
description: 'Status enum',
type: 'string',
enum: StatusEnum::class,
nullable: false
)]
public string $statusEnum;

#[Property(
title: 'statusEnumMixed',
description: 'Status enum mixed',
type: 'string',
enum: [StatusEnum::DRAFT, StatusEnum::ARCHIVED, 'OTHER'],
nullable: false
)]
public string $statusEnumMixed;

/**
* @OA\Property(title="statusEnumBacked",
* description="Status enum backed",
* type="int",
* enum="\OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumBacked",
* nullable="false"
* )
*/
public int $statusEnumBacked;

#[Property(
title: 'statusEnumBackedMixed',
description: 'Status enum backed mixed',
type: 'int',
enum: [StatusEnumBacked::DRAFT, StatusEnumBacked::ARCHIVED, 9],
nullable: false
)]
public int $statusEnumBackedMixed;

#[Property(
title: 'statusEnumIntegerBacked',
description: 'Status enum integer backed',
type: 'int',
enum: StatusEnumIntegerBacked::class,
nullable: true
)]
public ?int $statusEnumIntegerBacked;

/**
* @OA\Property(
* title="statusEnumStringBacked",
* description="Status enum string backed",
* type="string",
* enum="\OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumStringBacked",
* nullable="true"
* )
*/
public ?string $statusEnumStringBacked;

#[Property(
title: 'statusEnumStringBackedMixed',
description: 'Status enum string backed mixed',
type: 'string',
enum: [StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other'],
nullable: true
)]
public ?string $statusEnumStringBackedMixed;

/** @var list<string> StatusEnumStringBacked array */
#[Property(
title: 'statusEnums',
description: 'StatusEnumStringBacked array',
type: 'array',
items: new Items(
title: 'itemsStatusEnumStringBacked',
type: 'string',
enum: StatusEnumStringBacked::class
)
)]
public array $statusEnums;

/** @var list<string> StatusEnumStringBacked array */
#[Property(
title: 'statusEnumsMixed',
description: 'StatusEnumStringBacked array mixed',
type: 'array',
items: new Items(
title: 'itemsStatusEnumStringBackedMixed',
type: 'string',
enum: [StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other']
)
)]
public array $statusEnumsMixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\PHP;
namespace OpenApi\Tests\Fixtures\PHP\Enums;

use OpenApi\Attributes\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\PHP;
namespace OpenApi\Tests\Fixtures\PHP\Enums;

use OpenApi\Attributes\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\PHP;
namespace OpenApi\Tests\Fixtures\PHP\Enums;

use OpenApi\Attributes\Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\PHP;
namespace OpenApi\Tests\Fixtures\PHP\Enums;

use OpenApi\Attributes\Schema;

Expand Down
69 changes: 0 additions & 69 deletions tests/Fixtures/PHP/ReferencesEnum.php

This file was deleted.

38 changes: 19 additions & 19 deletions tests/Processors/ExpandEnumsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
use OpenApi\Attributes\Property as AttributesProperty;
use OpenApi\Generator;
use OpenApi\Processors\ExpandEnums;
use OpenApi\Tests\Fixtures\PHP\StatusEnum;
use OpenApi\Tests\Fixtures\PHP\StatusEnumBacked;
use OpenApi\Tests\Fixtures\PHP\StatusEnumIntegerBacked;
use OpenApi\Tests\Fixtures\PHP\StatusEnumStringBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnum;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumIntegerBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumStringBacked;
use OpenApi\Tests\OpenApiTestCase;

class ExpandEnumsTest extends OpenApiTestCase
Expand All @@ -31,7 +31,7 @@ public function setUp(): void

public function testExpandUnitEnum(): void
{
$analysis = $this->analysisFromFixtures(['PHP/StatusEnum.php']);
$analysis = $this->analysisFromFixtures(['PHP/Enums/StatusEnum.php']);
$analysis->process([new ExpandEnums()]);
$schema = $analysis->getSchemaForSource(StatusEnum::class);

Expand All @@ -40,7 +40,7 @@ public function testExpandUnitEnum(): void

public function testExpandBackedEnum(): void
{
$analysis = $this->analysisFromFixtures(['PHP/StatusEnumBacked.php']);
$analysis = $this->analysisFromFixtures(['PHP/Enums/StatusEnumBacked.php']);
$analysis->process([new ExpandEnums()]);
$schema = $analysis->getSchemaForSource(StatusEnumBacked::class);

Expand All @@ -49,7 +49,7 @@ public function testExpandBackedEnum(): void

public function testExpandBackedIntegerEnum(): void
{
$analysis = $this->analysisFromFixtures(['PHP/StatusEnumIntegerBacked.php']);
$analysis = $this->analysisFromFixtures(['PHP/Enums/StatusEnumIntegerBacked.php']);
$analysis->process([new ExpandEnums()]);
$schema = $analysis->getSchemaForSource(StatusEnumIntegerBacked::class);

Expand All @@ -58,7 +58,7 @@ public function testExpandBackedIntegerEnum(): void

public function testExpandBackedStringEnum(): void
{
$analysis = $this->analysisFromFixtures(['PHP/StatusEnumStringBacked.php']);
$analysis = $this->analysisFromFixtures(['PHP/Enums/StatusEnumStringBacked.php']);
$analysis->process([new ExpandEnums()]);
$schema = $analysis->getSchemaForSource(StatusEnumStringBacked::class);

Expand All @@ -80,57 +80,57 @@ public function expandEnumClassStringFixtures(): iterable

return [
'statusEnum' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnum',
$mapValues(StatusEnum::cases()),
],
'statusEnumMixed' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnumMixed',
$mapValues([StatusEnum::DRAFT, StatusEnum::ARCHIVED, 'OTHER']),
],
'statusEnumBacked' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnumBacked',
$mapValues(StatusEnumBacked::cases()),
],
'statusEnumBackedMixed' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnumBackedMixed',
$mapValues([StatusEnumBacked::DRAFT, StatusEnumBacked::ARCHIVED, 9]),
],
'statusEnumIntegerBacked' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnumIntegerBacked',
$mapValues(StatusEnumIntegerBacked::cases()),
],
'statusEnumStringBacked' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnumStringBacked',
$mapValues(StatusEnumStringBacked::cases()),
],
'statusEnumStringBackedMixed' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnumStringBackedMixed',
$mapValues([StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other']),
],
'statusEnums' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnums',
Generator::UNDEFINED,
],
'itemsStatusEnumStringBacked' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'itemsStatusEnumStringBacked',
$mapValues(StatusEnumStringBacked::cases()),
],
'statusEnumsMixed' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'statusEnumsMixed',
Generator::UNDEFINED,
],
'itemsStatusEnumStringBackedMixed' => [
['PHP/ReferencesEnum.php'],
['PHP/Enums/ReferencesEnum.php'],
'itemsStatusEnumStringBackedMixed',
$mapValues([StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other']),
],
Expand Down

0 comments on commit 09356f4

Please sign in to comment.