Repository navigation
Expand file tree
/
Copy pathFormConfiguringHookTest.php
More file actions
109 lines (86 loc) · 3.31 KB
/
Copy pathFormConfiguringHookTest.php
File metadata and controls
109 lines (86 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Model;
use NyonCode\WireCore\Core\Plugin\Hooks\FormConfiguringPayload;
use NyonCode\WireCore\Core\Plugin\PluginManager;
use NyonCode\WireCore\Foundation\Enums\Hook;
use NyonCode\WireForms\Components\TextInput;
use NyonCode\WireForms\Forms\Form;
/*
* The half of the additive story forms did not have.
*
* `table.configuring` let a plugin add a column to a list it does not own; there
* was no counterpart for the form beside it, so "adjust an installed module
* rather than fork it" stopped at the edge of the table.
*/
class FchInvoice extends Model
{
protected $table = 'fch_invoices';
}
/**
* The field names a form ends up built from — read through the config, which is
* where the hook runs.
*
* @return array<int, string>
*/
function fchSchema(Form $form): array
{
return array_map(
static fn (object $component): ?string => $component->getName(),
$form->getFlatComponents(),
);
}
it('lets a plugin add a field to a form it does not own', function () {
app(PluginManager::class)->hook(
Hook::FormConfiguring,
function (FormConfiguringPayload $payload): FormConfiguringPayload {
$payload->schema = [...$payload->schema, TextInput::make('internal_note')];
return $payload;
},
);
$form = Form::make()->schema([TextInput::make('number')]);
expect(fchSchema($form))->toBe(['number', 'internal_note']);
});
it('runs once per form, because the config it feeds is memoized', function () {
$calls = 0;
app(PluginManager::class)->hook(Hook::FormConfiguring, function (FormConfiguringPayload $payload) use (&$calls) {
$calls++;
return $payload;
});
$form = Form::make()->schema([TextInput::make('number')]);
fchSchema($form);
fchSchema($form);
expect($calls)->toBe(1);
});
it('scopes a callback to the model a form edits', function () {
// What makes this usable in an application with several modules installed:
// the callback belongs to one of them.
$touched = [];
app(PluginManager::class)->hook(
Hook::FormConfiguring,
function (FormConfiguringPayload $payload) use (&$touched): FormConfiguringPayload {
$touched[] = $payload->target?->model;
$payload->schema = [...$payload->schema, TextInput::make('internal_note')];
return $payload;
},
for: FchInvoice::class,
);
$invoiceForm = Form::make()->model(FchInvoice::class)->schema([TextInput::make('number')]);
$otherForm = Form::make()->schema([TextInput::make('number')]);
expect(fchSchema($invoiceForm))->toBe(['number', 'internal_note'])
->and(fchSchema($otherForm))->toBe(['number'])
->and($touched)->toBe([FchInvoice::class]);
});
it('builds its schema unchanged when nothing is bound to run hooks', function () {
// A form composed outside a booted application. Form::make() resolves
// through the container, so the container is swapped only once it exists.
$form = Form::make()->schema([TextInput::make('number')]);
$booted = Container::getInstance();
try {
Container::setInstance(new Container);
expect(fchSchema($form))->toBe(['number']);
} finally {
Container::setInstance($booted);
}
});