Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
5 | class EmailTest extends FormTestCase { |
||
6 | |||
7 | /** @test */ |
||
8 | public function it_can_be_rendered() |
||
9 | { |
||
10 | $form = $this->form(); |
||
11 | $form->email('foo'); |
||
12 | $this->assertContains('<input name="foo" value=""', $form->render()); |
||
13 | } |
||
14 | |||
15 | /** @test */ |
||
16 | public function it_renders_a_default_value() |
||
17 | { |
||
18 | $form = $this->form(); |
||
19 | $form->email('foo')->default('bar'); |
||
20 | $this->assertContains('<input name="foo" value="bar"', $form->render()); |
||
21 | } |
||
22 | |||
23 | /** @test */ |
||
24 | public function it_renders_model_values() |
||
25 | { |
||
26 | $form = $this->form(); |
||
27 | $form->defaults($this->model(['foo'=>'bar'])); |
||
28 | $form->email('foo'); |
||
29 | $this->assertContains('<input name="foo" value="bar"', $form->render()); |
||
30 | } |
||
31 | |||
32 | /** @test */ |
||
33 | public function it_can_fill_model_values() |
||
34 | { |
||
35 | $model = $this->model(['foo'=>'']); |
||
36 | |||
37 | $form = $this->form(); |
||
38 | $form->email('foo')->default('bar'); |
||
39 | $form->fill($model); |
||
40 | |||
41 | $form->assertEquals($model->foo, 'bar'); |
||
42 | } |
||
43 | |||
44 | /** @test */ |
||
45 | public function it_escapes_value() |
||
46 | { |
||
47 | $form = $this->form(); |
||
48 | $form->email('foo')->default('bar&'); |
||
49 | $this->assertContains('<input name="foo" value="bar&"', $form->render()); |
||
50 | } |
||
51 | |||
52 | /** @test */ |
||
53 | public function it_provides_expected_values() |
||
54 | { |
||
55 | $form = $this->form(); |
||
56 | $form->email('foo'); |
||
57 | |||
58 | $this->assertSame($form->get('foo'), ''); |
||
59 | } |
||
60 | |||
61 | /** @test */ |
||
62 | public function it_provides_expected_default_values() |
||
63 | { |
||
64 | $form = $this->form(); |
||
65 | $form->email('foo')->default('[email protected]'); |
||
66 | |||
67 | $this->assertSame($form->get('foo'), '[email protected]'); |
||
68 | } |
||
69 | |||
70 | /** @test */ |
||
71 | public function it_validates_required() |
||
72 | { |
||
73 | $this->assertValid(function($form) { $form->email('foo'); }); |
||
74 | |||
75 | $this->assertNotValid(function($form) { $form->email('foo')->required(); }); |
||
76 | } |
||
77 | |||
78 | /** @test */ |
||
79 | public function it_validates_email() |
||
80 | { |
||
81 | $this->assertValid(function($form) { $form->email('foo')->default('[email protected]'); }); |
||
82 | $this->assertValid(function($form) { $form->email('foo')->default('bar\'[email protected]'); }); |
||
83 | |||
84 | $this->assertNotValid(function($form) { $form->email('foo')->default('bar@invalid'); }); |
||
85 | $this->assertNotValid(function($form) { $form->email('foo')->default('bar@inva\'lid.com'); }); |
||
86 | } |
||
87 | |||
88 | } |
||
89 |