Skip to content

Commit e59863e

Browse files
committed
fixed method clear + tests, removed unused test enum
1 parent b1b2727 commit e59863e

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

src/MabeEnum/Enum.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,13 @@ static public function get($value)
144144
*/
145145
final static function clear()
146146
{
147-
$class = get_called_class();
147+
$class = get_called_class();
148+
$length = strlen($class);
148149

149150
// clear instantiated enums
150-
foreach (self::$enums as $id => $enum) {
151-
if (strcasecmp($class . '.', $id) === 0) {
152-
unset(self::$enums[$id]);
151+
foreach (self::$instances as $id => $enum) {
152+
if (strncasecmp($class . '.', $id, $length) === 0) {
153+
unset(self::$instances[$id]);
153154
}
154155
}
155156

@@ -181,10 +182,9 @@ final static public function getConstants()
181182
if (count($constants) > count(array_unique($constants))) {
182183
$ambiguous = array();
183184
foreach (array_count_values($constants) as $constValue => $countValue) {
184-
if ($countValue < 2) {
185-
continue;
185+
if ($countValue > 1) {
186+
$ambiguous[] = $constValue;
186187
}
187-
$ambiguous[] = $constValue;
188188
}
189189
throw new LogicException(sprintf(
190190
'All possible values needs to be unique. The following are ambiguous: %s',

tests/MabeEnumTest/EnumTest.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,13 @@ public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
6666

6767
public function testInstantiateUsingMagicMethod()
6868
{
69-
if (version_compare(PHP_VERSION, '5.3', '<')) {
70-
$this->markTestSkipped("Instantiating using magic method doesn't work for PHP < 5.3");
71-
}
72-
7369
$enum = MabeEnumTest_TestAsset_EnumInheritance::ONE();
7470
$this->assertInstanceOf('MabeEnumTest_TestAsset_EnumInheritance', $enum);
7571
$this->assertSame(MabeEnumTest_TestAsset_EnumInheritance::ONE, $enum->getValue());
7672
}
7773

7874
public function testInstantiateUsingMagicMethodThrowsBadMethodCallException()
7975
{
80-
if (version_compare(PHP_VERSION, '5.3', '<')) {
81-
$this->markTestSkipped("Instantiating using magic method doesn't work for PHP < 5.3");
82-
}
83-
8476
$this->setExpectedException('BadMethodCallException');
8577
MabeEnumTest_TestAsset_EnumInheritance::UNKNOWN();
8678
}
@@ -90,4 +82,22 @@ public function testAmbuguousConstantsThrowsLogicException()
9082
$this->setExpectedException('LogicException');
9183
MabeEnumTest_TestAsset_EnumAmbiguous::get(MabeEnumTest_TestAsset_EnumAmbiguous::AMBIGUOUS1);
9284
}
85+
86+
public function testSingleton()
87+
{
88+
$enum1 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::get(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
89+
$enum2 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
90+
$this->assertSame($enum1, $enum2);
91+
}
92+
93+
public function testClear()
94+
{
95+
$enum1 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
96+
MabeEnumTest_TestAsset_EnumWithoutDefaultValue::clear();
97+
$enum2 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
98+
$enum3 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE();
99+
100+
$this->assertNotSame($enum1, $enum2);
101+
$this->assertSame($enum2, $enum3);
102+
}
93103
}

tests/MabeEnumTest/TestAsset/EnumWithDefaultValue.php

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

0 commit comments

Comments
 (0)