Skip to content

Commit

Permalink
New tests, bugs fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 11, 2024
1 parent d1e15dd commit 157f7f1
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Smoren\ArrayView\Exceptions;

class LengthError extends \RuntimeException
class SizeError extends \RuntimeException
{
}
36 changes: 36 additions & 0 deletions src/Interfaces/ArrayViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset);

/**
* @param numeric|string|ArraySelectorInterface $offset
* @param T|array<T>|ArrayViewInterface<T> $value
* @return void
*/
public function offsetSet($offset, $value): void;

/**
* @param numeric|string|ArraySelectorInterface $offset
* @return void
*/
public function offsetUnset($offset): void;

/**
* @return \Generator<int, T>
*/
public function getIterator(): \Generator;
}
4 changes: 2 additions & 2 deletions src/Views/ArrayMaskView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Smoren\ArrayView\Views;

use Smoren\ArrayView\Exceptions\LengthError;
use Smoren\ArrayView\Exceptions\SizeError;
use Smoren\ArrayView\Interfaces\ArrayViewInterface;

/**
Expand All @@ -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(
Expand Down
30 changes: 14 additions & 16 deletions src/Views/ArrayView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -178,21 +178,20 @@ 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];
}

return $this;
}

/**
* @return \Generator<int, T>
* {@inheritDoc}
*/
public function getIterator(): \Generator
{
Expand All @@ -204,16 +203,15 @@ public function getIterator(): \Generator
}

/**
* @return bool
* {@inheritDoc}
*/
public function isReadonly(): bool
{
return $this->readonly;
}

/**
* @param numeric|string|ArraySelectorInterface $offset
* @return bool
* {@inheritDoc}
*/
public function offsetExists($offset): bool
{
Expand All @@ -233,8 +231,7 @@ public function offsetExists($offset): bool
}

/**
* @param numeric|string|ArraySelectorInterface $offset
* @return T|array<T>
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
Expand All @@ -260,9 +257,7 @@ public function offsetGet($offset)
}

/**
* @param numeric|string|ArraySelectorInterface $offset
* @param T|array<T>|ArrayViewInterface<T> $value
* @return void
* {@inheritDoc}
*/
public function offsetSet($offset, $value): void
{
Expand Down Expand Up @@ -297,23 +292,26 @@ public function offsetSet($offset, $value): void
}

/**
* @param numeric|string|ArraySelectorInterface $offset
* @return void
* @throws NotSupportedError
*
* {@inheritDoc}
*/
public function offsetUnset($offset): void
{
throw new NotSupportedError();
}

/**
* @return int
* {@inheritDoc}
*/
public function count(): int
{
return $this->getParentSize();
}

/**
* @return int
*/
protected function getParentSize(): int
{
return ($this->parentView !== null)
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/ArrayView/ErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 [
Expand Down

0 comments on commit 157f7f1

Please sign in to comment.