Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Refactored version
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Knap committed Sep 2, 2016
2 parents a421b93 + 29a8d9d commit 456442c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 90 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ isTrue(false); // returns null - WTF?
And now the **same code with Enum** instead of Constants:

```php
class MyBoolean extends \PetrKnap\Php\Enum\AbstractEnum
class MyBoolean extends \PetrKnap\Php\Enum\Enum
{
protected function members()
{
Expand Down Expand Up @@ -82,7 +82,7 @@ isTrue(false); // uncaught type error - OK

### Enum declaration
```php
class DayOfWeekEnum extends \PetrKnap\Php\Enum\AbstractEnum
class DayOfWeekEnum extends \PetrKnap\Php\Enum\Enum
{
protected function members()
{
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"phpunit/phpunit": "4.*"
},
"scripts": {
"test": [
"./vendor/bin/phpunit"
"tests/": [
"sudo docker run -v $(pwd):/app --rm php:5.3-cli bash -c 'cd /app && php ./vendor/bin/phpunit'"
],
"post-autoload-dump": [
"@test"
"@tests/"
]
},
"autoload": {
Expand Down
37 changes: 20 additions & 17 deletions src/Enum/AbstractEnum.php → src/Enum/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PetrKnap\Php\Enum;

use PetrKnap\Php\Enum\Exception\EnumNotFoundException;

/**
* Abstract enum object
*
Expand All @@ -10,7 +12,7 @@
* @package PetrKnap\Php\Enum
* @license https://github.com/petrknap/php-enum/blob/master/LICENSE MIT
*/
abstract class AbstractEnum
abstract class Enum implements EnumInterface
{
/**
* @var self[][]
Expand All @@ -34,7 +36,6 @@ abstract class AbstractEnum

/**
* @param string $memberName
* @throws EnumException
*/
protected function __construct($memberName)
{
Expand Down Expand Up @@ -77,19 +78,15 @@ public static function __callStatic($memberName, array $args)
}

/**
* Returns member name
*
* @return string
* @inheritdoc
*/
public function getName()
{
return $this->memberName;
}

/**
* Returns member value
*
* @return mixed
* @inheritdoc
*/
public function getValue()
{
Expand Down Expand Up @@ -137,18 +134,17 @@ private function exists($memberName)
/**
* @param string $memberName
* @return mixed
* @throws EnumException
* @throws EnumNotFoundException
*/
private function get($memberName)
{
if (!$this->exists($memberName)) {
throw new EnumException(
throw new EnumNotFoundException(
sprintf(
"%s does not exist in %s",
$memberName,
get_called_class()
),
EnumException::OUT_OF_RANGE
)
);
}

Expand All @@ -158,21 +154,28 @@ private function get($memberName)
/**
* @param mixed $value
* @return self
* @throws EnumException
* @throws EnumNotFoundException
*/
public static function findByValue($value)
public static function getEnumByValue($value)
{
foreach (self::getMembers() as $n => $v) {
if ($value === $v) {
return self::__callStatic($n, array());
}
}
throw new EnumException(
throw new EnumNotFoundException(
sprintf(
"Value not found in %s",
get_called_class()
),
EnumException::OUT_OF_RANGE
)
);
}

/**
* @return string
*/
public function __toString()
{
return sprintf("%s::%s", get_called_class(), $this->getName());
}
}
9 changes: 0 additions & 9 deletions src/Enum/EnumException.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/Enum/EnumInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PetrKnap\Php\Enum;

interface EnumInterface
{
/**
* Returns member name
*
* @return string
*/
public function getName();

/**
* Returns member value
*
* @return mixed
*/
public function getValue();
}
8 changes: 8 additions & 0 deletions src/Enum/Exception/EnumException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PetrKnap\Php\Enum\Exception;

abstract class EnumException extends \Exception
{
// Empty class
}
8 changes: 8 additions & 0 deletions src/Enum/Exception/EnumNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PetrKnap\Php\Enum\Exception;

class EnumNotFoundException extends EnumException
{
// Empty class
}
106 changes: 49 additions & 57 deletions tests/Enum/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,60 @@

namespace PetrKnap\Php\Enum\Test;

use PetrKnap\Php\Enum\EnumException;
use PetrKnap\Php\Enum\Enum;
use PetrKnap\Php\Enum\Exception\EnumException;
use PetrKnap\Php\Enum\Exception\EnumNotFoundException;
use PetrKnap\Php\Enum\Test\EnumTest\MyBoolean;

class EnumTest extends \PHPUnit_Framework_TestCase
{
public function goodKeyProvider()
/**
* @dataProvider dataCallStaticsWorks
* @param string $name
* @param mixed|EnumException $expectedValue
*/
public function testCallStaticsWorks($name, $expectedValue)
{
return array(array("MY_TRUE", 1), array("MY_FALSE", 2));
if ($expectedValue instanceof EnumException) {
$this->setExpectedException(get_class($expectedValue));
}

$this->assertSame($expectedValue, MyBoolean::__callStatic($name, array())->getValue());
}

public function wrongKeyProvider()
public function dataCallStaticsWorks()
{
return array(array("MY_NULL"), array("MY_VOID"));
return array(
array("MY_TRUE", 1),
array("MY_FALSE", 2),
array("MY_NULL", new EnumNotFoundException())
);
}

/**
* @covers EnumMock::__callStatic
* @dataProvider goodKeyProvider
*
* @param string $name
* @dataProvider dataGetEnumByValueWorks
* @param mixed $value
* @param Enum|EnumException $expectedEnum
*/
public function testMagicConstruction_GoodKey($name, $value)
public function testGetEnumByValueWorks($value, $expectedEnum)
{
/** @var MyBoolean $enum */
$enum = MyBoolean::$name();
if ($expectedEnum instanceof EnumException) {
$this->setExpectedException(get_class($expectedEnum));
}

$this->assertInstanceOf(MyBoolean::getClass(), $enum);
$this->assertSame($name, $enum->getName());
$this->assertSame($value, $enum->getValue());
$this->assertSame($expectedEnum, MyBoolean::getEnumByValue($value));
}

/**
* @covers EnumMock::__callStatic
* @dataProvider wrongKeyProvider
*
* @param string $name
*/
public function testMagicConstruction_WrongKey($name)
public function dataGetEnumByValueWorks()
{
$this->setExpectedException(
get_class(new EnumException()),
"",
EnumException::OUT_OF_RANGE
return array(
array(1, MyBoolean::MY_TRUE()),
array(2, MyBoolean::MY_FALSE()),
array(3, new EnumNotFoundException())
);

MyBoolean::$name();
}

/**
* @covers EnumMock::__callStatic
*/
public function testComparable()
public function testComparableWorks()
{
$this->assertSame(MyBoolean::MY_TRUE(), MyBoolean::MY_TRUE());
$this->assertNotSame(MyBoolean::MY_TRUE(), MyBoolean::MY_FALSE());
Expand All @@ -63,41 +64,32 @@ public function testComparable()
$this->assertFalse(MyBoolean::MY_TRUE() == MyBoolean::MY_FALSE());
}

/**
* @covers EnumMock::getMembers
* @runInSeparateProcess
*/
public function testGetMembers()
public function testGetMembersWorks()
{
$members = MyBoolean::getMembers();

$this->assertInternalType("array", $members);
$this->assertCount(2, $members);
$this->assertArrayHasKey("MY_TRUE", $members);
$this->assertEquals(1, $members["MY_TRUE"]);
$this->assertArrayHasKey("MY_FALSE", $members);
$this->assertEquals(2, $members["MY_FALSE"]);
$this->assertEquals(array(
"MY_TRUE" => 1,
"MY_FALSE" => 2
), MyBoolean::getMembers());
}

/**
* @dataProvider dataFindByValue
* @param mixed $value
* @param mixed $expected
* @dataProvider dataToStringWorks
* @param Enum $enum
* @param string $expectedString
*/
public function testFindByValue($value, $expected)
public function testToStringWorks(Enum $enum, $expectedString)
{
if ($expected instanceof \Exception) {
$this->setExpectedException(get_class($expected));
}
$this->assertSame($expected, MyBoolean::findByValue($value));
$this->assertSame($expectedString, $enum->__toString());
$this->assertSame($expectedString, (string) $enum);
$this->assertSame($expectedString, "{$enum}");
$this->assertSame($expectedString, $enum . "");
}

public function dataFindByValue()
public function dataToStringWorks()
{
return array(
array(1, MyBoolean::MY_TRUE()),
array(2, MyBoolean::MY_FALSE()),
array(3, new EnumException())
array(MyBoolean::MY_TRUE(), MyBoolean::getClass() . "::MY_TRUE"),
array(MyBoolean::MY_FALSE(), MyBoolean::getClass() . "::MY_FALSE")
);
}
}
4 changes: 2 additions & 2 deletions tests/Enum/EnumTest/MyBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace PetrKnap\Php\Enum\Test\EnumTest;

use PetrKnap\Php\Enum\AbstractEnum;
use PetrKnap\Php\Enum\Enum;

/**
* @method static MyBoolean MY_TRUE()
* @method static MyBoolean MY_FALSE()
*/
class MyBoolean extends AbstractEnum
class MyBoolean extends Enum
{
protected function members()
{
Expand Down

0 comments on commit 456442c

Please sign in to comment.