From 157f7f11580f95b3aef9f640a11aeaf04be0ee0f Mon Sep 17 00:00:00 2001 From: Smoren Date: Mon, 11 Mar 2024 08:41:46 +0300 Subject: [PATCH] New tests, bugs fixes. --- .../{LengthError.php => SizeError.php} | 2 +- src/Interfaces/ArrayViewInterface.php | 36 ++++++++++ src/Views/ArrayMaskView.php | 4 +- src/Views/ArrayView.php | 30 ++++----- tests/unit/ArrayView/ErrorsTest.php | 65 +++++++++++++++++++ 5 files changed, 118 insertions(+), 19 deletions(-) rename src/Exceptions/{LengthError.php => SizeError.php} (53%) diff --git a/src/Exceptions/LengthError.php b/src/Exceptions/SizeError.php similarity index 53% rename from src/Exceptions/LengthError.php rename to src/Exceptions/SizeError.php index 6aeed77..a8516a4 100644 --- a/src/Exceptions/LengthError.php +++ b/src/Exceptions/SizeError.php @@ -2,6 +2,6 @@ namespace Smoren\ArrayView\Exceptions; -class LengthError extends \RuntimeException +class SizeError extends \RuntimeException { } diff --git a/src/Interfaces/ArrayViewInterface.php b/src/Interfaces/ArrayViewInterface.php index 90a7331..d248ec5 100644 --- a/src/Interfaces/ArrayViewInterface.php +++ b/src/Interfaces/ArrayViewInterface.php @@ -77,4 +77,40 @@ public function set($newValues): self; * @return bool */ public function isReadonly(): bool; + + /** + * @return int + */ + public function count(): int; + + /** + * @param numeric|string|ArraySelectorInterface $offset + * @return bool + */ + public function offsetExists($offset): bool; + + /** + * @param numeric|string|ArraySelectorInterface $offset + * @return T|array + */ + #[\ReturnTypeWillChange] + public function offsetGet($offset); + + /** + * @param numeric|string|ArraySelectorInterface $offset + * @param T|array|ArrayViewInterface $value + * @return void + */ + public function offsetSet($offset, $value): void; + + /** + * @param numeric|string|ArraySelectorInterface $offset + * @return void + */ + public function offsetUnset($offset): void; + + /** + * @return \Generator + */ + public function getIterator(): \Generator; } diff --git a/src/Views/ArrayMaskView.php b/src/Views/ArrayMaskView.php index b2f46c2..05deb6a 100644 --- a/src/Views/ArrayMaskView.php +++ b/src/Views/ArrayMaskView.php @@ -2,7 +2,7 @@ namespace Smoren\ArrayView\Views; -use Smoren\ArrayView\Exceptions\LengthError; +use Smoren\ArrayView\Exceptions\SizeError; use Smoren\ArrayView\Interfaces\ArrayViewInterface; /** @@ -25,7 +25,7 @@ public function __construct(&$source, array $mask, ?bool $readonly = null) { [$sourceSize, $maskSize] = [\count($source), \count($mask)]; if ($sourceSize !== $maskSize) { - throw new LengthError("Mask length not equal to source length ({$maskSize} != {$maskSize})."); + throw new SizeError("Mask size not equal to source length ({$maskSize} != {$sourceSize})."); } $indexes = array_filter( diff --git a/src/Views/ArrayView.php b/src/Views/ArrayView.php index 9eff704..9809387 100644 --- a/src/Views/ArrayView.php +++ b/src/Views/ArrayView.php @@ -6,7 +6,7 @@ use Smoren\ArrayView\Exceptions\IndexError; use Smoren\ArrayView\Exceptions\KeyError; -use Smoren\ArrayView\Exceptions\LengthError; +use Smoren\ArrayView\Exceptions\SizeError; use Smoren\ArrayView\Exceptions\NotSupportedError; use Smoren\ArrayView\Exceptions\ReadonlyError; use Smoren\ArrayView\Exceptions\ValueError; @@ -146,7 +146,7 @@ public function applyWith($data, callable $mapper): self { [$dataSize, $thisSize] = [\count($data), \count($this)]; if ($dataSize !== $thisSize) { - throw new LengthError("Length of values array not equal to view length ({$dataSize} != {$thisSize})."); + throw new SizeError("Length of values array not equal to view length ({$dataSize} != {$thisSize})."); } $dataView = ArrayView::toView($data); @@ -178,13 +178,12 @@ public function set($newValues): self [$dataSize, $thisSize] = [\count($newValues), \count($this)]; if ($dataSize !== $thisSize) { - throw new LengthError("Length of values array not equal to view length ({$dataSize} != {$thisSize})."); + throw new SizeError("Length of values array not equal to view length ({$dataSize} != {$thisSize})."); } $newValuesView = ArrayView::toView($newValues); for ($i = 0; $i < \count($this); $i++) { - // @phpstan-ignore-next-line $this[$i] = $newValuesView[$i]; } @@ -192,7 +191,7 @@ public function set($newValues): self } /** - * @return \Generator + * {@inheritDoc} */ public function getIterator(): \Generator { @@ -204,7 +203,7 @@ public function getIterator(): \Generator } /** - * @return bool + * {@inheritDoc} */ public function isReadonly(): bool { @@ -212,8 +211,7 @@ public function isReadonly(): bool } /** - * @param numeric|string|ArraySelectorInterface $offset - * @return bool + * {@inheritDoc} */ public function offsetExists($offset): bool { @@ -233,8 +231,7 @@ public function offsetExists($offset): bool } /** - * @param numeric|string|ArraySelectorInterface $offset - * @return T|array + * {@inheritDoc} */ #[\ReturnTypeWillChange] public function offsetGet($offset) @@ -260,9 +257,7 @@ public function offsetGet($offset) } /** - * @param numeric|string|ArraySelectorInterface $offset - * @param T|array|ArrayViewInterface $value - * @return void + * {@inheritDoc} */ public function offsetSet($offset, $value): void { @@ -297,9 +292,9 @@ public function offsetSet($offset, $value): void } /** - * @param numeric|string|ArraySelectorInterface $offset - * @return void * @throws NotSupportedError + * + * {@inheritDoc} */ public function offsetUnset($offset): void { @@ -307,13 +302,16 @@ public function offsetUnset($offset): void } /** - * @return int + * {@inheritDoc} */ public function count(): int { return $this->getParentSize(); } + /** + * @return int + */ protected function getParentSize(): int { return ($this->parentView !== null) diff --git a/tests/unit/ArrayView/ErrorsTest.php b/tests/unit/ArrayView/ErrorsTest.php index aa6b153..0180e30 100644 --- a/tests/unit/ArrayView/ErrorsTest.php +++ b/tests/unit/ArrayView/ErrorsTest.php @@ -4,7 +4,9 @@ use Smoren\ArrayView\Exceptions\IndexError; use Smoren\ArrayView\Exceptions\KeyError; +use Smoren\ArrayView\Exceptions\SizeError; use Smoren\ArrayView\Exceptions\ValueError; +use Smoren\ArrayView\Selectors\MaskSelector; use Smoren\ArrayView\Views\ArrayView; class ErrorsTest extends \Codeception\Test\Unit @@ -77,6 +79,54 @@ public function testWriteKeyError(array $source, array $keys) } } + /** + * @dataProvider dataProviderForBadSizeMask + */ + public function testReadByMaskSizeError(array $source, array $boolMask) + { + $view = ArrayView::toView($source); + + $boolMaskSize = \count($boolMask); + $sourceSize = \count($source); + + $this->expectException(SizeError::class); + $this->expectExceptionMessage("Mask size not equal to source length ({$boolMaskSize} != {$sourceSize})."); + + $_ = $view[new MaskSelector($boolMask)]; + } + + /** + * @dataProvider dataProviderForBadSizeMask + */ + public function testGetSubviewByMaskSizeError(array $source, array $boolMask) + { + $view = ArrayView::toView($source); + + $boolMaskSize = \count($boolMask); + $sourceSize = \count($source); + + $this->expectException(SizeError::class); + $this->expectExceptionMessage("Mask size not equal to source length ({$boolMaskSize} != {$sourceSize})."); + + $view->subview(new MaskSelector($boolMask)); + } + + /** + * @dataProvider dataProviderForBadSizeMask + */ + public function testWriteByMaskSizeError(array $source, array $boolMask) + { + $view = ArrayView::toView($source); + + $boolMaskSize = \count($boolMask); + $sourceSize = \count($source); + + $this->expectException(SizeError::class); + $this->expectExceptionMessage("Mask size not equal to source length ({$boolMaskSize} != {$sourceSize})."); + + $view[new MaskSelector($boolMask)] = $boolMask; + } + /** * @dataProvider dataProviderForNonSequentialError */ @@ -112,6 +162,21 @@ public function dataProviderForBadKeys(): array ]; } + public function dataProviderForBadSizeMask(): array + { + return [ + [[], [1]], + [[1], []], + [[1], [1, 0]], + [[1, 2, 3], [1]], + [[1, 2, 3], [0]], + [[1, 2, 3], [0, 1]], + [[1, 2, 3], [0, 1, 1, 0]], + [[1, 2, 3], [1, 1, 1, 1, 1]], + [[1, 2, 3], [0, 0, 0, 0, 0]], + ]; + } + public function dataProviderForNonSequentialError(): array { return [