1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helmut\Forms\Testing; |
4
|
|
|
|
5
|
|
|
class NameTest extends FormTestCase { |
6
|
|
|
|
7
|
|
|
/** @test */ |
8
|
|
View Code Duplication |
public function it_can_be_rendered() |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
$form = $this->form(); |
11
|
|
|
$form->name('foo'); |
|
|
|
|
12
|
|
|
$this->assertContains('<input name="foo_first" value=""', $form->render()); |
13
|
|
|
$this->assertContains('<input name="foo_surname" value=""', $form->render()); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** @test */ |
17
|
|
View Code Duplication |
public function it_renders_default_values() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$form = $this->form(); |
20
|
|
|
$form->name('foo')->default(['first'=>'bar', 'surname'=>'baz']); |
|
|
|
|
21
|
|
|
$this->assertContains('<input name="foo_first" value="bar"', $form->render()); |
22
|
|
|
$this->assertContains('<input name="foo_surname" value="baz"', $form->render()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** @test */ |
26
|
|
|
public function it_renders_model_values() |
27
|
|
|
{ |
28
|
|
|
$form = $this->form(); |
29
|
|
|
$form->defaults($this->model(['foo'=>'bar baz', 'foo_first'=>'bar', 'foo_surname'=>'baz'])); |
30
|
|
|
$form->name('foo'); |
|
|
|
|
31
|
|
|
$this->assertContains('<input name="foo_first" value="bar"', $form->render()); |
32
|
|
|
$this->assertContains('<input name="foo_surname" value="baz"', $form->render()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** @test */ |
36
|
|
|
public function it_can_fill_model_values() |
37
|
|
|
{ |
38
|
|
|
$model = $this->model(['foo'=>'', 'foo_first'=>'', 'foo_surname'=>'']); |
39
|
|
|
|
40
|
|
|
$form = $this->form(); |
41
|
|
|
$form->name('foo')->default(['first'=>'bar', 'surname'=>'baz']); |
|
|
|
|
42
|
|
|
$form->fill($model); |
43
|
|
|
|
44
|
|
|
$form->assertEquals($model->foo, 'bar baz'); |
|
|
|
|
45
|
|
|
$form->assertEquals($model->foo_first, 'bar'); |
|
|
|
|
46
|
|
|
$form->assertEquals($model->foo_surname, 'baz'); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @test */ |
50
|
|
View Code Duplication |
public function it_escapes_values() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$form = $this->form(); |
53
|
|
|
$form->name('foo')->default(['first'=>'bar&', 'surname'=>'baz&']); |
|
|
|
|
54
|
|
|
$this->assertContains('<input name="foo_first" value="bar&"', $form->render()); |
55
|
|
|
$this->assertContains('<input name="foo_surname" value="baz&"', $form->render()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @test */ |
59
|
|
View Code Duplication |
public function it_provides_expected_values() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$form = $this->form(); |
62
|
|
|
$form->name('foo'); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$expected = ['foo_first'=>'', 'foo_surname'=>'', 'foo'=>'']; |
65
|
|
|
$this->assertSame($form->get('foo'), $expected); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @test */ |
69
|
|
|
public function it_provides_expected_default_values() |
70
|
|
|
{ |
71
|
|
|
$form = $this->form(); |
72
|
|
|
$form->name('foo')->default(['first'=>'bar', 'surname'=>'baz']); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$expected = ['foo_first'=>'bar', 'foo_surname'=>'baz', 'foo'=>'bar baz']; |
75
|
|
|
$this->assertSame($form->get('foo'), $expected); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @test */ |
79
|
|
|
public function it_validates_required() |
80
|
|
|
{ |
81
|
|
|
$this->assertValid(function($form) { $form->name('foo'); }); |
82
|
|
|
$this->assertValid(function($form) { $form->name('foo')->default(['first'=>'bar', 'surname'=>'baz'])->required(); }); |
83
|
|
|
|
84
|
|
|
$this->assertNotValid(function($form) { $form->name('foo')->required(); }); |
85
|
|
|
$this->assertNotValid(function($form) { $form->name('foo')->default(['first'=>'bar'])->required(); }); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.