Skip to content
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
6 changes: 3 additions & 3 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ final public function is($enumerator)
*
* @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
* @return static
* @throws InvalidArgumentException On an unknwon or invalid value
* @throws InvalidArgumentException On an unknown or invalid value
* @throws LogicException On ambiguous constant values
*/
final public static function get($enumerator)
Expand All @@ -184,7 +184,7 @@ final public static function get($enumerator)
*
* @param null|bool|int|float|string|array $value Enumerator value
* @return static
* @throws InvalidArgumentException On an unknwon or invalid value
* @throws InvalidArgumentException On an unknown or invalid value
* @throws LogicException On ambiguous constant values
*/
final public static function byValue($value)
Expand Down Expand Up @@ -423,6 +423,6 @@ private static function noAmbiguousValues($constants)
*/
final public static function __callStatic(string $method, array $args)
{
return self::byName($method);
return static::byName($method);
}
}
4 changes: 2 additions & 2 deletions src/EnumSerializableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait EnumSerializableTrait
{
/**
* The method will be defined by MabeEnum\Enum
* @return null|bool|int|float|string
* @return null|bool|int|float|string|array
*/
abstract public function getValue();

Expand All @@ -49,7 +49,7 @@ public function serialize(): string
public function unserialize($serialized): void
{
$value = \unserialize($serialized);
$constants = self::getConstants();
$constants = static::getConstants();
$name = \array_search($value, $constants, true);
if ($name === false) {
$message = \is_scalar($value)
Expand Down
13 changes: 13 additions & 0 deletions tests/MabeEnumTest/EnumSerializableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LogicException;
use MabeEnum\Enum;
use MabeEnumTest\TestAsset\ExtendedSerializableEnum;
use MabeEnumTest\TestAsset\SerializableEnum;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
Expand Down Expand Up @@ -65,6 +66,18 @@ public function testUnserializeThrowsLogicExceptionOnChangingValue()
$enum->unserialize(serialize(SerializableEnum::STR));
}

public function testInheritence()
{
$enum = ExtendedSerializableEnum::EXTENDED();

$serialized = serialize($enum);
$this->assertInternalType('string', $serialized);

$unserialized = unserialize($serialized);
$this->assertInstanceOf(ExtendedSerializableEnum::class, $unserialized);
$this->assertSame($enum->getValue(), $unserialized->getValue());
}

/**
* Clears all instantiated enumerations and detected constants of the given enumerator
* @param string $enumeration
Expand Down
12 changes: 12 additions & 0 deletions tests/MabeEnumTest/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,22 @@ public function testEnumInheritance()
), EnumInheritance::getConstants());

$enum = EnumInheritance::get(EnumInheritance::ONE);
$this->assertInstanceOf(EnumInheritance::class, $enum);
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
$this->assertSame(0, $enum->getOrdinal());

$enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
$this->assertInstanceOf(EnumInheritance::class, $enum);
$this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue());
$this->assertSame(17, $enum->getOrdinal());

$enum = EnumInheritance::ONE();
$this->assertInstanceOf(EnumInheritance::class, $enum);
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
$this->assertSame(0, $enum->getOrdinal());

$enum = EnumInheritance::INHERITANCE();
$this->assertInstanceOf(EnumInheritance::class, $enum);
$this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue());
$this->assertSame(17, $enum->getOrdinal());
}
Expand Down
15 changes: 15 additions & 0 deletions tests/MabeEnumTest/TestAsset/ExtendedSerializableEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MabeEnumTest\TestAsset;

/**
* Extended serializable enumeration
*
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
* @copyright Copyright (c) 2017 Marc Bennewitz
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
*/
class ExtendedSerializableEnum extends SerializableEnum
{
const EXTENDED = 'extended';
}