Skip to content

Commit 42072a3

Browse files
committed
Improve TypedMap::unserializeKey() and test coverage
1 parent f9f55ec commit 42072a3

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/TypedMap.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Typhoon\TypedMap;
66

7+
use function Typhoon\Formatter\format;
8+
79
/**
810
* @api
911
* @implements \ArrayAccess<Key, mixed>
@@ -147,9 +149,23 @@ public function __unserialize(array $data): void
147149

148150
private static function unserializeKey(string $serializedKey): Key
149151
{
150-
$key = unserialize($serializedKey);
151-
\assert($key instanceof Key);
152+
$key = @unserialize($serializedKey);
153+
154+
if ($key instanceof Key) {
155+
return $key;
156+
}
157+
158+
if ($key === false) {
159+
throw new \LogicException(\sprintf(
160+
'Failed to unserialize key `%s`',
161+
format($serializedKey)
162+
));
163+
}
152164

153-
return $key;
165+
throw new \LogicException(\sprintf(
166+
'Expected unserialized key to be an instance of `%s`, got: `%s`',
167+
Key::class,
168+
format($key),
169+
));
154170
}
155171
}

tests/OptionalKeys.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Typhoon\TypedMap;
66

77
/**
8-
* @implements OptionalKey<string>
8+
* @implements OptionalKey<mixed>
99
*/
1010
enum OptionalKeys implements OptionalKey
1111
{

tests/TypedMapTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function testWithMapReturnsNewMapWithMergedKeys(): void
6666
self::assertNotSame($map2, $merged);
6767
}
6868

69-
public function testItRemovesKeyViaWithout(): void
7069
public function testWithoutCalledWithNoKeysReturnsSameMap(): void
7170
{
7271
$map = TypedMap::one(Keys::A, 123);
@@ -76,6 +75,7 @@ public function testWithoutCalledWithNoKeysReturnsSameMap(): void
7675
self::assertSame($map, $newMap);
7776
}
7877

78+
public function testWithoutReturnsNewMapWithoutKeys(): void
7979
{
8080
$map = TypedMap::one(Keys::A, 123);
8181
$initialMapCopy = clone $map;
@@ -107,6 +107,18 @@ public function testMapCount(): void
107107
self::assertCount(1, $map2);
108108
}
109109

110+
public function testToPairs(): void
111+
{
112+
$map = TypedMap::one(Keys::A, 1)->with(Keys::B, 'a');
113+
114+
$pairs = $map->toPairs();
115+
116+
self::assertEquals(
117+
[new Pair(Keys::A, 1), new Pair(Keys::B, 'a')],
118+
$pairs,
119+
);
120+
}
121+
110122
public function testOffsetSetThrows(): void
111123
{
112124
$map = new TypedMap();
@@ -125,7 +137,7 @@ public function testOffsetUnsetThrows(): void
125137
unset($map[Keys::A]);
126138
}
127139

128-
public function testItDeserializesCorrectly(): void
140+
public function testItUnserializesCorrectly(): void
129141
{
130142
$map = TypedMap::one(Keys::A, 'a')->with(Keys::B, new \stdClass());
131143

@@ -143,4 +155,22 @@ public function testSerializedRepresentationDoesNotChange(): void
143155
serialize($map),
144156
);
145157
}
158+
159+
public function testUnserializeThrowsIfKeyIsNotUnserializable(): void
160+
{
161+
$map = new TypedMap();
162+
163+
$this->expectExceptionObject(new \LogicException("Failed to unserialize key `''`"));
164+
165+
$map->__unserialize(['' => 1]);
166+
}
167+
168+
public function testUnserializeChecksKey(): void
169+
{
170+
$map = new TypedMap();
171+
172+
$this->expectExceptionObject(new \LogicException("Expected unserialized key to be an instance of `Typhoon\\TypedMap\\Key`, got: `''`"));
173+
174+
$map->__unserialize([serialize('') => 1]);
175+
}
146176
}

0 commit comments

Comments
 (0)