Skip to content

Commit c740b01

Browse files
committed
More tests for errors.
1 parent 2523420 commit c740b01

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/Views/ArrayView.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ public function offsetGet($offset)
254254
return $this->subview($offset)->toArray();
255255
}
256256

257-
throw new KeyError("Invalid key: \"{$offset}\".");
257+
$strOffset = is_scalar($offset) ? strval($offset) : gettype($offset);
258+
throw new KeyError("Invalid key: \"{$strOffset}\".");
258259
}
259260

260261
/**
@@ -289,7 +290,8 @@ public function offsetSet($offset, $value): void
289290
return;
290291
}
291292

292-
throw new KeyError("Invalid key: \"{$offset}\".");
293+
$strOffset = is_scalar($offset) ? strval($offset) : gettype($offset);
294+
throw new KeyError("Invalid key: \"{$strOffset}\".");
293295
}
294296

295297
/**
@@ -332,6 +334,10 @@ protected function convertIndex(int $i): int
332334
*/
333335
private function numericOffsetExists($offset): bool
334336
{
337+
if (is_nan($offset) || is_infinite($offset)) {
338+
return false;
339+
}
340+
335341
try {
336342
$index = $this->convertIndex(intval($offset));
337343
} catch (IndexError $e) {

tests/unit/ArrayView/ErrorsTest.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Smoren\ArrayView\Tests\Unit\ArrayView;
44

55
use Smoren\ArrayView\Exceptions\IndexError;
6+
use Smoren\ArrayView\Exceptions\KeyError;
67
use Smoren\ArrayView\Exceptions\ValueError;
78
use Smoren\ArrayView\Views\ArrayView;
89

@@ -17,7 +18,8 @@ public function testReadIndexError(array $source, array $indexes)
1718
foreach ($indexes as $index) {
1819
try {
1920
$_ = $view[$index];
20-
$this->fail();
21+
$strIndex = strval($index);
22+
$this->fail("IndexError not thrown for key: \"{$strIndex}\"");
2123
} catch (IndexError $e) {
2224
$this->assertSame("Index {$index} is out of range.", $e->getMessage());
2325
}
@@ -33,13 +35,48 @@ public function testWriteIndexError(array $source, array $indexes)
3335
foreach ($indexes as $index) {
3436
try {
3537
$view[$index] = 1;
36-
$this->fail();
38+
$strIndex = strval($index);
39+
$this->fail("IndexError not thrown for key: \"{$strIndex}\"");
3740
} catch (IndexError $e) {
3841
$this->assertSame("Index {$index} is out of range.", $e->getMessage());
3942
}
4043
}
4144
}
4245

46+
/**
47+
* @dataProvider dataProviderForBadKeys
48+
*/
49+
public function testReadKeyError(array $source, array $keys)
50+
{
51+
$view = ArrayView::toView($source);
52+
foreach ($keys as $key) {
53+
$strKey = is_scalar($key) ? strval($key) : gettype($key);
54+
try {
55+
$_ = $view[$key];
56+
$this->fail("KeyError not thrown for key: \"{$strKey}\"");
57+
} catch (KeyError $e) {
58+
$this->assertSame("Invalid key: \"{$strKey}\".", $e->getMessage());
59+
}
60+
}
61+
}
62+
63+
/**
64+
* @dataProvider dataProviderForBadKeys
65+
*/
66+
public function testWriteKeyError(array $source, array $keys)
67+
{
68+
$view = ArrayView::toView($source);
69+
foreach ($keys as $key) {
70+
$strKey = is_scalar($key) ? strval($key) : gettype($key);
71+
try {
72+
$view[$key] = 1;
73+
$this->fail("KeyError not thrown for key: \"{$strKey}\"");
74+
} catch (KeyError $e) {
75+
$this->assertSame("Invalid key: \"{$strKey}\".", $e->getMessage());
76+
}
77+
}
78+
}
79+
4380
/**
4481
* @dataProvider dataProviderForNonSequentialError
4582
*/
@@ -53,9 +90,25 @@ public function testNonSequentialError(callable $arrayGetter)
5390
public function dataProviderForOutOfRangeIndexes(): array
5491
{
5592
return [
56-
[[], [-2, -1, 0, 1]],
57-
[[1], [-3, -2, 1, 2]],
58-
[[1, 2, 3], [-100, -5, 4, 100]],
93+
[[], [-2, -1, 0, 1, NAN, INF, -INF]],
94+
[[1], [-3, -2, 1, 2, NAN, INF, -INF]],
95+
[[1, 2, 3], [-100, -5, 4, 100, NAN, INF, -INF]],
96+
];
97+
}
98+
99+
public function dataProviderForBadKeys(): array
100+
{
101+
return [
102+
[[], ['a', 'b', 'c']],
103+
[[], ['1a', 'test', '!']],
104+
[[], [[], [1, 2, 3], ['a' => 'test']], new \stdClass([])],
105+
[[], [null, true, false, [], [1, 2, 3], ['a' => 'test']], new \stdClass([])],
106+
[[1], ['a', 'b', 'c']],
107+
[[1], ['1a', 'test', '!']],
108+
[[1], [null, true, false, [], [1, 2, 3], ['a' => 'test']], new \stdClass([])],
109+
[[1, 2, 3], ['a', 'b', 'c']],
110+
[[1, 2, 3], ['1a', 'test', '!']],
111+
[[2], [null, true, false, [], [1, 2, 3], ['a' => 'test']], new \stdClass([])],
59112
];
60113
}
61114

0 commit comments

Comments
 (0)