Skip to content

Solution for construct enum from inherited enum with incompatible value #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ abstract class Enum implements \JsonSerializable
public function __construct($value)
{
if ($value instanceof static) {
$this->value = $value->getValue();

return;
$value = $value->getValue();
}

if (!$this->isValid($value)) {
Expand Down
11 changes: 11 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,15 @@ public function testUnserialize()
$this->assertEquals(EnumFixture::FOO, $value->getValue());
$this->assertTrue(EnumFixture::FOO()->equals($value));
}

/**
* @see https://github.com/myclabs/php-enum/issues/95
*/
public function testEnumValuesInheritance()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Value 'value' is not part of the enum MyCLabs\Tests\Enum\EnumFixture");
$inheritedEnumFixture = InheritedEnumFixture::VALUE();
new EnumFixture($inheritedEnumFixture);
}
}
14 changes: 14 additions & 0 deletions tests/InheritedEnumFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace MyCLabs\Tests\Enum;

/**
* Class InheritedEnumFixture.
* @package MyCLabs\Tests\Enum
*
* @method static InheritedEnumFixture VALUE()
*/
class InheritedEnumFixture extends EnumFixture
{
const VALUE = 'value';
}