|
| 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 | +} |
0 commit comments