Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Options::fromArray argument order #2231

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Services/Forms/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ 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;
}

return is_array($value)
? Option::make(...$value)
: Option::make($key, $value);
}));
})->values());
}
}
67 changes: 67 additions & 0 deletions tests/unit/Services/Forms/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace A17\Twill\Tests\Unit\Services\Forms;

use A17\Twill\Services\Forms\Option;
use A17\Twill\Services\Forms\Options;
use A17\Twill\Tests\Unit\TestCase;

class OptionsTest extends TestCase
{
public static function inputs(): array
{
return [
'array of objects' => [
[
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());
}
}