Skip to content

Commit 157f7f1

Browse files
committed
New tests, bugs fixes.
1 parent d1e15dd commit 157f7f1

File tree

5 files changed

+118
-19
lines changed

5 files changed

+118
-19
lines changed

src/Exceptions/LengthError.php renamed to src/Exceptions/SizeError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Smoren\ArrayView\Exceptions;
44

5-
class LengthError extends \RuntimeException
5+
class SizeError extends \RuntimeException
66
{
77
}

src/Interfaces/ArrayViewInterface.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,40 @@ public function set($newValues): self;
7777
* @return bool
7878
*/
7979
public function isReadonly(): bool;
80+
81+
/**
82+
* @return int
83+
*/
84+
public function count(): int;
85+
86+
/**
87+
* @param numeric|string|ArraySelectorInterface $offset
88+
* @return bool
89+
*/
90+
public function offsetExists($offset): bool;
91+
92+
/**
93+
* @param numeric|string|ArraySelectorInterface $offset
94+
* @return T|array<T>
95+
*/
96+
#[\ReturnTypeWillChange]
97+
public function offsetGet($offset);
98+
99+
/**
100+
* @param numeric|string|ArraySelectorInterface $offset
101+
* @param T|array<T>|ArrayViewInterface<T> $value
102+
* @return void
103+
*/
104+
public function offsetSet($offset, $value): void;
105+
106+
/**
107+
* @param numeric|string|ArraySelectorInterface $offset
108+
* @return void
109+
*/
110+
public function offsetUnset($offset): void;
111+
112+
/**
113+
* @return \Generator<int, T>
114+
*/
115+
public function getIterator(): \Generator;
80116
}

src/Views/ArrayMaskView.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Smoren\ArrayView\Views;
44

5-
use Smoren\ArrayView\Exceptions\LengthError;
5+
use Smoren\ArrayView\Exceptions\SizeError;
66
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
77

88
/**
@@ -25,7 +25,7 @@ public function __construct(&$source, array $mask, ?bool $readonly = null)
2525
{
2626
[$sourceSize, $maskSize] = [\count($source), \count($mask)];
2727
if ($sourceSize !== $maskSize) {
28-
throw new LengthError("Mask length not equal to source length ({$maskSize} != {$maskSize}).");
28+
throw new SizeError("Mask size not equal to source length ({$maskSize} != {$sourceSize}).");
2929
}
3030

3131
$indexes = array_filter(

src/Views/ArrayView.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Smoren\ArrayView\Exceptions\IndexError;
88
use Smoren\ArrayView\Exceptions\KeyError;
9-
use Smoren\ArrayView\Exceptions\LengthError;
9+
use Smoren\ArrayView\Exceptions\SizeError;
1010
use Smoren\ArrayView\Exceptions\NotSupportedError;
1111
use Smoren\ArrayView\Exceptions\ReadonlyError;
1212
use Smoren\ArrayView\Exceptions\ValueError;
@@ -146,7 +146,7 @@ public function applyWith($data, callable $mapper): self
146146
{
147147
[$dataSize, $thisSize] = [\count($data), \count($this)];
148148
if ($dataSize !== $thisSize) {
149-
throw new LengthError("Length of values array not equal to view length ({$dataSize} != {$thisSize}).");
149+
throw new SizeError("Length of values array not equal to view length ({$dataSize} != {$thisSize}).");
150150
}
151151

152152
$dataView = ArrayView::toView($data);
@@ -178,21 +178,20 @@ public function set($newValues): self
178178

179179
[$dataSize, $thisSize] = [\count($newValues), \count($this)];
180180
if ($dataSize !== $thisSize) {
181-
throw new LengthError("Length of values array not equal to view length ({$dataSize} != {$thisSize}).");
181+
throw new SizeError("Length of values array not equal to view length ({$dataSize} != {$thisSize}).");
182182
}
183183

184184
$newValuesView = ArrayView::toView($newValues);
185185

186186
for ($i = 0; $i < \count($this); $i++) {
187-
// @phpstan-ignore-next-line
188187
$this[$i] = $newValuesView[$i];
189188
}
190189

191190
return $this;
192191
}
193192

194193
/**
195-
* @return \Generator<int, T>
194+
* {@inheritDoc}
196195
*/
197196
public function getIterator(): \Generator
198197
{
@@ -204,16 +203,15 @@ public function getIterator(): \Generator
204203
}
205204

206205
/**
207-
* @return bool
206+
* {@inheritDoc}
208207
*/
209208
public function isReadonly(): bool
210209
{
211210
return $this->readonly;
212211
}
213212

214213
/**
215-
* @param numeric|string|ArraySelectorInterface $offset
216-
* @return bool
214+
* {@inheritDoc}
217215
*/
218216
public function offsetExists($offset): bool
219217
{
@@ -233,8 +231,7 @@ public function offsetExists($offset): bool
233231
}
234232

235233
/**
236-
* @param numeric|string|ArraySelectorInterface $offset
237-
* @return T|array<T>
234+
* {@inheritDoc}
238235
*/
239236
#[\ReturnTypeWillChange]
240237
public function offsetGet($offset)
@@ -260,9 +257,7 @@ public function offsetGet($offset)
260257
}
261258

262259
/**
263-
* @param numeric|string|ArraySelectorInterface $offset
264-
* @param T|array<T>|ArrayViewInterface<T> $value
265-
* @return void
260+
* {@inheritDoc}
266261
*/
267262
public function offsetSet($offset, $value): void
268263
{
@@ -297,23 +292,26 @@ public function offsetSet($offset, $value): void
297292
}
298293

299294
/**
300-
* @param numeric|string|ArraySelectorInterface $offset
301-
* @return void
302295
* @throws NotSupportedError
296+
*
297+
* {@inheritDoc}
303298
*/
304299
public function offsetUnset($offset): void
305300
{
306301
throw new NotSupportedError();
307302
}
308303

309304
/**
310-
* @return int
305+
* {@inheritDoc}
311306
*/
312307
public function count(): int
313308
{
314309
return $this->getParentSize();
315310
}
316311

312+
/**
313+
* @return int
314+
*/
317315
protected function getParentSize(): int
318316
{
319317
return ($this->parentView !== null)

tests/unit/ArrayView/ErrorsTest.php

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

55
use Smoren\ArrayView\Exceptions\IndexError;
66
use Smoren\ArrayView\Exceptions\KeyError;
7+
use Smoren\ArrayView\Exceptions\SizeError;
78
use Smoren\ArrayView\Exceptions\ValueError;
9+
use Smoren\ArrayView\Selectors\MaskSelector;
810
use Smoren\ArrayView\Views\ArrayView;
911

1012
class ErrorsTest extends \Codeception\Test\Unit
@@ -77,6 +79,54 @@ public function testWriteKeyError(array $source, array $keys)
7779
}
7880
}
7981

82+
/**
83+
* @dataProvider dataProviderForBadSizeMask
84+
*/
85+
public function testReadByMaskSizeError(array $source, array $boolMask)
86+
{
87+
$view = ArrayView::toView($source);
88+
89+
$boolMaskSize = \count($boolMask);
90+
$sourceSize = \count($source);
91+
92+
$this->expectException(SizeError::class);
93+
$this->expectExceptionMessage("Mask size not equal to source length ({$boolMaskSize} != {$sourceSize}).");
94+
95+
$_ = $view[new MaskSelector($boolMask)];
96+
}
97+
98+
/**
99+
* @dataProvider dataProviderForBadSizeMask
100+
*/
101+
public function testGetSubviewByMaskSizeError(array $source, array $boolMask)
102+
{
103+
$view = ArrayView::toView($source);
104+
105+
$boolMaskSize = \count($boolMask);
106+
$sourceSize = \count($source);
107+
108+
$this->expectException(SizeError::class);
109+
$this->expectExceptionMessage("Mask size not equal to source length ({$boolMaskSize} != {$sourceSize}).");
110+
111+
$view->subview(new MaskSelector($boolMask));
112+
}
113+
114+
/**
115+
* @dataProvider dataProviderForBadSizeMask
116+
*/
117+
public function testWriteByMaskSizeError(array $source, array $boolMask)
118+
{
119+
$view = ArrayView::toView($source);
120+
121+
$boolMaskSize = \count($boolMask);
122+
$sourceSize = \count($source);
123+
124+
$this->expectException(SizeError::class);
125+
$this->expectExceptionMessage("Mask size not equal to source length ({$boolMaskSize} != {$sourceSize}).");
126+
127+
$view[new MaskSelector($boolMask)] = $boolMask;
128+
}
129+
80130
/**
81131
* @dataProvider dataProviderForNonSequentialError
82132
*/
@@ -112,6 +162,21 @@ public function dataProviderForBadKeys(): array
112162
];
113163
}
114164

165+
public function dataProviderForBadSizeMask(): array
166+
{
167+
return [
168+
[[], [1]],
169+
[[1], []],
170+
[[1], [1, 0]],
171+
[[1, 2, 3], [1]],
172+
[[1, 2, 3], [0]],
173+
[[1, 2, 3], [0, 1]],
174+
[[1, 2, 3], [0, 1, 1, 0]],
175+
[[1, 2, 3], [1, 1, 1, 1, 1]],
176+
[[1, 2, 3], [0, 0, 0, 0, 0]],
177+
];
178+
}
179+
115180
public function dataProviderForNonSequentialError(): array
116181
{
117182
return [

0 commit comments

Comments
 (0)