Created
April 14, 2013 21:50
-
-
Save henriquemoody/5384381 to your computer and use it in GitHub Desktop.
Abstract enumerable implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class AbstractEnum | |
{ | |
/** | |
* @var string | |
*/ | |
private $name; | |
/** | |
* @var mixed | |
*/ | |
private $value; | |
/** | |
* @param mixed $value | |
* @throws InvalidArgumentException | |
*/ | |
public function __construct($value) | |
{ | |
$available = static::getAvailable(); | |
$name = array_search($value, $available); | |
if (false === $name) { | |
$message = sprintf('"%s" is not an allowed value (allowed: %s)', $value, implode(', ', $available)); | |
throw new \InvalidArgumentException($message); | |
} | |
$this->name = $name; | |
$this->value = $value; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getValue() | |
{ | |
return $this->value; | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return (string) $this->getValue(); | |
} | |
/** | |
* @return array | |
*/ | |
public static function getAvailable() | |
{ | |
$reflection = new \ReflectionClass(get_called_class()); | |
return $reflection->getConstants(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TestAbstractEnum extends AbstractEnum | |
{ | |
const ONE = 1; | |
const TWO = 3; | |
const THREE = 3; | |
const FOUR = 4; | |
} | |
class AbstractEnumTest extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @covers AbstractEnum::getAvailable | |
*/ | |
public function testShouldReturnAllAvailableValues() | |
{ | |
$expected = array( | |
'ONE' => TestAbstractEnum::ONE, | |
'TWO' => TestAbstractEnum::TWO, | |
'THREE' => TestAbstractEnum::THREE, | |
'FOUR' => TestAbstractEnum::FOUR, | |
); | |
$actual = TestAbstractEnum::getAvailable(); | |
$this->assertSame($expected, $actual); | |
} | |
/** | |
* @covers AbstractEnum::__construct | |
*/ | |
public function testShouldDefineNameAndValueOnConstructor() | |
{ | |
$enum = new TestAbstractEnum(TestAbstractEnum::ONE); | |
$this->assertAttributeSame('ONE', 'name', $enum); | |
$this->assertAttributeSame(TestAbstractEnum::ONE, 'value', $enum); | |
} | |
/** | |
* @covers AbstractEnum::__construct | |
* @expectedException InvalidArgumentException | |
* @expectedExceptionMessage "5" is not an allowed value | |
*/ | |
public function testShouldThrowsAnExceptionWhenValueIsNotValid() | |
{ | |
new TestAbstractEnum(5); | |
} | |
/** | |
* @covers AbstractEnum::getName | |
*/ | |
public function testShouldReturnTheDefinedValueName() | |
{ | |
$value = TestAbstractEnum::ONE; | |
$enum = new TestAbstractEnum($value); | |
$this->assertSame('ONE', $enum->getName()); | |
} | |
/** | |
* @covers AbstractEnum::getValue | |
*/ | |
public function testShouldReturnTheDefinedValue() | |
{ | |
$value = TestAbstractEnum::ONE; | |
$enum = new TestAbstractEnum($value); | |
$this->assertSame($value, $enum->getValue()); | |
} | |
/** | |
* @covers AbstractEnum::__toString | |
*/ | |
public function testShouldReturnValueWhenConvertingObjectToString() | |
{ | |
$value = TestAbstractEnum::ONE; | |
$enum = new TestAbstractEnum($value); | |
$this->assertSame((string) $value, (string) $enum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment