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

Commit 6a857fb

Browse files
author
Petr Knap
committed
Fixed getMembers method
1 parent 127eb12 commit 6a857fb

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/Enum/AbstractEnum.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ abstract class AbstractEnum
3939
*/
4040
protected function __construct($memberName)
4141
{
42-
$this->memberName = $memberName;
43-
$this->memberValue = $this->get($memberName);
42+
if(!($memberName === null && !$this->exists(null))) {
43+
$this->memberName = $memberName;
44+
$this->memberValue = $this->get($memberName);
45+
}
4446
}
4547

4648
/**
@@ -98,7 +100,15 @@ protected static function setMembers(array $members)
98100
*/
99101
public static function getMembers()
100102
{
101-
return self::$members[get_called_class()];
103+
$className = get_called_class();
104+
105+
$members = &self::$members[$className];
106+
107+
if(empty($members)) {
108+
new $className(null);
109+
}
110+
111+
return $members;
102112
}
103113

104114
/**

tests/Enum/EnumTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public function wrongKeyProvider()
1818
}
1919

2020
/**
21+
* @covers EnumMock::__callStatic
2122
* @dataProvider goodKeyProvider
23+
*
2224
* @param string $name
2325
* @param mixed $value
2426
*/
@@ -33,7 +35,9 @@ public function testMagicConstruction_GoodKey($name, $value)
3335
}
3436

3537
/**
38+
* @covers EnumMock::__callStatic
3639
* @dataProvider wrongKeyProvider
40+
*
3741
* @param string $name
3842
*/
3943
public function testMagicConstruction_WrongKey($name)
@@ -47,6 +51,9 @@ public function testMagicConstruction_WrongKey($name)
4751
EnumMock::$name();
4852
}
4953

54+
/**
55+
* @covers EnumMock::__callStatic
56+
*/
5057
public function testComparable()
5158
{
5259
$this->assertSame(EnumMock::A(), EnumMock::A());
@@ -57,16 +64,18 @@ public function testComparable()
5764
}
5865

5966
/**
60-
* @dataProvider goodKeyProvider
61-
* @param string $name
62-
* @param mixed $value
67+
* @covers EnumMock::getMembers
68+
* @runInSeparateProcess
6369
*/
64-
public function testGetConstants($name, $value)
70+
public function testGetMembers()
6571
{
66-
$constants = EnumMock::getMembers();
72+
$members = EnumMock::getMembers();
6773

68-
$this->assertInternalType("array", $constants);
69-
$this->assertArrayHasKey($name, $constants);
70-
$this->assertEquals($value, $constants[$name]);
74+
$this->assertInternalType("array", $members);
75+
$this->assertCount(2, $members);
76+
$this->assertArrayHasKey("A", $members);
77+
$this->assertEquals("a", $members["A"]);
78+
$this->assertArrayHasKey("B", $members);
79+
$this->assertEquals("b", $members["B"]);
7180
}
7281
}

0 commit comments

Comments
 (0)