Skip to content

Commit 436bfd1

Browse files
committed
ArrayMaskView tests added.
1 parent 0a6b045 commit 436bfd1

File tree

2 files changed

+172
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)