Skip to content

Commit 82d35ba

Browse files
committed
Merge pull request #12 from mirfilip/hotfix/ability-to-create-invalid-enums
Hotfix/ability to create invalid enums
2 parents 5fa55b5 + dae119a commit 82d35ba

File tree

3 files changed

+87
-40
lines changed

3 files changed

+87
-40
lines changed

src/Enum.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* Create an enum by implementing this class and adding class constants.
1313
*
1414
* @author Matthieu Napoli <[email protected]>
15-
* @author Daniel Costa <[email protected]
15+
* @author Daniel Costa <[email protected]>
16+
* @author Mirosław Filip <[email protected]>
1617
*/
1718
abstract class Enum
1819
{
@@ -22,7 +23,7 @@ abstract class Enum
2223
* @var mixed
2324
*/
2425
protected $value;
25-
26+
2627
/**
2728
* Store existing constants in a static cache per object.
2829
*
@@ -39,7 +40,7 @@ abstract class Enum
3940
*/
4041
public function __construct($value)
4142
{
42-
if (!in_array($value, self::toArray())) {
43+
if (!$this->isValid($value)) {
4344
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
4445
}
4546

@@ -106,7 +107,7 @@ public static function toArray()
106107
*/
107108
public static function isValid($value)
108109
{
109-
return in_array($value, self::toArray());
110+
return in_array($value, self::toArray(), true);
110111
}
111112

112113
/**
@@ -118,7 +119,7 @@ public static function isValid($value)
118119
*/
119120
public static function isValidKey($key)
120121
{
121-
return in_array($key, self::keys());
122+
return in_array($key, self::keys(), true);
122123
}
123124

124125
/**

tests/EnumFixture.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,25 @@
1515
* @method static EnumFixture BAR()
1616
* @method static EnumFixture NUMBER()
1717
*
18-
* @author Daniel Costa <[email protected]
18+
* @method static EnumFixture PROBLEMATIC_NUMBER()
19+
* @method static EnumFixture PROBLEMATIC_NULL()
20+
* @method static EnumFixture PROBLEMATIC_EMPTY_STRING()
21+
* @method static EnumFixture PROBLEMATIC_BOOLEAN_FALSE()
22+
*
23+
* @author Daniel Costa <[email protected]>
24+
* @author Mirosław Filip <[email protected]>
1925
*/
2026
class EnumFixture extends Enum
2127
{
2228
const FOO = "foo";
2329
const BAR = "bar";
2430
const NUMBER = 42;
31+
32+
/**
33+
* Values that are known to cause problems when used with soft typing
34+
*/
35+
const PROBLEMATIC_NUMBER = 0;
36+
const PROBLEMATIC_NULL = null;
37+
const PROBLEMATIC_EMPTY_STRING = '';
38+
const PROBLEMATIC_BOOLEAN_FALSE = false;
2539
}

tests/EnumTest.php

+66-34
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
/**
1010
* @author Matthieu Napoli <[email protected]>
11-
* @author Daniel Costa <[email protected]
11+
* @author Daniel Costa <[email protected]>
12+
* @author Mirosław Filip <[email protected]>
1213
*/
1314
class EnumTest extends \PHPUnit_Framework_TestCase
1415
{
@@ -38,42 +39,44 @@ public function testGetKey()
3839
}
3940

4041
/**
41-
* @expectedException \UnexpectedValueException
42+
* @dataProvider invalidValueProvider
4243
*/
43-
public function testInvalidValueString()
44+
public function testCreatingEnumWithInvalidValue($value)
4445
{
45-
new EnumFixture("test");
46-
}
46+
$this->setExpectedException(
47+
'\UnexpectedValueException',
48+
'Value \'' . $value . '\' is not part of the enum MyCLabs\Tests\Enum\EnumFixture'
49+
);
4750

48-
/**
49-
* @expectedException \UnexpectedValueException
50-
*/
51-
public function testInvalidValueInt()
52-
{
53-
new EnumFixture(1234);
51+
new EnumFixture($value);
5452
}
5553

5654
/**
57-
* @expectedException \UnexpectedValueException
55+
* Contains values not existing in EnumFixture
56+
* @return array
5857
*/
59-
public function testInvalidValueEmpty()
60-
{
61-
new EnumFixture(null);
58+
public function invalidValueProvider() {
59+
return array(
60+
"string" => array('test'),
61+
"int" => array(1234),
62+
);
6263
}
6364

6465
/**
6566
* __toString()
67+
* @dataProvider toStringProvider
6668
*/
67-
public function testToString()
69+
public function testToString($expected, $enumObject)
6870
{
69-
$value = new EnumFixture(EnumFixture::FOO);
70-
$this->assertEquals(EnumFixture::FOO, (string) $value);
71-
72-
$value = new EnumFixture(EnumFixture::BAR);
73-
$this->assertEquals(EnumFixture::BAR, (string) $value);
71+
$this->assertSame($expected, (string) $enumObject);
72+
}
7473

75-
$value = new EnumFixture(EnumFixture::NUMBER);
76-
$this->assertEquals((string) EnumFixture::NUMBER, (string) $value);
74+
public function toStringProvider() {
75+
return array(
76+
array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)),
77+
array(EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)),
78+
array((string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)),
79+
);
7780
}
7881

7982
/**
@@ -82,13 +85,17 @@ public function testToString()
8285
public function testKeys()
8386
{
8487
$values = EnumFixture::keys();
85-
$this->assertInternalType("array", $values);
8688
$expectedValues = array(
8789
"FOO",
8890
"BAR",
8991
"NUMBER",
92+
"PROBLEMATIC_NUMBER",
93+
"PROBLEMATIC_NULL",
94+
"PROBLEMATIC_EMPTY_STRING",
95+
"PROBLEMATIC_BOOLEAN_FALSE",
9096
);
91-
$this->assertEquals($expectedValues, $values);
97+
98+
$this->assertSame($expectedValues, $values);
9299
}
93100

94101
/**
@@ -97,13 +104,17 @@ public function testKeys()
97104
public function testToArray()
98105
{
99106
$values = EnumFixture::toArray();
100-
$this->assertInternalType("array", $values);
101107
$expectedValues = array(
102-
"FOO" => EnumFixture::FOO,
103-
"BAR" => EnumFixture::BAR,
104-
"NUMBER" => EnumFixture::NUMBER,
108+
"FOO" => EnumFixture::FOO,
109+
"BAR" => EnumFixture::BAR,
110+
"NUMBER" => EnumFixture::NUMBER,
111+
"PROBLEMATIC_NUMBER" => EnumFixture::PROBLEMATIC_NUMBER,
112+
"PROBLEMATIC_NULL" => EnumFixture::PROBLEMATIC_NULL,
113+
"PROBLEMATIC_EMPTY_STRING" => EnumFixture::PROBLEMATIC_EMPTY_STRING,
114+
"PROBLEMATIC_BOOLEAN_FALSE" => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE,
105115
);
106-
$this->assertEquals($expectedValues, $values);
116+
117+
$this->assertSame($expectedValues, $values);
107118
}
108119

109120
/**
@@ -128,11 +139,29 @@ public function testBadStaticAccess()
128139

129140
/**
130141
* isValid()
142+
* @dataProvider isValidProvider
131143
*/
132-
public function testIsValid()
144+
public function testIsValid($value, $isValid)
133145
{
134-
$this->assertTrue(EnumFixture::isValid('foo'));
135-
$this->assertFalse(EnumFixture::isValid('baz'));
146+
$this->assertSame($isValid, EnumFixture::isValid($value));
147+
}
148+
149+
public function isValidProvider() {
150+
return array(
151+
/**
152+
* Valid values
153+
*/
154+
array('foo', true),
155+
array(42, true),
156+
array(null, true),
157+
array(0, true),
158+
array('', true),
159+
array(false, true),
160+
/**
161+
* Invalid values
162+
*/
163+
array('baz', false)
164+
);
136165
}
137166

138167
/**
@@ -150,6 +179,9 @@ public function testIsValidKey()
150179
public function testSearch()
151180
{
152181
$this->assertEquals('FOO', EnumFixture::search('foo'));
153-
$this->assertNotEquals('FOO', EnumFixture::isValidKey('baz'));
182+
/**
183+
* @see https://github.com/myclabs/php-enum/issues/9
184+
*/
185+
$this->assertEquals(EnumFixture::PROBLEMATIC_NUMBER, EnumFixture::search(1));
154186
}
155187
}

0 commit comments

Comments
 (0)