diff --git a/src/Services/Forms/Options.php b/src/Services/Forms/Options.php index 98178dda7..58fccd6b5 100644 --- a/src/Services/Forms/Options.php +++ b/src/Services/Forms/Options.php @@ -16,7 +16,7 @@ class Options extends Collection */ public static function fromArray(array $options): static { - return static::make(collect($options)->map(function ($key, $value) { + return static::make(collect($options)->map(function ($value, $key) { if ($value instanceof Option) { return $value; } @@ -24,6 +24,6 @@ public static function fromArray(array $options): static return is_array($value) ? Option::make(...$value) : Option::make($key, $value); - })); + })->values()); } } diff --git a/tests/unit/Services/Forms/OptionsTest.php b/tests/unit/Services/Forms/OptionsTest.php new file mode 100644 index 000000000..8692a96df --- /dev/null +++ b/tests/unit/Services/Forms/OptionsTest.php @@ -0,0 +1,67 @@ + [ + [ + Option::make('foo', 'Foo label'), + Option::make('bar', 'Bar label', false), + ], + false, + ], + + 'array of key-value pairs' => [ + [ + 'foo' => 'Foo label', + 'bar' => 'Bar label', + ], + true, + ], + + 'array of arrays' => [ + [ + [ + 'value' => 'foo', + 'label' => 'Foo label', + ], + [ + 'value' => 'bar', + 'label' => 'Bar label', + 'selectable' => false, + ], + ], + false, + ], + ]; + } + + /** + * @dataProvider inputs + */ + public function testFromArray(array $options, bool $barShouldBeSelectable): void + { + $options = Options::fromArray($options); + + $this->assertEquals([ + [ + 'value' => 'foo', + 'label' => 'Foo label', + 'selectable' => true, + ], + [ + 'value' => 'bar', + 'label' => 'Bar label', + 'selectable' => $barShouldBeSelectable, + ], + ], $options->toArray()); + } +}