-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathFieldBuilderTest.php
More file actions
396 lines (344 loc) · 10 KB
/
FieldBuilderTest.php
File metadata and controls
396 lines (344 loc) · 10 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
namespace Styde\Html\Tests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\View;
use Illuminate\Support\MessageBag;
use Styde\Html\Facades\Field;
use Styde\Html\Facades\Form;
class FieldBuilderTest extends TestCase
{
/** @test */
function it_generates_a_text_field()
{
$this->assertTemplateMatches(
'field/text',
Field::text('name', 'value')
);
}
/** @test */
function it_generates_a_required_text_field()
{
$this->assertTemplateMatches(
'field/text-required',
Field::text('name', ['required' => true])
);
}
/** @test */
function it_generates_an_optional_text_field()
{
$this->assertTemplateMatches(
'field/text-not-required',
Field::text('name', ['required' => false])
);
}
/** @test */
function it_generates_a_required_password_field()
{
$this->assertTemplateMatches(
'field/password-required',
Field::password('password')->required()
);
}
/** @test */
public function it_generates_a_text_field_with_a_custom_label()
{
$this->assertTemplateMatches(
'field/text-custom-label',
Field::text('name', 'value', ['label' => 'Full name'])
);
}
/** @test */
public function it_generates_a_text_field_with_an_help_text()
{
$this->assertTemplateMatches(
'field/text-help-text',
Field::text('name', 'value', ['helpText' => 'This is a help text for the field'])
);
}
/** @test */
public function it_generates_a_text_field_with_a_raw_help_text()
{
$this->assertTemplateMatches(
'field/text-raw-help-text',
Field::text('name', 'value')->rawHelpText('This is a text with a <a href="#">link</a>')
);
}
/** @test */
public function it_generates_a_text_field_with_a_custom_id()
{
$this->assertTemplateMatches(
'field/text-custom-id',
Field::text('name', 'value', ['id' => 'custom_id'])
);
}
/** @test */
public function it_generates_a_select_field()
{
trans()->addLines([
'validation.empty_option.default' => 'Select value',
], 'en');
$this->assertTemplateMatches(
'field/select',
Field::select('gender', ['m' => 'Male', 'f' => 'Female'])
);
}
/** @test */
public function it_generates_a_select_field_with_custom_trans_empty()
{
trans()->addLines([
'validation.empty_option.gender' => 'Select gender',
], 'en');
$this->assertTemplateMatches(
'field/select-empty',
Field::select('gender', ['m' => 'Male', 'f' => 'Female'])
);
}
/** @test */
function it_adds_an_empty_option_to_select_fields()
{
$this->assertTemplateMatches(
'field/select-empty',
Field::select('gender', ['m' => 'Male', 'f' => 'Female'], ['empty' => 'Select gender'])
);
}
/** @test */
function it_generates_a_multiple_select_field()
{
$options = [
'php' => 'PHP',
'laravel' => 'Laravel',
'symfony' => 'Symfony',
'ruby' => 'Ruby on Rails'
];
$this->assertTemplateMatches(
'field/select-multiple',
Field::select('tags', $options, ['php', 'laravel'], ['multiple'])
);
$this->assertTemplateMatches(
'field/select-multiple',
Field::selectMultiple('tags', $options, ['php', 'laravel'])
);
}
/** @test */
function it_generates_a_multiple_select_field_with_optgroup()
{
$options = [
'backend' => [
'laravel' => 'Laravel',
'rails' => 'Ruby on Rails',
],
'frontend' => [
'vue' => 'Vue',
'angular' => 'Angular',
],
];
$this->assertTemplateMatches(
'field/select-group',
Field::selectMultiple('frameworks', $options, ['vue', 'laravel'])
);
}
/** @test */
function it_generates_a_text_field_with_errors()
{
tap(app('session.store'), function ($session) {
$session->put('errors', new MessageBag([
'name' => ['This is really wrong']
]));
Field::setSessionStore($session);
});
$this->assertTemplateMatches(
'field/text_with_errors',
Field::text('name')
);
}
/** @test */
function it_generates_checkboxes()
{
$tags = [
'php' => 'PHP',
'python' => 'Python',
'js' => 'JS',
'ruby' => 'Ruby on Rails'
];
$checked = ['php', 'js'];
$this->assertTemplateMatches(
'field/checkboxes',
Field::checkboxes('tags', $tags, $checked)
);
}
/** @test */
function it_generates_checkboxes_with_help_text()
{
$tags = [
'php' => 'PHP',
'python' => 'Python',
'js' => 'JS',
'ruby' => 'Ruby on Rails'
];
$checked = ['php', 'js'];
$this->assertTemplateMatches(
'field/checkboxes_help_text',
Field::checkboxes('tags', $tags, $checked)->helpText('This is a help text')->required()
);
}
/** @test */
function it_generates_radios()
{
$this->assertTemplateMatches(
'field/radios',
Field::radios('gender', ['m' => 'Male', 'f' => 'Female'], 'm')
);
}
/** @test */
function it_generates_an_input_field_with_label()
{
$this->assertTemplateMatches(
'field/input',
Field::input('text', 'profession', 'developer')
);
}
/** @test */
function it_generates_an_email_field()
{
$this->assertTemplateMatches(
'field/email',
);
}
/** @test */
function it_generates_an_url_field()
{
$this->assertTemplateMatches(
'field/url',
Field::url('site', 'https://styde.net')
);
}
/** @test */
function it_generates_a_file_field()
{
$this->assertTemplateMatches(
'field/file',
Field::file('myFile')
);
}
/** @test */
function it_generates_a_textarea_field()
{
$this->assertTemplateMatches(
'field/textarea',
Field::textarea('address', '742 Evergreen Terrace', ['rows' => 2, 'cols' => 20])
);
}
/** @test */
function it_generates_an_hidden_field()
{
$this->assertTemplateMatches(
'field/hidden',
Field::hidden('_token', 'some-random-token')
);
}
/** @test */
function it_generates_a_checkbox_field()
{
$this->assertTemplateMatches(
'field/checkbox',
Field::checkbox('remember_me')
);
}
/** @test */
function field_can_have_a_custom_template()
{
View::addLocation(__DIR__.'/views');
$this->assertTemplateMatches(
'field/text-custom-template',
Field::text('name', 'value')->template('custom-templates.field-text')
);
}
/** @test */
function it_can_customize_the_template_by_attributes_array()
{
View::addLocation(__DIR__.'/views');
$this->assertTemplateMatches(
'field/text-custom-template',
Field::text('name', 'value', ['template' => 'custom-templates.field-text'])
);
}
/** @test */
function it_generates_a_field_with_dots_name()
{
$this->assertTemplateMatches(
'field/text-dots-name',
Field::text('student.name')
);
}
/** @test */
function it_can_add_labels_with_html()
{
$this->assertTemplateMatches(
'field/text-with-raw-label',
Field::text('name', 'value')->rawLabel('Label with <strong>HTML</strong>')
);
}
/** @test */
function it_adds_a_select_field_with_options_from_model()
{
$post = new class extends Model {
public function getCategoryOptions()
{
return ['general' => 'General', 'random' => 'Random'];
}
};
Form::setCurrentModel($post);
$this->assertTemplateMatches(
'field/select-from-model',
Field::select('category', [], null, ['empty' => 'Select category'])
);
}
/** @test */
function it_adds_a_select_field_with_options_in_wrong_name_method()
{
$post = new class extends Model {
public function getUserIdOptions()
{
return ['1' => 'John', '2' => 'Mary'];
}
};
Form::setCurrentModel($post);
$this->assertTemplateMatches(
'field/select-without-options-from-model',
Field::select('user', [], null, ['empty' => 'Select user'])
);
}
/** @test */
function it_adds_a_select_field_from_model_without_options()
{
$this->assertTemplateMatches(
'field/select-without-options-from-model',
Field::select('user', [], null, ['empty' => 'Select user'])
);
}
/** @test */
function renders_control_only()
{
$this->assertHtmlEquals(
'<input type="text" name="first_name">',
Field::text('first_name')->controlOnly()
);
}
/** @test */
function renders_as_full_field()
{
$this->assertTemplateMatches(
'field/text',
Field::text('name', 'value')->controlOnly()->fullField()
);
}
/** @test */
function it_is_macroable()
{
Field::macro('myCustomField', function () {
return 'my-custom-field';
});
$this->assertSame('my-custom-field', Field::myCustomField());
}
}