Skip to content

Commit f12b80a

Browse files
committed
Fix ability to create invalid enums. Fixes #9
1 parent e65fd09 commit f12b80a

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

src/Enum.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class Enum
4040
*/
4141
public function __construct($value)
4242
{
43-
if (!in_array($value, self::toArray())) {
43+
if (!$this->isValid($value)) {
4444
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
4545
}
4646

@@ -95,7 +95,6 @@ public static function toArray()
9595
$reflection = new \ReflectionClass($class);
9696
self::$cache[$class] = $reflection->getConstants();
9797
}
98-
9998
return self::$cache[$class];
10099
}
101100

@@ -107,7 +106,7 @@ public static function toArray()
107106
*/
108107
public static function isValid($value)
109108
{
110-
return in_array($value, self::toArray());
109+
return in_array($value, self::toArray(), true);
111110
}
112111

113112
/**
@@ -119,7 +118,7 @@ public static function isValid($value)
119118
*/
120119
public static function isValidKey($key)
121120
{
122-
return in_array($key, self::keys());
121+
return in_array($key, self::keys(), true);
123122
}
124123

125124
/**
@@ -131,7 +130,8 @@ public static function isValidKey($key)
131130
*/
132131
public static function search($value)
133132
{
134-
return array_search($value, array_combine(self::keys(), self::toArray()));
133+
// TODO: Replace combine with self::toArray()
134+
return array_search($value, array_combine(self::keys(), self::toArray()), true);
135135
}
136136

137137
/**

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

+40-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public function invalidValueProvider() {
5959
return [
6060
"string" => ['test'],
6161
"int" => [1234],
62-
"null" => [null],
6362
];
6463
}
6564

@@ -86,13 +85,17 @@ public function toStringProvider() {
8685
public function testKeys()
8786
{
8887
$values = EnumFixture::keys();
89-
$this->assertInternalType("array", $values);
9088
$expectedValues = array(
9189
"FOO",
9290
"BAR",
9391
"NUMBER",
92+
"PROBLEMATIC_NUMBER",
93+
"PROBLEMATIC_NULL",
94+
"PROBLEMATIC_EMPTY_STRING",
95+
"PROBLEMATIC_BOOLEAN_FALSE",
9496
);
95-
$this->assertEquals($expectedValues, $values);
97+
98+
$this->assertSame($expectedValues, $values);
9699
}
97100

98101
/**
@@ -101,13 +104,17 @@ public function testKeys()
101104
public function testToArray()
102105
{
103106
$values = EnumFixture::toArray();
104-
$this->assertInternalType("array", $values);
105107
$expectedValues = array(
106-
"FOO" => EnumFixture::FOO,
107-
"BAR" => EnumFixture::BAR,
108-
"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,
109115
);
110-
$this->assertEquals($expectedValues, $values);
116+
117+
$this->assertSame($expectedValues, $values);
111118
}
112119

113120
/**
@@ -132,11 +139,29 @@ public function testBadStaticAccess()
132139

133140
/**
134141
* isValid()
142+
* @dataProvider isValidProvider
135143
*/
136-
public function testIsValid()
144+
public function testIsValid($value, $isValid)
137145
{
138-
$this->assertTrue(EnumFixture::isValid('foo'));
139-
$this->assertFalse(EnumFixture::isValid('baz'));
146+
$this->assertSame($isValid, EnumFixture::isValid($value));
147+
}
148+
149+
public function isValidProvider() {
150+
return [
151+
/**
152+
* Valid values
153+
*/
154+
['foo', true],
155+
[42, true],
156+
[null, true],
157+
[0, true],
158+
['', true],
159+
[false, true],
160+
/**
161+
* Invalid values
162+
*/
163+
['baz', false]
164+
];
140165
}
141166

142167
/**
@@ -154,6 +179,9 @@ public function testIsValidKey()
154179
public function testSearch()
155180
{
156181
$this->assertEquals('FOO', EnumFixture::search('foo'));
157-
$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));
158186
}
159187
}

0 commit comments

Comments
 (0)