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

Commit 456442c

Browse files
author
Petr Knap
committed
Refactored version
2 parents a421b93 + 29a8d9d commit 456442c

File tree

9 files changed

+112
-90
lines changed

9 files changed

+112
-90
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ isTrue(false); // returns null - WTF?
4848
And now the **same code with Enum** instead of Constants:
4949

5050
```php
51-
class MyBoolean extends \PetrKnap\Php\Enum\AbstractEnum
51+
class MyBoolean extends \PetrKnap\Php\Enum\Enum
5252
{
5353
protected function members()
5454
{
@@ -82,7 +82,7 @@ isTrue(false); // uncaught type error - OK
8282

8383
### Enum declaration
8484
```php
85-
class DayOfWeekEnum extends \PetrKnap\Php\Enum\AbstractEnum
85+
class DayOfWeekEnum extends \PetrKnap\Php\Enum\Enum
8686
{
8787
protected function members()
8888
{

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"phpunit/phpunit": "4.*"
2525
},
2626
"scripts": {
27-
"test": [
28-
"./vendor/bin/phpunit"
27+
"tests/": [
28+
"sudo docker run -v $(pwd):/app --rm php:5.3-cli bash -c 'cd /app && php ./vendor/bin/phpunit'"
2929
],
3030
"post-autoload-dump": [
31-
"@test"
31+
"@tests/"
3232
]
3333
},
3434
"autoload": {

src/Enum/AbstractEnum.php renamed to src/Enum/Enum.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PetrKnap\Php\Enum;
44

5+
use PetrKnap\Php\Enum\Exception\EnumNotFoundException;
6+
57
/**
68
* Abstract enum object
79
*
@@ -10,7 +12,7 @@
1012
* @package PetrKnap\Php\Enum
1113
* @license https://github.com/petrknap/php-enum/blob/master/LICENSE MIT
1214
*/
13-
abstract class AbstractEnum
15+
abstract class Enum implements EnumInterface
1416
{
1517
/**
1618
* @var self[][]
@@ -34,7 +36,6 @@ abstract class AbstractEnum
3436

3537
/**
3638
* @param string $memberName
37-
* @throws EnumException
3839
*/
3940
protected function __construct($memberName)
4041
{
@@ -77,19 +78,15 @@ public static function __callStatic($memberName, array $args)
7778
}
7879

7980
/**
80-
* Returns member name
81-
*
82-
* @return string
81+
* @inheritdoc
8382
*/
8483
public function getName()
8584
{
8685
return $this->memberName;
8786
}
8887

8988
/**
90-
* Returns member value
91-
*
92-
* @return mixed
89+
* @inheritdoc
9390
*/
9491
public function getValue()
9592
{
@@ -137,18 +134,17 @@ private function exists($memberName)
137134
/**
138135
* @param string $memberName
139136
* @return mixed
140-
* @throws EnumException
137+
* @throws EnumNotFoundException
141138
*/
142139
private function get($memberName)
143140
{
144141
if (!$this->exists($memberName)) {
145-
throw new EnumException(
142+
throw new EnumNotFoundException(
146143
sprintf(
147144
"%s does not exist in %s",
148145
$memberName,
149146
get_called_class()
150-
),
151-
EnumException::OUT_OF_RANGE
147+
)
152148
);
153149
}
154150

@@ -158,21 +154,28 @@ private function get($memberName)
158154
/**
159155
* @param mixed $value
160156
* @return self
161-
* @throws EnumException
157+
* @throws EnumNotFoundException
162158
*/
163-
public static function findByValue($value)
159+
public static function getEnumByValue($value)
164160
{
165161
foreach (self::getMembers() as $n => $v) {
166162
if ($value === $v) {
167163
return self::__callStatic($n, array());
168164
}
169165
}
170-
throw new EnumException(
166+
throw new EnumNotFoundException(
171167
sprintf(
172168
"Value not found in %s",
173169
get_called_class()
174-
),
175-
EnumException::OUT_OF_RANGE
170+
)
176171
);
177172
}
173+
174+
/**
175+
* @return string
176+
*/
177+
public function __toString()
178+
{
179+
return sprintf("%s::%s", get_called_class(), $this->getName());
180+
}
178181
}

src/Enum/EnumException.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Enum/EnumInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PetrKnap\Php\Enum;
4+
5+
interface EnumInterface
6+
{
7+
/**
8+
* Returns member name
9+
*
10+
* @return string
11+
*/
12+
public function getName();
13+
14+
/**
15+
* Returns member value
16+
*
17+
* @return mixed
18+
*/
19+
public function getValue();
20+
}

src/Enum/Exception/EnumException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PetrKnap\Php\Enum\Exception;
4+
5+
abstract class EnumException extends \Exception
6+
{
7+
// Empty class
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PetrKnap\Php\Enum\Exception;
4+
5+
class EnumNotFoundException extends EnumException
6+
{
7+
// Empty class
8+
}

tests/Enum/EnumTest.php

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,60 @@
22

33
namespace PetrKnap\Php\Enum\Test;
44

5-
use PetrKnap\Php\Enum\EnumException;
5+
use PetrKnap\Php\Enum\Enum;
6+
use PetrKnap\Php\Enum\Exception\EnumException;
7+
use PetrKnap\Php\Enum\Exception\EnumNotFoundException;
68
use PetrKnap\Php\Enum\Test\EnumTest\MyBoolean;
79

810
class EnumTest extends \PHPUnit_Framework_TestCase
911
{
10-
public function goodKeyProvider()
12+
/**
13+
* @dataProvider dataCallStaticsWorks
14+
* @param string $name
15+
* @param mixed|EnumException $expectedValue
16+
*/
17+
public function testCallStaticsWorks($name, $expectedValue)
1118
{
12-
return array(array("MY_TRUE", 1), array("MY_FALSE", 2));
19+
if ($expectedValue instanceof EnumException) {
20+
$this->setExpectedException(get_class($expectedValue));
21+
}
22+
23+
$this->assertSame($expectedValue, MyBoolean::__callStatic($name, array())->getValue());
1324
}
1425

15-
public function wrongKeyProvider()
26+
public function dataCallStaticsWorks()
1627
{
17-
return array(array("MY_NULL"), array("MY_VOID"));
28+
return array(
29+
array("MY_TRUE", 1),
30+
array("MY_FALSE", 2),
31+
array("MY_NULL", new EnumNotFoundException())
32+
);
1833
}
1934

2035
/**
21-
* @covers EnumMock::__callStatic
22-
* @dataProvider goodKeyProvider
23-
*
24-
* @param string $name
36+
* @dataProvider dataGetEnumByValueWorks
2537
* @param mixed $value
38+
* @param Enum|EnumException $expectedEnum
2639
*/
27-
public function testMagicConstruction_GoodKey($name, $value)
40+
public function testGetEnumByValueWorks($value, $expectedEnum)
2841
{
29-
/** @var MyBoolean $enum */
30-
$enum = MyBoolean::$name();
42+
if ($expectedEnum instanceof EnumException) {
43+
$this->setExpectedException(get_class($expectedEnum));
44+
}
3145

32-
$this->assertInstanceOf(MyBoolean::getClass(), $enum);
33-
$this->assertSame($name, $enum->getName());
34-
$this->assertSame($value, $enum->getValue());
46+
$this->assertSame($expectedEnum, MyBoolean::getEnumByValue($value));
3547
}
3648

37-
/**
38-
* @covers EnumMock::__callStatic
39-
* @dataProvider wrongKeyProvider
40-
*
41-
* @param string $name
42-
*/
43-
public function testMagicConstruction_WrongKey($name)
49+
public function dataGetEnumByValueWorks()
4450
{
45-
$this->setExpectedException(
46-
get_class(new EnumException()),
47-
"",
48-
EnumException::OUT_OF_RANGE
51+
return array(
52+
array(1, MyBoolean::MY_TRUE()),
53+
array(2, MyBoolean::MY_FALSE()),
54+
array(3, new EnumNotFoundException())
4955
);
50-
51-
MyBoolean::$name();
5256
}
5357

54-
/**
55-
* @covers EnumMock::__callStatic
56-
*/
57-
public function testComparable()
58+
public function testComparableWorks()
5859
{
5960
$this->assertSame(MyBoolean::MY_TRUE(), MyBoolean::MY_TRUE());
6061
$this->assertNotSame(MyBoolean::MY_TRUE(), MyBoolean::MY_FALSE());
@@ -63,41 +64,32 @@ public function testComparable()
6364
$this->assertFalse(MyBoolean::MY_TRUE() == MyBoolean::MY_FALSE());
6465
}
6566

66-
/**
67-
* @covers EnumMock::getMembers
68-
* @runInSeparateProcess
69-
*/
70-
public function testGetMembers()
67+
public function testGetMembersWorks()
7168
{
72-
$members = MyBoolean::getMembers();
73-
74-
$this->assertInternalType("array", $members);
75-
$this->assertCount(2, $members);
76-
$this->assertArrayHasKey("MY_TRUE", $members);
77-
$this->assertEquals(1, $members["MY_TRUE"]);
78-
$this->assertArrayHasKey("MY_FALSE", $members);
79-
$this->assertEquals(2, $members["MY_FALSE"]);
69+
$this->assertEquals(array(
70+
"MY_TRUE" => 1,
71+
"MY_FALSE" => 2
72+
), MyBoolean::getMembers());
8073
}
8174

8275
/**
83-
* @dataProvider dataFindByValue
84-
* @param mixed $value
85-
* @param mixed $expected
76+
* @dataProvider dataToStringWorks
77+
* @param Enum $enum
78+
* @param string $expectedString
8679
*/
87-
public function testFindByValue($value, $expected)
80+
public function testToStringWorks(Enum $enum, $expectedString)
8881
{
89-
if ($expected instanceof \Exception) {
90-
$this->setExpectedException(get_class($expected));
91-
}
92-
$this->assertSame($expected, MyBoolean::findByValue($value));
82+
$this->assertSame($expectedString, $enum->__toString());
83+
$this->assertSame($expectedString, (string) $enum);
84+
$this->assertSame($expectedString, "{$enum}");
85+
$this->assertSame($expectedString, $enum . "");
9386
}
9487

95-
public function dataFindByValue()
88+
public function dataToStringWorks()
9689
{
9790
return array(
98-
array(1, MyBoolean::MY_TRUE()),
99-
array(2, MyBoolean::MY_FALSE()),
100-
array(3, new EnumException())
91+
array(MyBoolean::MY_TRUE(), MyBoolean::getClass() . "::MY_TRUE"),
92+
array(MyBoolean::MY_FALSE(), MyBoolean::getClass() . "::MY_FALSE")
10193
);
10294
}
10395
}

tests/Enum/EnumTest/MyBoolean.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace PetrKnap\Php\Enum\Test\EnumTest;
44

5-
use PetrKnap\Php\Enum\AbstractEnum;
5+
use PetrKnap\Php\Enum\Enum;
66

77
/**
88
* @method static MyBoolean MY_TRUE()
99
* @method static MyBoolean MY_FALSE()
1010
*/
11-
class MyBoolean extends AbstractEnum
11+
class MyBoolean extends Enum
1212
{
1313
protected function members()
1414
{

0 commit comments

Comments
 (0)