Skip to content

Commit

Permalink
Create instance of enum from same enum in constructor
Browse files Browse the repository at this point in the history
It will prevent incorrect semantic exception message
  • Loading branch information
KartaviK committed Jan 30, 2019
1 parent 20e0424 commit 5147e69
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ abstract class Enum implements \JsonSerializable
*/
public function __construct($value)
{
if ($value instanceof static) {
$this->value = $value->getValue();

return;
}

if (!$this->isValid($value)) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class());
}
Expand Down
9 changes: 9 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ public function testBooleanEnum()
$this->assertFalse((new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))->jsonSerialize());
}

public function testConstructWithSameEnumArgument()
{
$enum = new EnumFixture(EnumFixture::FOO);

$enveloped = new EnumFixture($enum);

$this->assertEquals($enum, $enveloped);
}

private function assertJsonEqualsJson($json1, $json2)
{
$this->assertJsonStringEqualsJsonString($json1, $json2);
Expand Down

0 comments on commit 5147e69

Please sign in to comment.