Skip to content

Commit 7a5d094

Browse files
authored
Merge pull request #107 from marc-mabe/array-to-str
fixed array to string conversion in EnumMap::offsetGet()
2 parents 0744517 + 267c364 commit 7a5d094

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

src/EnumMap.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ public function offsetExists($enumerator)
148148
public function offsetGet($enumerator)
149149
{
150150
$enumeration = $this->enumeration;
151-
$ord = $enumeration::get($enumerator)->getOrdinal();
151+
$enumerator = $enumeration::get($enumerator);
152+
$ord = $enumerator->getOrdinal();
152153
if (!isset($this->map[$ord]) && !array_key_exists($ord, $this->map)) {
153-
throw new UnexpectedValueException(\sprintf(
154-
"Enumerator '%s' could not be found",
155-
\is_object($enumerator) ? $enumerator->getValue() : $enumerator
154+
throw new UnexpectedValueException(sprintf(
155+
'Enumerator %s could not be found',
156+
\var_export($enumerator->getValue(), true)
156157
));
157158
}
158159

tests/MabeEnumTest/EnumMapTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public function testOffsetGetMissingKey()
107107
$enumMap->offsetGet(EnumBasic::ONE);
108108
}
109109

110+
public function testOffsetGetMissingArrayKey()
111+
{
112+
$enumMap = new EnumMap(EnumBasic::class);
113+
114+
$this->expectException(UnexpectedValueException::class);
115+
$enumMap->offsetGet(EnumBasic::ARR);
116+
}
117+
110118
public function testIterate()
111119
{
112120
$enumMap = new EnumMap(EnumBasic::class);

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public function testSetBinaryBitsetLeIntOutOfRangeBitsOfExtendedBytes1()
451451
$enumSet = new EnumSet(EnumBasic::class);
452452

453453
$this->expectException(InvalidArgumentException::class, 'Out-Of-Range');
454-
$enumSet->setBinaryBitsetLe("\x0A\xFF\x01");
454+
$enumSet->setBinaryBitsetLe("\x0A\xFF\x02");
455455
}
456456

457457
public function testSetBinaryBitsetLeIntOutOfRangeBitsOfExtendedBytes2()

tests/MabeEnumTest/EnumTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ public function testEnumInheritance()
9090
'EIGHT' => 8,
9191
'NINE' => 9,
9292
'ZERO' => 0,
93-
'FLOAT' => 0.123,
94-
'STR' => 'str',
95-
'STR_EMPTY' => '',
96-
'NIL' => null,
97-
'BOOLEAN_TRUE' => true,
98-
'BOOLEAN_FALSE' => false,
93+
'ARR' => EnumInheritance::ARR,
94+
'FLOAT' => EnumInheritance::FLOAT,
95+
'STR' => EnumInheritance::STR,
96+
'STR_EMPTY' => EnumInheritance::STR_EMPTY,
97+
'NIL' => EnumInheritance::NIL,
98+
'BOOLEAN_TRUE' => EnumInheritance::BOOLEAN_TRUE,
99+
'BOOLEAN_FALSE' => EnumInheritance::BOOLEAN_FALSE,
99100
'INHERITANCE' => 'Inheritance',
100101
), EnumInheritance::getConstants());
101102

@@ -105,7 +106,7 @@ public function testEnumInheritance()
105106

106107
$enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
107108
$this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue());
108-
$this->assertSame(16, $enum->getOrdinal());
109+
$this->assertSame(17, $enum->getOrdinal());
109110
}
110111

111112
public function testGetWithStrictValue()
@@ -279,15 +280,15 @@ public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
279280

280281
public function testInstantiateUsingOrdinalNumber()
281282
{
282-
$enum = EnumInheritance::byOrdinal(16);
283-
$this->assertSame(16, $enum->getOrdinal());
283+
$enum = EnumInheritance::byOrdinal(17);
284+
$this->assertSame(17, $enum->getOrdinal());
284285
$this->assertSame('INHERITANCE', $enum->getName());
285286
}
286287

287288
public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()
288289
{
289290
$this->expectException(InvalidArgumentException::class);
290-
EnumInheritance::byOrdinal(17);
291+
EnumInheritance::byOrdinal(18);
291292
}
292293

293294
public function testInstantiateByName()

tests/MabeEnumTest/TestAsset/EnumBasic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class EnumBasic extends Enum
4141
const NINE = 9;
4242
const ZERO = 0;
4343

44+
const ARR = [0, 0.1, 'str', null, true, false];
4445
const FLOAT = 0.123;
4546
const STR = 'str';
4647
const STR_EMPTY = '';

0 commit comments

Comments
 (0)