Skip to content

Commit f662219

Browse files
committed
deprecated Enum::clear()
This static method was only for testing and on the same time it broke singleton behavior is used somewhere else as in this library tests. This removed all unnecessary usagas of it and rewrote necessary usages with reflection. So this cind of internal method is no longer needed and can be removed.
1 parent a496c91 commit f662219

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/Enum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ final public static function getByOrdinal($ordinal)
239239
* NOTE: This can break singleton behavior ... use it with caution!
240240
*
241241
* @return void
242+
* @deprecated
242243
*/
243244
final public static function clear()
244245
{

tests/MabeEnumTest/EnumSerializableTraitTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MabeEnumTest\TestAsset\SerializableEnum;
66
use PHPUnit_Framework_TestCase as TestCase;
7+
use ReflectionClass;
78

89
/**
910
* Unit tests for the trait MabeEnum\EnumSerializableTrait
@@ -16,11 +17,9 @@ class EnumSerializableTraitTest extends TestCase
1617
{
1718
public function setUp()
1819
{
19-
if (version_compare(PHP_VERSION, '5.4', '<')) {
20+
if (PHP_VERSION_ID < 50400) {
2021
$this->markTestSkipped('Traits require PHP-5.4');
2122
}
22-
23-
SerializableEnum::clear();
2423
}
2524

2625
public function testSerializeSerializableEnum()
@@ -38,7 +37,7 @@ public function testUnserializeFirstWillHoldTheSameInstance()
3837
$this->assertInternalType('string', $serialized);
3938

4039
// clear all instantiated instances so we can virtual test unserializing first
41-
SerializableEnum::clear();
40+
$this->clearEnumeration('MabeEnumTest\TestAsset\SerializableEnum');
4241

4342
// First unserialize
4443
$unserialized = unserialize($serialized);
@@ -69,4 +68,23 @@ public function testUnserializeThrowsLogicExceptionOnChangingValue()
6968
$enum = SerializableEnum::get(SerializableEnum::INT);
7069
$enum->unserialize(serialize(SerializableEnum::STR));
7170
}
71+
72+
/**
73+
* Clears all instantiated enumerations and detected constants of the given enumerator
74+
* @param string $enumeration
75+
*/
76+
private function clearEnumeration($enumeration)
77+
{
78+
$reflClass = new ReflectionClass($enumeration);
79+
while ($reflClass->getName() !== 'MabeEnum\Enum') {
80+
$reflClass = $reflClass->getParentClass();
81+
}
82+
83+
$reflPropInstances = $reflClass->getProperty('instances');
84+
$reflPropInstances->setAccessible(true);
85+
$reflPropInstances->setValue(null, array());
86+
$reflPropConstants = $reflClass->getProperty('constants');
87+
$reflPropConstants->setAccessible(true);
88+
$reflPropConstants->setValue(null, array());
89+
}
7290
}

tests/MabeEnumTest/EnumTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
*/
2121
class EnumTest extends TestCase
2222
{
23-
public function setUp()
24-
{
25-
EnumBasic::clear();
26-
EnumInheritance::clear();
27-
}
28-
2923
public function testGetNameReturnsConstantNameOfCurrentValue()
3024
{
3125
$enum = EnumBasic::get(EnumBasic::ONE);

0 commit comments

Comments
 (0)