Skip to content

Commit

Permalink
fix argument order
Browse files Browse the repository at this point in the history
  • Loading branch information
bonroyage authored and ifox committed May 15, 2023
1 parent 0fb6f71 commit 8331d82
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
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());
}
}

0 comments on commit 8331d82

Please sign in to comment.