Skip to content

Commit 19eedda

Browse files
committed
IndexListView tests added.
1 parent 436bfd1 commit 19eedda

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Smoren\ArrayView\Tests\Unit\ArrayIndexListView;
4+
5+
use Smoren\ArrayView\Selectors\IndexListSelector;
6+
use Smoren\ArrayView\Selectors\MaskSelector;
7+
use Smoren\ArrayView\Views\ArrayIndexListView;
8+
use Smoren\ArrayView\Views\ArrayView;
9+
10+
class ReadTest extends \Codeception\Test\Unit
11+
{
12+
/**
13+
* @dataProvider dataProviderForRead
14+
*/
15+
public function testReadByMethod(array $source, array $indexes, array $expected)
16+
{
17+
$view = ArrayView::toView($source);
18+
$subview = $view->subview(new IndexListSelector($indexes));
19+
20+
$this->assertInstanceOf(ArrayIndexListView::class, $subview);
21+
22+
$this->assertSame($expected, [...$subview]);
23+
$this->assertSame(\count($expected), \count($subview));
24+
25+
for ($i = 0; $i < \count($subview); ++$i) {
26+
$this->assertSame($expected[$i], $subview[$i]);
27+
}
28+
29+
for ($i = 0; $i < \count($view); ++$i) {
30+
$this->assertSame($source[$i], $view[$i]);
31+
}
32+
33+
$this->assertSame($source, $view->toArray());
34+
$this->assertSame($expected, $subview->toArray());
35+
36+
$this->assertSame($source, [...$view]);
37+
$this->assertSame($expected, [...$subview]);
38+
}
39+
40+
/**
41+
* @dataProvider dataProviderForRead
42+
*/
43+
public function testReadByIndex(array $source, array $mask, array $expected)
44+
{
45+
$view = ArrayView::toView($source);
46+
$subArray = $view[new IndexListSelector($mask)];
47+
48+
$this->assertSame($expected, $subArray);
49+
$this->assertSame(\count($expected), \count($subArray));
50+
51+
for ($i = 0; $i < \count($subArray); ++$i) {
52+
$this->assertSame($expected[$i], $subArray[$i]);
53+
}
54+
55+
for ($i = 0; $i < \count($view); ++$i) {
56+
$this->assertSame($source[$i], $view[$i]);
57+
}
58+
59+
$this->assertSame($source, $view->toArray());
60+
$this->assertSame($source, [...$view]);
61+
$this->assertSame($expected, $subArray);
62+
}
63+
64+
public function dataProviderForRead(): array
65+
{
66+
return [
67+
[[], [], []],
68+
[[1], [], []],
69+
[[1, 2, 3], [], []],
70+
[[1], [0], [1]],
71+
[[1], [0, 0], [1, 1]],
72+
[[1], [0, 0, 0], [1, 1, 1]],
73+
[[1, 2], [0], [1]],
74+
[[1, 2], [1], [2]],
75+
[[1, 2], [0, 1], [1, 2]],
76+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 5, 7], [2, 4, 6, 8]],
77+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [7, 5, 3, 1], [8, 6, 4, 2]],
78+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 3, 7], [2, 6, 4, 8]],
79+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 7, 8], [1, 2, 8, 9]],
80+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 5, 5, 3], [2, 2, 6, 6, 4]],
81+
];
82+
}
83+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Smoren\ArrayView\Tests\Unit\ArrayIndexListView;
4+
5+
use Smoren\ArrayView\Selectors\IndexListSelector;
6+
use Smoren\ArrayView\Selectors\MaskSelector;
7+
use Smoren\ArrayView\Views\ArrayView;
8+
9+
class WriteTest extends \Codeception\Test\Unit
10+
{
11+
/**
12+
* @dataProvider dataProviderForMaskSubviewWrite
13+
*/
14+
public function testWriteByIndex(array $source, array $config, array $toWrite, array $expected)
15+
{
16+
$view = ArrayView::toView($source);
17+
18+
$view[new IndexListSelector($config)] = $toWrite;
19+
20+
$this->assertSame($expected, [...$view]);
21+
$this->assertSame($expected, $source);
22+
}
23+
24+
/**
25+
* @dataProvider dataProviderForMaskSubviewWrite
26+
*/
27+
public function testWriteBySubview(array $source, $config, array $toWrite, array $expected)
28+
{
29+
$view = ArrayView::toView($source);
30+
31+
$view->subview(new IndexListSelector($config))[':'] = $toWrite;
32+
33+
$this->assertSame($expected, [...$view]);
34+
$this->assertSame($expected, $source);
35+
}
36+
37+
public function dataProviderForMaskSubviewWrite(): array
38+
{
39+
return [
40+
[
41+
[],
42+
[],
43+
[],
44+
[],
45+
],
46+
[
47+
[1],
48+
[],
49+
[],
50+
[1],
51+
],
52+
[
53+
[1, 2, 3],
54+
[],
55+
[],
56+
[1, 2, 3],
57+
],
58+
[
59+
[1],
60+
[0],
61+
[2],
62+
[2],
63+
],
64+
[
65+
[1],
66+
[0, 0],
67+
[3, 3],
68+
[3],
69+
],
70+
[
71+
[1],
72+
[0, 0, 0],
73+
[4, 4, 4],
74+
[4],
75+
],
76+
[
77+
[1, 2],
78+
[0],
79+
[2],
80+
[2, 2],
81+
],
82+
[
83+
[1, 2],
84+
[1],
85+
[3],
86+
[1, 3],
87+
],
88+
[
89+
[1, 2],
90+
[0, 1],
91+
[2, 3],
92+
[2, 3],
93+
],
94+
[
95+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
96+
[1, 3, 5, 7],
97+
[3, 5, 7, 9],
98+
[1, 3, 3, 5, 5, 7, 7, 9, 9],
99+
],
100+
[
101+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
102+
[7, 5, 3, 1],
103+
[9, 7, 5, 3],
104+
[1, 3, 3, 5, 5, 7, 7, 9, 9],
105+
],
106+
[
107+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
108+
[1, 5, 3, 7],
109+
[3, 7, 5, 9],
110+
[1, 3, 3, 5, 5, 7, 7, 9, 9],
111+
],
112+
[
113+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
114+
[0, 1, 7, 8],
115+
[2, 3, 9, 10],
116+
[2, 3, 3, 4, 5, 6, 7, 9, 10],
117+
],
118+
[
119+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
120+
[1, 1, 5, 5, 3],
121+
[4, 4, 8, 8, 5],
122+
[1, 4, 3, 5, 5, 8, 7, 8, 9],
123+
],
124+
];
125+
}
126+
}

0 commit comments

Comments
 (0)