Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests, fixes. #3

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Interfaces/ArrayViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ interface ArrayViewInterface extends \ArrayAccess, \IteratorAggregate, \Countabl
*/
public static function toView(&$source, ?bool $readonly = null): ArrayViewInterface;

/**
* @param array<T>|ArrayViewInterface<T> $source
* @param bool|null $readonly
* @return ArrayViewInterface<T>
*/
public static function toUnlinkedView($source, ?bool $readonly = null): ArrayViewInterface;

/**
* @return array<T>
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Selectors/IndexListSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ final class IndexListSelector implements ArraySelectorInterface
private array $value;

/**
* @param array<int> $value
* @param array<int>|ArrayViewInterface<int> $value
*/
public function __construct(array $value)
public function __construct($value)
{
$this->value = $value;
$this->value = \is_array($value) ? $value : $value->toArray();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Selectors/MaskSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class MaskSelector implements ArraySelectorInterface
/**
* @var array<bool>
*/
private array $value;
private $value;

/**
* @param array<bool> $value
* @param array<bool>|ArrayViewInterface<bool> $value
*/
public function __construct(array $value)
public function __construct($value)
{
$this->value = $value;
$this->value = \is_array($value) ? $value : $value->toArray();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Views/ArrayView.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public static function toView(&$source, ?bool $readonly = null): ArrayViewInterf
return $source;
}

/**
* {@inheritDoc}
*/
public static function toUnlinkedView($source, ?bool $readonly = null): ArrayViewInterface
{
return static::toView($source, $readonly);
}

/**
* @param array<T>|ArrayViewInterface<T> $source
* @param bool|null $readonly
Expand Down
177 changes: 177 additions & 0 deletions tests/unit/ArrayView/ReadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArrayView;

use Smoren\ArrayView\Selectors\IndexListSelector;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayIndexListView;
use Smoren\ArrayView\Views\ArrayMaskView;
use Smoren\ArrayView\Views\ArraySliceView;
use Smoren\ArrayView\Views\ArrayView;

class ReadTest extends \Codeception\Test\Unit
{
/**
* @dataProvider dataProviderForArrayRead
*/
public function testRead(array $source)
{
$view = ArrayView::toView($source);

foreach ($source as $i => $value) {
$actual = $view[$i];
$actualByStringIndex = $view[strval($i)];
$expected = $source[$i];

$this->assertSame($expected, $actual);
$this->assertSame($expected, $actualByStringIndex);
}

$this->assertSame($source, $view->toArray());
$this->assertSame($source, [...$view]);
}

/**
* @dataProvider dataProviderForReadCombine
*/
public function testReadCombined(array $source, callable $viewGetter, array $expected)
{
$view = $viewGetter($source);

$this->assertSame($view->toArray(), $expected);
}

public function dataProviderForArrayRead(): array
{
return [
[[1]],
[[1, 2]],
[[1, 2, 3]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]],
[[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]],
];
}

public function dataProviderForReadCombine(): array
{
return [
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2'),
[1, 3, 5, 7, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => (new ArrayIndexListView($source, array_keys($source)))
->subview('::2'),
[1, 3, 5, 7, 9],
],

[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => (new ArrayIndexListView($source, array_keys($source)))
->subview('::2'),
[1, 3, 5, 7, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => (new ArrayMaskView(
$source,
[true, true, true, true, true, true, true, true, true, true]
))->subview('::2'),
[1, 3, 5, 7, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => (new ArraySliceView($source, new SliceSelector("::1")))
->subview('::2'),
[1, 3, 5, 7, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2')
->subview(new MaskSelector([true, false, true, false, true])),
[1, 5, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2')
->subview(new MaskSelector([true, false, true, false, true]))
->subview(new IndexListSelector([0, 2])),
[1, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2')
->subview(new MaskSelector([true, false, true, false, true]))
->subview(new IndexListSelector([0, 2])),
[1, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2')
->subview(new MaskSelector(ArrayView::toUnlinkedView([true, false, true, false, true])))
->subview(new IndexListSelector(ArrayView::toUnlinkedView([0, 2]))),
[1, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2')
->subview(new MaskSelector([true, false, true, false, true]))
->subview(new IndexListSelector([0, 2]))
->subview('1:'),
[9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview(new MaskSelector([true, false, true, false, true, false, true, false, true, false]))
->subview(new MaskSelector([true, false, true, false, true]))
->subview(new MaskSelector([true, false, true]))
->subview(new MaskSelector([false, true])),
[9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview(new MaskSelector([true, false, true, false, true, false, true, false, true, false]))
->subview(new MaskSelector([true, false, true, false, true]))
->subview(new MaskSelector([true, false, true])),
[1, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview(new IndexListSelector([0, 2, 4, 6, 8]))
->subview(new IndexListSelector([0, 2, 4]))
->subview(new IndexListSelector([0, 2]))
->subview(new IndexListSelector([1])),
[9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2')
->subview('::2')
->subview('::2'),
[1, 9],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)
->subview('::2')
->subview('::2')
->subview('::2')
->subview('1:'),
[9],
],
];
}
}
Loading
Loading