|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Smoren\ArrayView\Tests\Unit\ArraySliceView; |
| 4 | + |
| 5 | +use Smoren\ArrayView\Selectors\SliceSelector; |
| 6 | +use Smoren\ArrayView\Views\ArrayView; |
| 7 | + |
| 8 | +class WriteTest extends \Codeception\Test\Unit |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @dataProvider dataProviderForSliceSubviewWrite |
| 12 | + */ |
| 13 | + public function testWriteBySliceIndex(array $source, string $config, array $toWrite, array $expected) |
| 14 | + { |
| 15 | + $view = ArrayView::toView($source); |
| 16 | + |
| 17 | + $view[$config] = $toWrite; |
| 18 | + |
| 19 | + $this->assertSame($expected, [...$view]); |
| 20 | + $this->assertSame($expected, $source); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @dataProvider dataProviderForSliceSubviewWrite |
| 25 | + * @dataProvider dataProviderForSliceArraySubviewWrite |
| 26 | + */ |
| 27 | + public function testWriteBySubview(array $source, $config, array $toWrite, array $expected) |
| 28 | + { |
| 29 | + $view = ArrayView::toView($source); |
| 30 | + |
| 31 | + $view->subview(new SliceSelector($config))[':'] = $toWrite; |
| 32 | + |
| 33 | + $this->assertSame($expected, [...$view]); |
| 34 | + $this->assertSame($expected, $source); |
| 35 | + } |
| 36 | + |
| 37 | + public function dataProviderForSliceSubviewWrite(): array |
| 38 | + { |
| 39 | + return [ |
| 40 | + [[], ':', [], []], |
| 41 | + [[1], ':', [11], [11]], |
| 42 | + [[1, 2, 3], ':', [2, 4, 6], [2, 4, 6]], |
| 43 | + [[1, 2, 3], '0:', [2, 4, 6], [2, 4, 6]], |
| 44 | + [[1, 2, 3], ':3', [2, 4, 6], [2, 4, 6]], |
| 45 | + [[1, 2, 3], '0:3', [2, 4, 6], [2, 4, 6]], |
| 46 | + [[1, 2, 3], '1:', [22, 33], [1, 22, 33]], |
| 47 | + [[1, 2, 3], ':2', [11, 22], [11, 22, 3]], |
| 48 | + [[1, 2, 3], ':-1', [11, 22], [11, 22, 3]], |
| 49 | + [[1, 2, 3, 4, 5, 6], '::2', [77, 88, 99], [77, 2, 88, 4, 99, 6]], |
| 50 | + [[1, 2, 3, 4, 5, 6], '::-2', [77, 88, 99], [1, 99, 3, 88, 5, 77]], |
| 51 | + [[1, 2, 3, 4, 5, 6], '1::2', [77, 88, 99], [1, 77, 3, 88, 5, 99]], |
| 52 | + [[1, 2, 3, 4, 5, 6], '-2::-2', [77, 88, 99], [99, 2, 88, 4, 77, 6]], |
| 53 | + [[1, 2, 3, 4, 5, 6, 7, 8], ':-2:2', [77, 88, 99], [77, 2, 88, 4, 99, 6, 7, 8]], |
| 54 | + [[1, 2, 3, 4, 5, 6, 7, 8], ':6:2', [77, 88, 99], [77, 2, 88, 4, 99, 6, 7, 8]], |
| 55 | + [[1, 2, 3, 4, 5, 6, 7, 8], '1:-1:2', [77, 88, 99], [1, 77, 3, 88, 5, 99, 7, 8]], |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + public function dataProviderForSliceArraySubviewWrite(): array |
| 60 | + { |
| 61 | + return [ |
| 62 | + [[], [], [], []], |
| 63 | + [[], [null, null], [], []], |
| 64 | + [[1], [null, null], [11], [11]], |
| 65 | + [[1, 2, 3], [null, null], [2, 4, 6], [2, 4, 6]], |
| 66 | + [[1, 2, 3], [0,], [2, 4, 6], [2, 4, 6]], |
| 67 | + [[1, 2, 3], [0, 3], [2, 4, 6], [2, 4, 6]], |
| 68 | + [[1, 2, 3], [0, 3], [2, 4, 6], [2, 4, 6]], |
| 69 | + [[1, 2, 3], [1,], [22, 33], [1, 22, 33]], |
| 70 | + [[1, 2, 3], [null, 2], [11, 22], [11, 22, 3]], |
| 71 | + [[1, 2, 3], [null, -1], [11, 22], [11, 22, 3]], |
| 72 | + [[1, 2, 3, 4, 5, 6], [null, null, 2], [77, 88, 99], [77, 2, 88, 4, 99, 6]], |
| 73 | + [[1, 2, 3, 4, 5, 6], [null, null, -2], [77, 88, 99], [1, 99, 3, 88, 5, 77]], |
| 74 | + [[1, 2, 3, 4, 5, 6], [1, null, 2], [77, 88, 99], [1, 77, 3, 88, 5, 99]], |
| 75 | + [[1, 2, 3, 4, 5, 6], [-2, null, -2], [77, 88, 99], [99, 2, 88, 4, 77, 6]], |
| 76 | + [[1, 2, 3, 4, 5, 6, 7, 8], [null, -2, 2], [77, 88, 99], [77, 2, 88, 4, 99, 6, 7, 8]], |
| 77 | + [[1, 2, 3, 4, 5, 6, 7, 8], [null, 6, 2], [77, 88, 99], [77, 2, 88, 4, 99, 6, 7, 8]], |
| 78 | + [[1, 2, 3, 4, 5, 6, 7, 8], [1, -1, 2], [77, 88, 99], [1, 77, 3, 88, 5, 99, 7, 8]], |
| 79 | + ]; |
| 80 | + } |
| 81 | +} |
0 commit comments