Skip to content

Commit 646920a

Browse files
committed
Some indexes are out of range error added.
1 parent fc0f174 commit 646920a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Selectors/IndexListSelector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Smoren\ArrayView\Selectors;
66

7+
use Smoren\ArrayView\Exceptions\IndexError;
78
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
89
use Smoren\ArrayView\Interfaces\IndexListSelectorInterface;
910
use Smoren\ArrayView\Views\ArrayIndexListView;
@@ -42,6 +43,10 @@ public function __construct($value)
4243
*/
4344
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView
4445
{
46+
if (!$this->compatibleWith($source)) {
47+
throw new IndexError('Some indexes are out of range.');
48+
}
49+
4550
return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
4651
}
4752

tests/unit/ArrayView/ErrorsTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,33 @@ public function testNonSequentialError(callable $arrayGetter)
210210
ArrayView::toView($nonSequentialArray);
211211
}
212212

213+
/**
214+
* @dataProvider dataProviderForBadIndexList
215+
*/
216+
public function testReadBadIndexList(array $source, array $indexes)
217+
{
218+
$view = ArrayView::toView($source);
219+
$this->expectException(IndexError::class);
220+
$this->expectExceptionMessage('Some indexes are out of range.');
221+
$_ = $view[new IndexListSelector($indexes)];
222+
}
223+
224+
/**
225+
* @dataProvider dataProviderForBadIndexList
226+
*/
227+
public function testWriteBadIndexList(array $source, array $indexes)
228+
{
229+
$initialSource = [...$source];
230+
$view = ArrayView::toView($source);
231+
232+
try {
233+
$view[new IndexListSelector($indexes)] = $indexes;
234+
} catch (IndexError $e) {
235+
$this->assertSame($initialSource, [...$view]);
236+
$this->assertSame('Some indexes are out of range.', $e->getMessage());
237+
}
238+
}
239+
213240
public function dataProviderForOutOfRangeIndexes(): array
214241
{
215242
return [
@@ -379,6 +406,25 @@ public function dataProviderForUnsetError(): array
379406
];
380407
}
381408

409+
public function dataProviderForBadIndexList(): array
410+
{
411+
return [
412+
[[1], [0, 1]],
413+
[[1], [1, -1, -2]],
414+
[[1], [0, 1, 0, -1, -2]],
415+
[[1], [1, -1]],
416+
[[1], [0, 0, -2]],
417+
[[1, 2], [2]],
418+
[[1, 2], [1, 2]],
419+
[[1, 2], [0, 1, 2]],
420+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 5, -10]],
421+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [9, 5, 3, 1]],
422+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 10, 9, 7]],
423+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [-10, 1, 7, 10]],
424+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 50, 5, 3]],
425+
];
426+
}
427+
382428
public function dataProviderForNonSequentialError(): array
383429
{
384430
return [

0 commit comments

Comments
 (0)