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 CheckboxTest extends FormTestCase { |
||
6 | |||
7 | /** @test */ |
||
8 | public function it_can_be_rendered() |
||
9 | { |
||
10 | $form = $this->form(); |
||
11 | $form->checkbox('foo'); |
||
12 | $this->assertContains('<input name="foo" type="checkbox"', $form->render()); |
||
13 | } |
||
14 | |||
15 | /** @test */ |
||
16 | public function it_is_unchecked_by_default() |
||
17 | { |
||
18 | $form = $this->form(); |
||
19 | $form->checkbox('foo'); |
||
20 | $this->assertNotContains('checked="checked"', $form->render()); |
||
21 | } |
||
22 | |||
23 | /** @test */ |
||
24 | public function it_can_be_checked() |
||
25 | { |
||
26 | $form = $this->form(); |
||
27 | $form->checkbox('foo')->checked(); |
||
28 | $this->assertContains('checked="checked"', $form->render()); |
||
29 | } |
||
30 | |||
31 | /** @test */ |
||
32 | public function it_renders_model_values() |
||
33 | { |
||
34 | $form = $this->form(); |
||
35 | $form->defaults($this->model(['foo'=>1])); |
||
36 | $form->checkbox('foo'); |
||
37 | $this->assertContains('checked="checked"', $form->render()); |
||
38 | } |
||
39 | |||
40 | /** @test */ |
||
41 | public function it_can_fill_model_values() |
||
42 | { |
||
43 | $model = $this->model(['foo'=>'']); |
||
44 | |||
45 | $form = $this->form(); |
||
46 | $form->checkbox('foo')->checked(); |
||
47 | $form->fill($model); |
||
48 | |||
49 | $form->assertEquals($model->foo, 1); |
||
50 | } |
||
51 | |||
52 | /** @test */ |
||
53 | public function it_can_be_unchecked() |
||
54 | { |
||
55 | $form = $this->form(); |
||
56 | $form->checkbox('foo')->checked()->unchecked(); |
||
57 | $this->assertNotContains('checked="checked"', $form->render()); |
||
58 | } |
||
59 | |||
60 | /** @test */ |
||
61 | public function it_provides_expected_values() |
||
62 | { |
||
63 | $form = $this->form(); |
||
64 | $form->checkbox('foo'); |
||
65 | |||
66 | $this->assertFalse($form->get('foo')); |
||
67 | } |
||
68 | |||
69 | /** @test */ |
||
70 | public function it_provides_expected_default_values() |
||
71 | { |
||
72 | $form = $this->form(); |
||
73 | $form->checkbox('foo')->checked(); |
||
74 | |||
75 | $this->assertTrue($form->get('foo')); |
||
76 | } |
||
77 | |||
78 | /** @test */ |
||
79 | public function it_validates_required() |
||
80 | { |
||
81 | $this->assertValid(function($form) { $form->checkbox('foo'); }); |
||
82 | $this->assertValid(function($form) { $form->checkbox('foo')->checked()->required(); }); |
||
83 | |||
84 | $this->assertNotValid(function($form) { $form->checkbox('foo')->required(); }); |
||
85 | $this->assertNotValid(function($form) { $form->checkbox('foo')->checked()->unchecked()->required(); }); |
||
86 | } |
||
87 | |||
88 | } |
||
89 |