Skip to content

Commit 071e715

Browse files
committed
Unit tests
1 parent 00739ef commit 071e715

13 files changed

+431
-393
lines changed

docs/guide/upgrading/9.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ class PostForm extends SharpForm
517517

518518
## SharpFormAutocompleteFormField was split in two subclasses for local and remote cases
519519

520+
REWRITE THIS WITH:
521+
- no more `setAdditionalTemplateData`
522+
-
523+
520524
This isn’t a breaking change, since `SharpFormAutocompleteFormField::make($key, $mode)` is still supported, but deprecated. You should migrate to the new `SharpFormAutocompleteLocalField` and `SharpFormAutocompleteRemoteField` classes.
521525

522526
In 8.x:

src/Form/Fields/Formatters/AutocompleteLocalFormatter.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,6 @@ public function toFront(SharpFormField $field, $value)
2020
return is_null($value) || is_array($value)
2121
? $value
2222
: [$field->itemIdAttribute() => $value];
23-
24-
// if(is_null($value)) {
25-
// return null;
26-
// }
27-
//
28-
// if(is_array($value)) {
29-
// $selectedValue = isset($value[$field->itemIdAttribute()])
30-
// ? collect($field->localValues())
31-
// ->firstWhere($field->itemIdAttribute(), $value[$field->itemIdAttribute()])
32-
// : null;
33-
// } else {
34-
// $selectedValue = collect($field->localValues())->firstWhere($field->itemIdAttribute(), $value);
35-
// }
36-
//
37-
// return $selectedValue
38-
// ? [
39-
// ...$selectedValue,
40-
// '_html' => $field->renderResultItem($selectedValue),
41-
// ]
42-
// : null;
4323
}
4424

4525
public function fromFront(SharpFormField $field, string $attribute, $value)

src/Form/Fields/Formatters/AutocompleteRemoteFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public function toFront(SharpFormField $field, $value)
1717
{
1818
$value = ArrayConverter::modelToArray($value);
1919

20-
if(is_null($value)) {
20+
if (is_null($value)) {
2121
return null;
2222
}
2323

24-
if(is_array($value)) {
24+
if (is_array($value)) {
2525
return $field->itemWithRenderedTemplates($value);
2626
}
2727

src/Form/Fields/Utils/IsSharpFormAutocompleteField.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
interface IsSharpFormAutocompleteField
66
{
77
public function setItemIdAttribute(string $itemIdAttribute): self;
8-
public function setAdditionalTemplateData(array $data): self;
98
public function isRemote(): bool;
109
public function isLocal(): bool;
1110
public function itemIdAttribute(): string;

src/Form/Fields/Utils/SharpFormAutocompleteCommonField.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ trait SharpFormAutocompleteCommonField
2222

2323
public function itemWithRenderedTemplates(array $item): array
2424
{
25+
$resultItem = $this->resultItemTemplate
26+
? ['_htmlResult' => $this->renderResultItem($item)]
27+
: [];
28+
2529
return [
2630
...$item,
2731
'_html' => $this->listItemTemplate
2832
? $this->renderListItem($item)
2933
: ($item['label'] ?? $item[$this->itemIdAttribute] ?? null),
30-
'_htmlResult' => $this->resultItemTemplate ? $this->renderResultItem($item) : null,
34+
...$resultItem,
3135
];
3236
}
3337

@@ -70,13 +74,6 @@ public function renderResultItem(array $data): string
7074
return $this->resultItemTemplate->with($data)->render();
7175
}
7276

73-
public function setAdditionalTemplateData(array $data): self
74-
{
75-
// TODO keep this ?
76-
77-
return $this;
78-
}
79-
8077
public function isRemote(): bool
8178
{
8279
return $this->mode === 'remote';
@@ -97,8 +94,6 @@ protected function validationRulesBase(): array
9794
return [
9895
'mode' => 'required|in:local,remote',
9996
'itemIdAttribute' => 'required',
100-
// 'listItemTemplate' => 'required',
101-
// 'resultItemTemplate' => 'required',
10297
'templateData' => 'nullable|array',
10398
];
10499
}
@@ -110,8 +105,6 @@ protected function toArrayBase(): array
110105
'mode' => $this->mode,
111106
'placeholder' => $this->placeholder,
112107
'itemIdAttribute' => $this->itemIdAttribute,
113-
'listItemTemplate' => $this->listItemTemplate,
114-
'resultItemTemplate' => $this->resultItemTemplate,
115108
'localized' => $this->localized,
116109
],
117110
$this->dynamicAttributes

src/Form/Fields/Utils/SharpFormFieldWithOptions.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ protected static function formatOptions(array|Collection $options, string $idAtt
2020

2121
if (is_array($firstOption) && isset($firstOption[$idAttribute])) {
2222
// We assume that we already have ["id", "label"] in this case
23-
return $options
24-
->when($format)->map($format)
25-
->all();
23+
return $options->map($format)->values()->all();
2624
}
2725

2826
// Simple [key => value] array case
2927
return $options
3028
->map(fn ($label, $id) => compact('id', 'label'))
31-
->when($format)->map($format)
29+
->map($format)
30+
->values()
3231
->all();
3332
}
3433

@@ -38,6 +37,8 @@ protected static function formatDynamicOptions(array|Collection &$options, int $
3837
return [];
3938
}
4039

40+
$format ??= fn ($option) => $option;
41+
4142
return collect($options)
4243
->map(function ($values) use ($depth, $format) {
4344
if ($depth > 1) {
@@ -46,7 +47,7 @@ protected static function formatDynamicOptions(array|Collection &$options, int $
4647

4748
return collect($values)
4849
->map(fn ($label, $id) => compact('id', 'label'))
49-
->when($format)->map($format)
50+
->map($format)
5051
->values()
5152
->all();
5253
})

src/Http/Controllers/Api/ApiAutocompleteController.php renamed to src/Http/Controllers/Api/ApiFormAutocompleteController.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
use Code16\Sharp\Form\Fields\SharpFormAutocompleteRemoteField;
66
use Code16\Sharp\Utils\Transformers\ArrayConverter;
77
use Illuminate\Http\Request;
8-
use Illuminate\Routing\Router;
98
use Illuminate\Support\Arr;
10-
use Illuminate\Support\Facades\Http;
119
use Illuminate\Validation\ValidationException;
1210

13-
class ApiAutocompleteController extends ApiController
11+
class ApiFormAutocompleteController extends ApiController
1412
{
1513
public function index(string $entityKey, string $autocompleteFieldKey)
1614
{
@@ -25,18 +23,18 @@ public function index(string $entityKey, string $autocompleteFieldKey)
2523

2624
$field = $form->findFieldByKey($autocompleteFieldKey);
2725

28-
if(!$field instanceof SharpFormAutocompleteRemoteField) {
26+
if (!$field instanceof SharpFormAutocompleteRemoteField) {
2927
throw ValidationException::withMessages([
3028
'autocompleteFieldKey' => 'Unknown remote autocomplete field : '.$autocompleteFieldKey,
3129
]);
3230
}
3331

3432
request()->validate([
3533
'endpoint' => ['nullable', 'starts_with:'.str($field->remoteEndpoint())->before('{{')],
36-
'search' => ['required', 'string'],
34+
'search' => ['nullable', 'string'],
3735
]);
3836

39-
if($callback = $field->getQueryResultsCallback()) {
37+
if ($callback = $field->getQueryResultsCallback()) {
4038
$data = collect($callback(request()->input('search')))->map(fn ($record) => ArrayConverter::modelToArray($record));
4139
} else {
4240
// if($field->isExternalEndpoint()) {
@@ -57,7 +55,7 @@ public function index(string $entityKey, string $autocompleteFieldKey)
5755
// } else {
5856
$response = app()->handle(
5957
tap(Request::create(
60-
uri: url($field->remoteEndpoint()),
58+
uri: url($field->remoteEndpoint()), // TODO allow full URLs (route(...)) = check if route exists
6159
method: $field->remoteMethod(),
6260
parameters: [
6361
$field->remoteSearchAttribute() => request()->input('search'),

src/routes/api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
use Code16\Sharp\Http\Controllers\Api\ApiAutocompleteController;
43
use Code16\Sharp\Http\Controllers\Api\ApiEntityListController;
54
use Code16\Sharp\Http\Controllers\Api\ApiEntityListFiltersController;
5+
use Code16\Sharp\Http\Controllers\Api\ApiFormAutocompleteController;
66
use Code16\Sharp\Http\Controllers\Api\ApiFormEditorUploadFormController;
77
use Code16\Sharp\Http\Controllers\Api\ApiFormUploadController;
88
use Code16\Sharp\Http\Controllers\Api\ApiFormUploadThumbnailController;
@@ -93,6 +93,6 @@
9393
Route::post('/upload', [ApiFormUploadController::class, 'store'])
9494
->name('code16.sharp.api.form.upload');
9595

96-
Route::get('/autocomplete/{entityKey}/{autocompleteFieldKey}', [ApiAutocompleteController::class, 'index'])
96+
Route::get('/form/autocomplete/{entityKey}/{autocompleteFieldKey}', [ApiFormAutocompleteController::class, 'index'])
9797
->name('code16.sharp.api.form.autocomplete.index');
9898
});

tests/Unit/Form/Fields/Formatters/AutocompleteFormatterTest.php renamed to tests/Unit/Form/Fields/Formatters/AutocompleteLocalFormatterTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use Code16\Sharp\Form\Fields\Formatters\AutocompleteLocalFormatter;
44
use Code16\Sharp\Form\Fields\SharpFormAutocompleteLocalField;
5-
use Code16\Sharp\Form\Fields\SharpFormAutocompleteRemoteField;
65
use Illuminate\Support\Str;
76

87
it('allows to format local value to front', function () {
@@ -47,20 +46,6 @@ public function toArray()
4746
expect($toFront)->toEqual(['id' => $value]);
4847
});
4948

50-
it('allows to format remote value to front', function () {
51-
$value = [
52-
'id' => Str::random(),
53-
'label' => Str::random(),
54-
];
55-
56-
$toFront = (new AutocompleteLocalFormatter)->toFront(
57-
SharpFormAutocompleteRemoteField::make('text'),
58-
$value,
59-
);
60-
61-
expect($toFront)->toEqual($value);
62-
});
63-
6449
it('allows to format null value to front', function () {
6550
expect(
6651
(new AutocompleteLocalFormatter)->toFront(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use Code16\Sharp\Form\Fields\Formatters\AutocompleteRemoteFormatter;
4+
use Code16\Sharp\Form\Fields\SharpFormAutocompleteRemoteField;
5+
use Illuminate\Support\Str;
6+
7+
it('allows to format remote value to front', function () {
8+
$value = [
9+
'id' => 1,
10+
'name' => 'Bob',
11+
'age' => 42,
12+
];
13+
14+
$toFront = (new AutocompleteRemoteFormatter)
15+
->toFront(
16+
SharpFormAutocompleteRemoteField::make('text')
17+
->setListItemTemplate('{{ $name }}, {{ $age }}')
18+
->setResultItemTemplate('{{ $name }}, {{ $age }}'),
19+
$value,
20+
);
21+
22+
expect($toFront)->toEqual([
23+
'id' => 1,
24+
'name' => 'Bob',
25+
'age' => 42,
26+
'_html' => 'Bob, 42',
27+
'_htmlResult' => 'Bob, 42',
28+
]);
29+
});
30+
31+
it('allows to format remote value from front', function () {
32+
// Front always send an object
33+
$value = [
34+
'id' => Str::random(),
35+
'label' => Str::random(),
36+
];
37+
38+
// Back always need an id
39+
expect(
40+
(new AutocompleteRemoteFormatter)->fromFront(
41+
SharpFormAutocompleteRemoteField::make('text'),
42+
'attribute',
43+
$value,
44+
)
45+
)->toBe($value['id']);
46+
});

0 commit comments

Comments
 (0)