|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Illuminate\Validation\Rules\In; |
3 | 4 | use Spatie\LaravelOptions\Options; |
4 | 5 | use Spatie\LaravelOptions\Providers\NativeEnumProvider; |
5 | 6 | use Spatie\LaravelOptions\SelectOption; |
|
9 | 10 |
|
10 | 11 | it('can filter options', function () { |
11 | 12 | $options = Options::create(new NativeEnumProvider(StringEnum::class)) |
12 | | - ->filter(fn (StringEnum $enum) => $enum === StringEnum::Frodo) |
| 13 | + ->filter(fn(StringEnum $enum) => $enum === StringEnum::Frodo) |
13 | 14 | ->toArray(); |
14 | 15 |
|
15 | 16 | expect($options)->toBeArray()->toBe([ |
|
19 | 20 |
|
20 | 21 | it('can reject options', function () { |
21 | 22 | $options = Options::create(new NativeEnumProvider(StringEnum::class)) |
22 | | - ->reject(fn (StringEnum $enum) => $enum === StringEnum::Frodo) |
| 23 | + ->reject(fn(StringEnum $enum) => $enum === StringEnum::Frodo) |
23 | 24 | ->toArray(); |
24 | 25 |
|
25 | 26 | expect($options)->toBeArray()->toBe([ |
|
44 | 45 |
|
45 | 46 | it('can sort options using closure', function () { |
46 | 47 | $options = Options::create(new NativeEnumProvider(StringEnum::class)) |
47 | | - ->sort(fn (StringEnum $enum) => match ($enum) { |
| 48 | + ->sort(fn(StringEnum $enum) => match ($enum) { |
48 | 49 | StringEnum::Frodo => 4, |
49 | 50 | StringEnum::Sam => 3, |
50 | 51 | StringEnum::Merry => 2, |
|
81 | 82 | $model = Character::factory()->create(); |
82 | 83 |
|
83 | 84 | $options = Options::forModels([$model, $model]) |
84 | | - ->unique(fn (Character $character) => $character->getKey()) |
| 85 | + ->unique(fn(Character $character) => $character->getKey()) |
85 | 86 | ->toArray(); |
86 | 87 |
|
87 | 88 | expect($options)->toBeArray()->toBe([ |
|
92 | 93 | it('can add a null option', function () { |
93 | 94 | $options = Options::create(new NativeEnumProvider(StringEnum::class)) |
94 | 95 | ->nullable() |
95 | | - ->sort(fn (StringEnum $enum) => match ($enum) { |
| 96 | + ->sort(fn(StringEnum $enum) => match ($enum) { |
96 | 97 | StringEnum::Frodo => 4, |
97 | 98 | StringEnum::Sam => 3, |
98 | 99 | StringEnum::Merry => 2, |
|
139 | 140 |
|
140 | 141 | it('can append data using closure', function () { |
141 | 142 | $options = Options::create(new NativeEnumProvider(StringEnum::class)) |
142 | | - ->append(fn (StringEnum $enum) => ['upper' => strtoupper($enum->name)]) |
| 143 | + ->append(fn(StringEnum $enum) => ['upper' => strtoupper($enum->name)]) |
143 | 144 | ->toArray(); |
144 | 145 |
|
145 | 146 | expect($options)->toBeArray()->toBe([ |
|
178 | 179 | Character::factory()->create(['name' => 'Aragon', 'kind' => 'Men']); |
179 | 180 |
|
180 | 181 | $options = Options::forModels(SelectableCharacter::class) |
181 | | - ->append(fn (SelectableCharacter $character) => ['upper_name' => strtoupper($character->name)]) |
| 182 | + ->append(fn(SelectableCharacter $character) => ['upper_name' => strtoupper($character->name)]) |
182 | 183 | ->toArray(); |
183 | 184 |
|
184 | 185 | expect($options)->toBeArray()->toBe([ |
|
192 | 193 | }); |
193 | 194 |
|
194 | 195 | it('can be turned into a laravel validation rule', function () { |
195 | | - $rules = Options::create(new NativeEnumProvider(StringEnum::class)) |
196 | | - ->toValidationRule(); |
| 196 | + $rules = Options::create(new NativeEnumProvider(StringEnum::class))->toValidationRule(); |
197 | 197 |
|
198 | 198 | expect($rules) |
199 | 199 | ->toBeArray() |
200 | | - ->toHaveCount(1); |
201 | | - |
202 | | - $options = $rules[0]; |
203 | | - $optionsString = $options->__toString(); |
204 | | - |
205 | | - expect($options)->toBeInstanceOf(\Illuminate\Validation\Rules\In::class); |
206 | | - expect($optionsString) |
207 | | - ->toBeString() |
208 | | - ->toBe('in:"frodo","sam","merry","pippin"'); |
| 200 | + ->toHaveCount(1) |
| 201 | + ->toEqual([new In(['frodo', 'sam', 'merry', 'pippin'])]); |
209 | 202 | }); |
210 | 203 |
|
211 | 204 | it('can be turned into a laravel validation rule when nullable', function () { |
212 | 205 | $rules = Options::create(new NativeEnumProvider(StringEnum::class)) |
213 | | - ->nullable() |
214 | | - ->toValidationRule(); |
| 206 | + ->nullable() |
| 207 | + ->toValidationRule(); |
215 | 208 |
|
216 | 209 | expect($rules) |
217 | 210 | ->toBeArray() |
218 | | - ->toHaveCount(2); |
219 | | - |
220 | | - $nullable = $rules[0]; |
221 | | - |
222 | | - expect($nullable) |
223 | | - ->toBeString() |
224 | | - ->toBe('nullable'); |
225 | | - |
226 | | - $options = $rules[1]; |
227 | | - $optionsString = $options->__toString(); |
228 | | - |
229 | | - expect($options)->toBeInstanceOf(\Illuminate\Validation\Rules\In::class); |
230 | | - expect($optionsString) |
231 | | - ->toBeString() |
232 | | - ->toBe('in:"frodo","sam","merry","pippin"'); |
| 211 | + ->toHaveCount(2) |
| 212 | + ->toEqual([new In(['frodo', 'sam', 'merry', 'pippin']), 'nullable']); |
233 | 213 | }); |
0 commit comments