From a173bdb8da6f032f00da870d343efcc4fd227020 Mon Sep 17 00:00:00 2001 From: igor-chepurnoi Date: Tue, 22 Nov 2016 17:23:53 +0200 Subject: [PATCH] fix code style, update tests --- helpers/BaseEnum.php | 56 +++++++++++++++++--------------------------- tests/EnumTest.php | 32 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/helpers/BaseEnum.php b/helpers/BaseEnum.php index 97e807f..aa20ab1 100755 --- a/helpers/BaseEnum.php +++ b/helpers/BaseEnum.php @@ -3,19 +3,22 @@ namespace yii2mod\enum\helpers; use ReflectionClass; +use UnexpectedValueException; use Yii; use yii\helpers\ArrayHelper; -use yii\web\BadRequestHttpException; /** * Class BaseEnum * - * @author Dmitry Semenov - * * @package yii2mod\enum\helpers */ abstract class BaseEnum { + /** + * @var string message category + */ + public static $messageCategory = 'app'; + /** * The cached list of constants by name. * @@ -30,50 +33,32 @@ abstract class BaseEnum */ private static $byValue = []; - /** - * The value managed by this type instance. - * - * @var mixed - */ - private $value; - /** * @var array list of properties */ private static $list; /** - * @var string message category + * The value managed by this type instance. + * + * @var mixed */ - public static $messageCategory = 'app'; + private $_value; /** * Sets the value that will be managed by this type instance. * * @param mixed $value The value to be managed * - * @throws BadRequestHttpException If the value is not valid + * @throws UnexpectedValueException If the value is not valid */ public function __construct($value) { if (!self::isValidValue($value)) { - throw new BadRequestHttpException(); + throw new UnexpectedValueException("Value '{$value}' is not part of the enum " . get_called_class()); } - $this->value = $value; - } - - /** - * Creates a new type instance for a called name. - * - * @param string $name The name of the value - * @param array $arguments An ignored list of arguments - * - * @return $this The new type instance - */ - public static function __callStatic($name, array $arguments = []) - { - return self::createByName($name); + $this->_value = $value; } /** @@ -81,7 +66,7 @@ public static function __callStatic($name, array $arguments = []) * * @param string $name The name of a value * - * @throws \yii\web\BadRequestHttpException + * @throws UnexpectedValueException * * @return $this The new type instance */ @@ -90,7 +75,7 @@ public static function createByName($name) $constants = self::getConstantsByName(); if (!array_key_exists($name, $constants)) { - throw new BadRequestHttpException(); + throw new UnexpectedValueException("Name '{$name}' is not exists in the enum constants list " . get_called_class()); } return new static($constants[$name]); @@ -115,7 +100,7 @@ public static function getValueByName($value) * * @param mixed $value The value * - * @throws \yii\web\BadRequestHttpException + * @throws UnexpectedValueException * * @return $this The new type instance */ @@ -124,7 +109,7 @@ public static function createByValue($value) $constants = self::getConstantsByValue(); if (!array_key_exists($value, $constants)) { - throw new BadRequestHttpException(); + throw new UnexpectedValueException("Value '{$value}' is not exists in the enum constants list " . get_called_class()); } return new static($value); @@ -140,10 +125,12 @@ public static function createByValue($value) public static function listData() { $class = get_called_class(); + if (!isset(self::$list[$class])) { $reflection = new ReflectionClass($class); self::$list[$class] = $reflection->getStaticPropertyValue('list'); } + $result = ArrayHelper::getColumn(self::$list[$class], function ($value) { return Yii::t(self::$messageCategory, $value); }); @@ -161,6 +148,7 @@ public static function listData() public static function getLabel($value) { $list = static::$list; + if (isset($list[$value])) { return Yii::t(static::$messageCategory, $list[$value]); } @@ -235,7 +223,7 @@ public function getName() { $constants = self::getConstantsByValue(); - return $constants[$this->value]; + return $constants[$this->_value]; } /** @@ -245,7 +233,7 @@ public function getName() */ public function getValue() { - return $this->value; + return $this->_value; } /** diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 0e777c2..887f275 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -27,4 +27,36 @@ public function testValidation() $this->assertTrue(BooleanEnum::isValidValue(1)); $this->assertFalse(BooleanEnum::isValidValue('YES')); } + + public function testCreateByName() + { + $enum = BooleanEnum::createByName('YES'); + + $this->assertEquals(BooleanEnum::YES, $enum->getValue()); + $this->assertTrue(array_key_exists($enum->getName(), BooleanEnum::getConstantsByName())); + } + + /** + * @expectedException \UnexpectedValueException + */ + public function testFailedCreateByName() + { + BooleanEnum::createByName('not existing name'); + } + + public function testCreateByValue() + { + $enum = BooleanEnum::createByValue(BooleanEnum::YES); + + $this->assertEquals(BooleanEnum::YES, $enum->getValue()); + $this->assertTrue(array_key_exists($enum->getName(), BooleanEnum::getConstantsByName())); + } + + /** + * @expectedException \UnexpectedValueException + */ + public function testFailedCreateByValue() + { + BooleanEnum::createByValue('not existing value'); + } }