|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Derafu: Support - Essential PHP Utilities. |
| 7 | + * |
| 8 | + * Copyright (c) 2025 Esteban De La Fuente Rubio / Derafu <https://www.derafu.dev> |
| 9 | + * Licensed under the MIT License. |
| 10 | + * See LICENSE file for more details. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Derafu\TestsSupport\Arr; |
| 14 | + |
| 15 | +use Derafu\Support\Arr; |
| 16 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 17 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +#[CoversClass(Arr::class)] |
| 22 | +class ArrDotNestedTest extends TestCase |
| 23 | +{ |
| 24 | + #[Test] |
| 25 | + #[DataProvider('provideDotData')] |
| 26 | + public function shouldFlattenArrayWithDotNotation( |
| 27 | + array $input, |
| 28 | + string $prefix, |
| 29 | + array $expected |
| 30 | + ): void { |
| 31 | + $result = Arr::dot($input, $prefix); |
| 32 | + $this->assertSame($expected, $result); |
| 33 | + } |
| 34 | + |
| 35 | + public static function provideDotData(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + 'simple nested array' => [ |
| 39 | + ['a' => ['b' => 'c']], |
| 40 | + '', |
| 41 | + ['a.b' => 'c'], |
| 42 | + ], |
| 43 | + 'with prefix' => [ |
| 44 | + ['a' => ['b' => 'c']], |
| 45 | + 'x', |
| 46 | + ['x.a.b' => 'c'], |
| 47 | + ], |
| 48 | + 'flat array' => [ |
| 49 | + ['a' => 1, 'b' => 2], |
| 50 | + '', |
| 51 | + ['a' => 1, 'b' => 2], |
| 52 | + ], |
| 53 | + 'empty array' => [ |
| 54 | + [], |
| 55 | + '', |
| 56 | + [], |
| 57 | + ], |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + #[Test] |
| 62 | + #[DataProvider('provideNestedData')] |
| 63 | + public function shouldConvertDotNotationToNestedArray( |
| 64 | + array $input, |
| 65 | + array $expected |
| 66 | + ): void { |
| 67 | + $result = Arr::nested($input); |
| 68 | + $this->assertSame($expected, $result); |
| 69 | + } |
| 70 | + |
| 71 | + public static function provideNestedData(): array |
| 72 | + { |
| 73 | + return [ |
| 74 | + 'simple dot notation' => [ |
| 75 | + ['a.b' => 'c'], |
| 76 | + ['a' => ['b' => 'c']], |
| 77 | + ], |
| 78 | + 'flat array' => [ |
| 79 | + ['a' => 1, 'b' => 2], |
| 80 | + ['a' => 1, 'b' => 2], |
| 81 | + ], |
| 82 | + 'empty array' => [ |
| 83 | + [], |
| 84 | + [], |
| 85 | + ], |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + #[Test] |
| 90 | + #[DataProvider('provideCycleData')] |
| 91 | + public function shouldCompleteCycleDotToNestedAndBack( |
| 92 | + array $original |
| 93 | + ): void { |
| 94 | + $dot = Arr::dot($original); |
| 95 | + $nested = Arr::nested($dot); |
| 96 | + $this->assertSame($original, $nested); |
| 97 | + } |
| 98 | + |
| 99 | + public static function provideCycleData(): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'simple nested structure' => [ |
| 103 | + ['a' => ['b' => 'c']], |
| 104 | + ], |
| 105 | + 'flat array' => [ |
| 106 | + ['a' => 1, 'b' => 2], |
| 107 | + ], |
| 108 | + 'empty array' => [ |
| 109 | + [], |
| 110 | + ], |
| 111 | + ]; |
| 112 | + } |
| 113 | +} |
0 commit comments