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 NumberTest extends FormTestCase { |
||
6 | |||
7 | /** @test */ |
||
8 | public function it_can_be_rendered() |
||
9 | { |
||
10 | $form = $this->form(); |
||
11 | $form->number('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->number('foo')->default('123'); |
||
20 | $this->assertContains('<input name="foo" value="123"', $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->number('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->number('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->number('foo')->default('123&'); |
||
49 | $this->assertContains('<input name="foo" value="123&"', $form->render()); |
||
50 | } |
||
51 | |||
52 | /** @test */ |
||
53 | public function it_provides_expected_values() |
||
54 | { |
||
55 | $form = $this->form(); |
||
56 | $form->number('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->number('foo')->default('123'); |
||
66 | |||
67 | $this->assertSame($form->get('foo'), '123'); |
||
68 | } |
||
69 | |||
70 | /** @test */ |
||
71 | public function it_validates_required() |
||
72 | { |
||
73 | $this->assertValid(function($form) { $form->number('foo'); }); |
||
74 | |||
75 | $this->assertNotValid(function($form) { $form->number('foo')->required(); }); |
||
76 | } |
||
77 | |||
78 | /** @test */ |
||
79 | public function it_validates_numeric() |
||
80 | { |
||
81 | $this->assertValid(function($form) { $form->number('foo')->default(123); }); |
||
82 | |||
83 | $this->assertNotValid(function($form) { $form->number('foo')->default('abc'); }); |
||
84 | } |
||
85 | |||
86 | /** @test */ |
||
87 | public function it_validates_between() |
||
88 | { |
||
89 | $this->assertValid(function($form) { $form->number('foo')->between(5, 7); }); |
||
90 | $this->assertValid(function($form) { $form->number('foo')->between(5, 7)->default('5'); }); |
||
91 | $this->assertValid(function($form) { $form->number('foo')->between(5, 7)->default('6'); }); |
||
92 | $this->assertValid(function($form) { $form->number('foo')->between(5, 7)->default('7'); }); |
||
93 | |||
94 | $this->assertNotValid(function($form) { $form->number('foo')->between(5, 7)->required(); }); |
||
95 | $this->assertNotValid(function($form) { $form->number('foo')->between(5, 7)->default('3'); }); |
||
96 | $this->assertNotValid(function($form) { $form->number('foo')->between(5, 7)->default('12'); }); |
||
97 | } |
||
98 | |||
99 | /** @test */ |
||
100 | public function it_validates_min() |
||
101 | { |
||
102 | $this->assertValid(function($form) { $form->number('foo')->min(5); }); |
||
103 | $this->assertValid(function($form) { $form->number('foo')->min(5)->default(5); }); |
||
104 | $this->assertValid(function($form) { $form->number('foo')->min(5)->default(10); }); |
||
105 | |||
106 | $this->assertNotValid(function($form) { $form->number('foo')->min(5)->required(); }); |
||
107 | $this->assertNotValid(function($form) { $form->number('foo')->min(5)->default(3); }); |
||
108 | $this->assertNotValid(function($form) { $form->number('foo')->min(5)->default(-5); }); |
||
109 | } |
||
110 | |||
111 | /** @test */ |
||
112 | public function it_validates_max() |
||
113 | { |
||
114 | $this->assertValid(function($form) { $form->number('foo')->max(5); }); |
||
115 | $this->assertValid(function($form) { $form->number('foo')->max(5)->default(3); }); |
||
116 | $this->assertValid(function($form) { $form->number('foo')->max(5)->default(5); }); |
||
117 | |||
118 | $this->assertNotValid(function($form) { $form->number('foo')->max(5)->required(); }); |
||
119 | $this->assertNotValid(function($form) { $form->number('foo')->max(5)->default(5.1); }); |
||
120 | $this->assertNotValid(function($form) { $form->number('foo')->max(5)->default(10); }); |
||
121 | $this->assertNotValid(function($form) { $form->number('foo')->max(5)->default(12345); }); |
||
122 | } |
||
123 | |||
124 | /** @test */ |
||
125 | public function it_validates_integer() |
||
126 | { |
||
127 | $this->assertValid(function($form) { $form->number('foo')->integer(); }); |
||
128 | $this->assertValid(function($form) { $form->number('foo')->integer()->default(4); }); |
||
129 | $this->assertValid(function($form) { $form->number('foo')->integer()->default('4'); }); |
||
130 | |||
131 | $this->assertNotValid(function($form) { $form->number('foo')->integer()->required(); }); |
||
132 | $this->assertNotValid(function($form) { $form->number('foo')->integer()->default(10.3); }); |
||
133 | $this->assertNotValid(function($form) { $form->number('foo')->integer()->default(1/3); }); |
||
134 | $this->assertNotValid(function($form) { $form->number('foo')->integer()->default('10.2'); }); |
||
135 | } |
||
136 | |||
137 | /** @test */ |
||
138 | public function it_validates_in() |
||
139 | { |
||
140 | $this->assertValid(function($form) { $form->number('foo')->in(); }); |
||
141 | $this->assertValid(function($form) { $form->number('foo')->in([1, 2, 3]); }); |
||
142 | $this->assertValid(function($form) { $form->number('foo')->in([1, 2, 3])->default(1); }); |
||
143 | |||
144 | $this->assertNotValid(function($form) { $form->number('foo')->in([1, 2, 3])->required(); }); |
||
145 | $this->assertNotValid(function($form) { $form->number('foo')->in([1, 2, 3])->default(4); }); |
||
146 | } |
||
147 | |||
148 | /** @test */ |
||
149 | public function it_validates_not_in() |
||
150 | { |
||
151 | $this->assertValid(function($form) { $form->number('foo')->not_in(); }); |
||
152 | $this->assertValid(function($form) { $form->number('foo')->not_in([1, 2, 3]); }); |
||
153 | $this->assertValid(function($form) { $form->number('foo')->not_in([1, 2, 3])->default(4); }); |
||
154 | |||
155 | $this->assertNotValid(function($form) { $form->number('foo')->not_in([1, 2, 3])->required(); }); |
||
156 | $this->assertNotValid(function($form) { $form->number('foo')->not_in([1, 2, 3])->default(1); }); |
||
157 | } |
||
158 | |||
159 | } |
||
160 |