|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Smoren\ArrayView\Tests\Unit\ArrayView; |
| 4 | + |
| 5 | +use Smoren\ArrayView\Exceptions\ReadonlyError; |
| 6 | +use Smoren\ArrayView\Selectors\IndexListSelector; |
| 7 | +use Smoren\ArrayView\Selectors\MaskSelector; |
| 8 | +use Smoren\ArrayView\Views\ArrayView; |
| 9 | + |
| 10 | +class ReadonlyTest extends \Codeception\Test\Unit |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @dataProvider dataProviderForReadonly |
| 14 | + * @dataProvider dataProviderForReadonlySubview |
| 15 | + */ |
| 16 | + public function testWriteByIndex(array $source, callable $readonlyViewGetter, array $expected) |
| 17 | + { |
| 18 | + $view = $readonlyViewGetter($source); |
| 19 | + |
| 20 | + for ($i = 0; $i < \count($view); ++$i) { |
| 21 | + try { |
| 22 | + $view[$i] = 1; |
| 23 | + $this->fail(); |
| 24 | + } catch (\Exception $e) { |
| 25 | + $this->assertInstanceOf(ReadonlyError::class, $e); |
| 26 | + $this->assertSame("Cannot modify a readonly view.", $e->getMessage()); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + $this->assertSame($expected, [...$view]); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @dataProvider dataProviderForReadonly |
| 35 | + * @dataProvider dataProviderForReadonlySubview |
| 36 | + */ |
| 37 | + public function testAll(array $source, callable $readonlyViewGetter, array $expected) |
| 38 | + { |
| 39 | + $view = $readonlyViewGetter($source); |
| 40 | + |
| 41 | + try { |
| 42 | + $view[':'] = [...$view]; |
| 43 | + $this->fail(); |
| 44 | + } catch (\Exception $e) { |
| 45 | + $this->assertInstanceOf(ReadonlyError::class, $e); |
| 46 | + $this->assertSame("Cannot modify a readonly view.", $e->getMessage()); |
| 47 | + $this->assertSame($expected, [...$view]); |
| 48 | + } |
| 49 | + |
| 50 | + $this->assertSame($expected, [...$view]); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dataProvider dataProviderForReadonly |
| 55 | + * @dataProvider dataProviderForReadonlySubview |
| 56 | + */ |
| 57 | + public function testApply(array $source, callable $readonlyViewGetter, array $expected) |
| 58 | + { |
| 59 | + $view = $readonlyViewGetter($source); |
| 60 | + |
| 61 | + try { |
| 62 | + $view->apply(fn ($item) => $item); |
| 63 | + $this->fail(); |
| 64 | + } catch (\Exception $e) { |
| 65 | + $this->assertInstanceOf(ReadonlyError::class, $e); |
| 66 | + $this->assertSame("Cannot modify a readonly view.", $e->getMessage()); |
| 67 | + $this->assertSame($expected, [...$view]); |
| 68 | + } |
| 69 | + |
| 70 | + $this->assertSame($expected, [...$view]); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @dataProvider dataProviderForReadonly |
| 75 | + * @dataProvider dataProviderForReadonlySubview |
| 76 | + */ |
| 77 | + public function testApplyWith(array $source, callable $readonlyViewGetter, array $expected) |
| 78 | + { |
| 79 | + $view = $readonlyViewGetter($source); |
| 80 | + |
| 81 | + try { |
| 82 | + $view->applyWith([...$view], fn ($lhs, $rhs) => $lhs + $rhs); |
| 83 | + $this->fail(); |
| 84 | + } catch (\Exception $e) { |
| 85 | + $this->assertInstanceOf(ReadonlyError::class, $e); |
| 86 | + $this->assertSame("Cannot modify a readonly view.", $e->getMessage()); |
| 87 | + $this->assertSame($expected, [...$view]); |
| 88 | + } |
| 89 | + |
| 90 | + $this->assertSame($expected, [...$view]); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @dataProvider dataProviderForReadonlyCreateError |
| 95 | + */ |
| 96 | + public function testCreateError(array $source, callable $readonlyViewGetter) |
| 97 | + { |
| 98 | + $this->expectException(ReadonlyError::class); |
| 99 | + $this->expectExceptionMessage("Cannot create non-readonly view for readonly source."); |
| 100 | + $readonlyViewGetter($source); |
| 101 | + } |
| 102 | + |
| 103 | + public function dataProviderForReadonly(): array |
| 104 | + { |
| 105 | + return [ |
| 106 | + [ |
| 107 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 108 | + fn (array &$source) => ArrayView::toView($source, true), |
| 109 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 110 | + ], |
| 111 | + [ |
| 112 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 113 | + fn (array &$source) => ArrayView::toUnlinkedView(ArrayView::toView($source, true), true), |
| 114 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 115 | + ], |
| 116 | + [ |
| 117 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 118 | + fn (array &$source) => ArrayView::toView($source, true) |
| 119 | + ->subview('::2'), |
| 120 | + [1, 3, 5, 7, 9], |
| 121 | + ], |
| 122 | + [ |
| 123 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 124 | + fn (array &$source) => ArrayView::toView($source, true) |
| 125 | + ->subview('::2', true) |
| 126 | + ->subview(new MaskSelector([true, false, true, false, true])), |
| 127 | + [1, 5, 9], |
| 128 | + ], |
| 129 | + [ |
| 130 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 131 | + fn (array &$source) => ArrayView::toView($source, true) |
| 132 | + ->subview('::2') |
| 133 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 134 | + ->subview(new IndexListSelector([0, 2])), |
| 135 | + [1, 9], |
| 136 | + ], |
| 137 | + [ |
| 138 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 139 | + fn (array &$source) => ArrayView::toView($source, true) |
| 140 | + ->subview('::2') |
| 141 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 142 | + ->subview(new IndexListSelector([0, 2])) |
| 143 | + ->subview('1:'), |
| 144 | + [9], |
| 145 | + ], |
| 146 | + [ |
| 147 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 148 | + fn (array &$source) => ArrayView::toView($source, true) |
| 149 | + ->subview(new MaskSelector([true, false, true, false, true, false, true, false, true, false])) |
| 150 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 151 | + ->subview(new MaskSelector([true, false, true])) |
| 152 | + ->subview(new MaskSelector([false, true])), |
| 153 | + [9], |
| 154 | + ], |
| 155 | + [ |
| 156 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 157 | + fn (array &$source) => ArrayView::toView($source, true) |
| 158 | + ->subview(new MaskSelector([true, false, true, false, true, false, true, false, true, false])) |
| 159 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 160 | + ->subview(new MaskSelector([true, false, true])), |
| 161 | + [1, 9], |
| 162 | + ], |
| 163 | + [ |
| 164 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 165 | + fn (array &$source) => ArrayView::toView($source, true) |
| 166 | + ->subview(new IndexListSelector([0, 2, 4, 6, 8])) |
| 167 | + ->subview(new IndexListSelector([0, 2, 4])) |
| 168 | + ->subview(new IndexListSelector([0, 2])) |
| 169 | + ->subview(new IndexListSelector([1])), |
| 170 | + [9], |
| 171 | + ], |
| 172 | + [ |
| 173 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 174 | + fn (array &$source) => ArrayView::toView($source, true) |
| 175 | + ->subview('::2') |
| 176 | + ->subview('::2') |
| 177 | + ->subview('::2'), |
| 178 | + [1, 9], |
| 179 | + ], |
| 180 | + [ |
| 181 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 182 | + fn (array &$source) => ArrayView::toView($source, true) |
| 183 | + ->subview('::2') |
| 184 | + ->subview('::2') |
| 185 | + ->subview('::2') |
| 186 | + ->subview('1:'), |
| 187 | + [9], |
| 188 | + ], |
| 189 | + ]; |
| 190 | + } |
| 191 | + |
| 192 | + public function dataProviderForReadonlySubview(): array |
| 193 | + { |
| 194 | + return [ |
| 195 | + [ |
| 196 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 197 | + fn (array &$source) => ArrayView::toView($source, true) |
| 198 | + ->subview('::2', true), |
| 199 | + [1, 3, 5, 7, 9], |
| 200 | + ], |
| 201 | + [ |
| 202 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 203 | + fn (array &$source) => ArrayView::toUnlinkedView(new ArrayView($source)) |
| 204 | + ->subview('::2', true), |
| 205 | + [1, 3, 5, 7, 9], |
| 206 | + ], |
| 207 | + [ |
| 208 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 209 | + fn (array &$source) => ArrayView::toView($source, true) |
| 210 | + ->subview('::2', true) |
| 211 | + ->subview(new MaskSelector([true, false, true, false, true])), |
| 212 | + [1, 5, 9], |
| 213 | + ], |
| 214 | + [ |
| 215 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 216 | + fn (array &$source) => ArrayView::toView($source, true) |
| 217 | + ->subview('::2') |
| 218 | + ->subview(new MaskSelector([true, false, true, false, true]), true) |
| 219 | + ->subview(new IndexListSelector([0, 2])), |
| 220 | + [1, 9], |
| 221 | + ], |
| 222 | + [ |
| 223 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 224 | + fn (array &$source) => ArrayView::toView($source, true) |
| 225 | + ->subview('::2', true) |
| 226 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 227 | + ->subview(new IndexListSelector([0, 2]), true) |
| 228 | + ->subview('1:'), |
| 229 | + [9], |
| 230 | + ], |
| 231 | + [ |
| 232 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 233 | + fn (array &$source) => ArrayView::toView($source, true) |
| 234 | + ->subview(new MaskSelector([true, false, true, false, true, false, true, false, true, false]), true) |
| 235 | + ->subview(new MaskSelector([true, false, true, false, true]), true) |
| 236 | + ->subview(new MaskSelector([true, false, true]), true) |
| 237 | + ->subview(new MaskSelector([false, true]), true), |
| 238 | + [9], |
| 239 | + ], |
| 240 | + [ |
| 241 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 242 | + fn (array &$source) => ArrayView::toView($source, true) |
| 243 | + ->subview(new MaskSelector([true, false, true, false, true, false, true, false, true, false])) |
| 244 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 245 | + ->subview(new MaskSelector([true, false, true]), true), |
| 246 | + [1, 9], |
| 247 | + ], |
| 248 | + [ |
| 249 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 250 | + fn (array &$source) => ArrayView::toView($source, true) |
| 251 | + ->subview(new IndexListSelector([0, 2, 4, 6, 8])) |
| 252 | + ->subview(new IndexListSelector([0, 2, 4])) |
| 253 | + ->subview(new IndexListSelector([0, 2])) |
| 254 | + ->subview(new IndexListSelector([1]), true), |
| 255 | + [9], |
| 256 | + ], |
| 257 | + [ |
| 258 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 259 | + fn (array &$source) => ArrayView::toView($source, true) |
| 260 | + ->subview('::2') |
| 261 | + ->subview('::2', true) |
| 262 | + ->subview('::2'), |
| 263 | + [1, 9], |
| 264 | + ], |
| 265 | + [ |
| 266 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 267 | + fn (array &$source) => ArrayView::toView($source, true) |
| 268 | + ->subview('::2') |
| 269 | + ->subview('::2') |
| 270 | + ->subview('::2') |
| 271 | + ->subview('1:', true), |
| 272 | + [9], |
| 273 | + ], |
| 274 | + ]; |
| 275 | + } |
| 276 | + |
| 277 | + public function dataProviderForReadonlyCreateError(): array |
| 278 | + { |
| 279 | + return [ |
| 280 | + [ |
| 281 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 282 | + fn (array &$source) => ArrayView::toView($source, true) |
| 283 | + ->subview('::2', false), |
| 284 | + ], |
| 285 | + [ |
| 286 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 287 | + fn (array &$source) => ArrayView::toUnlinkedView(ArrayView::toView($source), true) |
| 288 | + ->subview('::2', false), |
| 289 | + ], |
| 290 | + [ |
| 291 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 292 | + fn (array &$source) => ArrayView::toView($source, true) |
| 293 | + ->subview('::2') |
| 294 | + ->subview(new MaskSelector([true, false, true, false, true]), false), |
| 295 | + ], |
| 296 | + [ |
| 297 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 298 | + fn (array &$source) => ArrayView::toView($source) |
| 299 | + ->subview('::2', true) |
| 300 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 301 | + ->subview(new IndexListSelector([0, 2]), false), |
| 302 | + ], |
| 303 | + [ |
| 304 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 305 | + fn (array &$source) => ArrayView::toView($source, true) |
| 306 | + ->subview('::2', false) |
| 307 | + ->subview(new MaskSelector([true, false, true, false, true])) |
| 308 | + ->subview(new IndexListSelector([0, 2])) |
| 309 | + ->subview('1:'), |
| 310 | + ], |
| 311 | + [ |
| 312 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 313 | + fn (array &$source) => ArrayView::toView($source, false) |
| 314 | + ->subview(new MaskSelector([true, false, true, false, true, false, true, false, true, false])) |
| 315 | + ->subview(new MaskSelector([true, false, true, false, true]), true) |
| 316 | + ->subview(new MaskSelector([true, false, true]), false) |
| 317 | + ->subview(new MaskSelector([false, true])), |
| 318 | + ], |
| 319 | + [ |
| 320 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 321 | + fn (array &$source) => ArrayView::toView($source, true) |
| 322 | + ->subview('::2', false) |
| 323 | + ->subview('::2') |
| 324 | + ->subview('::2'), |
| 325 | + ], |
| 326 | + [ |
| 327 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 328 | + fn (array &$source) => ArrayView::toView($source, false) |
| 329 | + ->subview('::2', true) |
| 330 | + ->subview('::2', false) |
| 331 | + ->subview('::2', true) |
| 332 | + ->subview('1:', false), |
| 333 | + ], |
| 334 | + ]; |
| 335 | + } |
| 336 | +} |
0 commit comments