Skip to content

Commit f7de579

Browse files
committed
Fix field options with tests
1 parent c4292fc commit f7de579

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

src/Html/Editor/Fields/Field.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Contracts\Support\Arrayable;
7+
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\Query\Builder as QueryBuilder;
89
use Illuminate\Support\Fluent;
910
use Illuminate\Support\Str;
@@ -108,12 +109,12 @@ public function type(string $type): static
108109
/**
109110
* Get options from a model.
110111
*
111-
* @param class-string|\Illuminate\Database\Eloquent\Builder $model
112+
* @param \Illuminate\Database\Eloquent\Builder|class-string<\Illuminate\Database\Eloquent\Model> $model
112113
* @param string $value
113114
* @param string $key
114115
* @return $this
115116
*/
116-
public function modelOptions($model, string $value, string $key = 'id'): static
117+
public function modelOptions(Builder|string $model, string $value, string $key = 'id'): static
117118
{
118119
return $this->options(
119120
Options::model($model, $value, $key)

src/Html/Editor/Fields/Options.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ public static function table(
7777
$callback($query);
7878
}
7979

80-
return $query->get()->map(function ($row) use ($value, $key) {
81-
return [
82-
'value' => $row->value,
83-
'label' => $row->label,
84-
];
80+
return $query->get()->map(function ($row) {
81+
return (array) $row;
8582
});
8683
}
8784

tests/FieldOptionsTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests;
4+
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
use Illuminate\Support\Facades\DB;
7+
use Yajra\DataTables\Html\Editor\Fields\Options;
8+
use Yajra\DataTables\Html\Tests\Models\User;
9+
10+
class FieldOptionsTest extends TestCase
11+
{
12+
use DatabaseTransactions;
13+
14+
/** @test */
15+
public function it_has_true_false()
16+
{
17+
$options = Options::trueFalse();
18+
19+
$this->assertEquals([
20+
['label' => __('True'), 'value' => 1],
21+
['label' => __('False'), 'value' => 0],
22+
], $options->toArray());
23+
}
24+
25+
/** @test */
26+
public function it_has_yes_no()
27+
{
28+
$options = Options::yesNo();
29+
30+
$this->assertEquals([
31+
['label' => __('Yes'), 'value' => 1],
32+
['label' => __('No'), 'value' => 0],
33+
], $options->toArray());
34+
}
35+
36+
/** @test */
37+
public function it_can_append_and_prepend()
38+
{
39+
$options = Options::yesNo();
40+
41+
$this->assertEquals([
42+
['label' => __('Yes'), 'value' => 1],
43+
['label' => __('No'), 'value' => 0],
44+
], $options->toArray());
45+
46+
$options->append(__('Maybe'), 2);
47+
$this->assertEquals([
48+
['label' => __('Yes'), 'value' => 1],
49+
['label' => __('No'), 'value' => 0],
50+
['label' => __('Maybe'), 'value' => 2],
51+
], $options->toArray());
52+
53+
$options->prepend(__('IDK'), 3);
54+
$this->assertEquals([
55+
['label' => __('IDK'), 'value' => 3],
56+
['label' => __('Yes'), 'value' => 1],
57+
['label' => __('No'), 'value' => 0],
58+
['label' => __('Maybe'), 'value' => 2],
59+
], $options->toArray());
60+
}
61+
62+
/** @test */
63+
public function it_can_get_options_from_table()
64+
{
65+
$options = Options::table('users', 'name');
66+
$this->assertCount(20, $options);
67+
}
68+
69+
/** @test */
70+
public function it_can_get_options_from_query()
71+
{
72+
$options = Options::table(DB::table('users')->where('id', 1), 'name');
73+
$this->assertCount(1, $options);
74+
}
75+
76+
/** @test */
77+
public function it_can_get_options_from_model()
78+
{
79+
$options = Options::model(User::class, 'name');
80+
$this->assertCount(20, $options);
81+
}
82+
83+
/** @test */
84+
public function it_can_get_options_from_model_builder()
85+
{
86+
$options = Options::model(User::query()->whereKey(1), 'name');
87+
$this->assertCount(1, $options);
88+
}
89+
}

0 commit comments

Comments
 (0)