Skip to content

Commit

Permalink
bug #197 Fix #[EnumCase] inheritance to set & get extras (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x-dev branch.

Discussion
----------

Fix #[EnumCase] inheritance to set & get extras

- [x] Add test case

Commits
-------

552cf2d Fix framework.http_method_override deprec
7d0385c [CS] Update cs-fixer
8a5324a Fix #[EnumCase] inheritance to set & get extras
  • Loading branch information
ogizanagi committed Jul 21, 2022
2 parents 8df336a + 552cf2d commit a296673
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
10 changes: 0 additions & 10 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
->exclude('tests/Fixtures/Integration/Symfony/var')
->exclude('tests/Unit/Bridge/Doctrine/DBAL/Types/TypesDumperTest')
->exclude('tests/Unit/Bridge/Doctrine/ODM/Types/TypesDumperTest')
// Enum ignored for now since php-cs-fixer removes extra blank lines:
->notPath([
'tests/Fixtures/Enum/SuitWithAttributesMissingLabel.php',
'tests/Fixtures/Enum/Suit.php',
'tests/Fixtures/Enum/SuitWithAttributesMissingAttribute.php',
'tests/Fixtures/Enum/SuitWithAttributes.php',
'tests/Fixtures/Enum/SuitWithExtras.php',
'tests/Fixtures/Enum/UnitSuit.php',
'tests/Fixtures/Integration/Symfony/src/Enum/Suit.php',
])
;

return (new PhpCsFixer\Config())
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include .make/help.mk
include .make/text.mk

PHP_CS_FIXER_VERSION=v3.8.0
PHP_CS_FIXER_VERSION=v3.9.4

###########
# Install #
Expand Down
2 changes: 1 addition & 1 deletion src/EnumCaseAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static function enumCaseAttributes(): \SplObjectStorage
$attributes = new \SplObjectStorage();

foreach ((new \ReflectionEnum(static::class))->getCases() as $rCase) {
if (null === $rAttr = $rCase->getAttributes(EnumCase::class)[0] ?? null) {
if (null === $rAttr = $rCase->getAttributes(EnumCase::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ReadableEnumTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function readableForName(string $name): string
throw new NameException($name, static::class);
}

return ($map[$name])->getReadable();
return $map[$name]->getReadable();
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Fixtures/Enum/SuitWithCustomCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/

namespace Elao\Enum\Tests\Fixtures\Enum;

use Elao\Enum\Attribute\EnumCase;
use Elao\Enum\ExtrasTrait;

enum SuitWithCustomCase
{
use ExtrasTrait;

#[CustomCase(icon: 'fa-heart', color: 'red')]
case Hearts;

#[CustomCase(icon: 'fa-diamond', color: 'red')]
case Diamonds;

#[CustomCase(icon: 'fa-club', color: 'black')]
case Clubs;

#[CustomCase(icon: 'fa-spade', color: 'black')]
case Spades;
}

#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
class CustomCase extends EnumCase
{
public function __construct(string $icon, string $color)
{
parent::__construct(null, ['icon' => $icon, 'color' => $color]);
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/Integration/Symfony/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ framework:
storage_factory_id: 'session.storage.factory.mock_file'
test: ~
assets: false
http_method_override: false

doctrine:
dbal:
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/EnumExtrasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Elao\Enum\Tests\Unit;

use Elao\Enum\Tests\Fixtures\Enum\SuitWithCustomCase;
use Elao\Enum\Tests\Fixtures\Enum\SuitWithExtras;
use Elao\Enum\Tests\IterableAssertionsTrait;
use PHPUnit\Framework\TestCase;
Expand All @@ -31,6 +32,15 @@ public function testGetExtra(): void
self::assertSame('value', SuitWithExtras::Hearts->getExtra('only-for-hearts'));
}

public function testGetExtraWithCustomCase(): void
{
self::assertSame('black', SuitWithCustomCase::Clubs->getExtra('color'));
self::assertSame('fa-club', SuitWithCustomCase::Clubs->getExtra('icon'));

self::assertSame('red', SuitWithCustomCase::Diamonds->getExtra('color'));
self::assertSame('fa-diamond', SuitWithCustomCase::Diamonds->getExtra('icon'));
}

public function testGetExtraReturnsNullOnMissingKey(): void
{
self::assertNull(SuitWithExtras::Clubs->getExtra('missing-key'));
Expand All @@ -55,6 +65,16 @@ public function testExtrasCanBeIteratedWithEnumCaseAsKeys(): void
})(), SuitWithExtras::extras('color'));
}

public function testWithCustomCaseExtrasCanBeIteratedWithEnumCaseAsKeys(): void
{
self::assertIterablesMatch((static function () {
yield SuitWithCustomCase::Hearts => 'red';
yield SuitWithCustomCase::Diamonds => 'red';
yield SuitWithCustomCase::Clubs => 'black';
yield SuitWithCustomCase::Spades => 'black';
})(), SuitWithCustomCase::extras('color'));
}

public function testExtrasAreNullOnMissingKey(): void
{
self::assertIterablesMatch((static function () {
Expand Down

0 comments on commit a296673

Please sign in to comment.