Skip to content

Commit cb53736

Browse files
committed
fixed bug in EnumMap::offsetGet() + more tests
1 parent ade08d0 commit cb53736

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/EnumMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function offsetGet($enumerator)
148148
{
149149
$enumeration = $this->enumeration;
150150
$ord = $enumeration::get($enumerator)->getOrdinal();
151-
if (!isset($this->map[$ord])) {
151+
if (!isset($this->map[$ord]) && !array_key_exists($ord, $this->map)) {
152152
throw new UnexpectedValueException(\sprintf(
153153
"Enumerator '%s' could not be found",
154154
\is_object($enumerator) ? $enumerator->getValue() : $enumerator
@@ -161,7 +161,7 @@ public function offsetGet($enumerator)
161161
/**
162162
* Attach a new enumerator or overwrite an existing one
163163
* @param Enum|null|boolean|int|float|string $enumerator
164-
* @param mixed $data
164+
* @param mixed $value
165165
* @return void
166166
* @throws InvalidArgumentException On an invalid given enumerator
167167
* @see attach()

tests/MabeEnumTest/EnumMapTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MabeEnumTest\TestAsset\EnumInheritance;
99
use OutOfBoundsException;
1010
use PHPUnit\Framework\TestCase;
11+
use UnexpectedValueException;
1112

1213
/**
1314
* Unit tests for the class MabeEnum\EnumMap
@@ -98,6 +99,14 @@ public function testBasicWithEnumeratorValues()
9899
$this->assertSame([], $enumMap->getValues());
99100
}
100101

102+
public function testOffsetGetMissingKey()
103+
{
104+
$enumMap = new EnumMap(EnumBasic::class);
105+
106+
$this->expectException(UnexpectedValueException::class);
107+
$enumMap->offsetGet(EnumBasic::ONE);
108+
}
109+
101110
public function testIterate()
102111
{
103112
$enumMap = new EnumMap(EnumBasic::class);
@@ -132,6 +141,7 @@ public function testIterate()
132141

133142
// go to the next element (out of range)
134143
$this->assertNull($enumMap->next());
144+
$this->assertNull($enumMap->current());
135145
$this->assertFalse($enumMap->valid());
136146
$this->assertSame(null, $enumMap->key());
137147

@@ -243,6 +253,8 @@ public function testNullValue()
243253
$enumMap[EnumBasic::ONE()] = null;
244254

245255
$this->assertSame(1, $enumMap->count());
256+
$this->assertNull($enumMap[EnumBasic::ONE]);
257+
$this->assertNull($enumMap->offsetGet(EnumBasic::ONE));
246258
$this->assertSame([EnumBasic::ONE()], $enumMap->getKeys());
247259

248260
$enumMap->rewind();

0 commit comments

Comments
 (0)