Conditions | 1 |
Paths | 1 |
Total Lines | 86 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
126 | public function testFormValidationForRequiredAndNotRequired() |
||
127 | { |
||
128 | /* |
||
129 | * Not required element not filled |
||
130 | */ |
||
131 | |||
132 | $form = ElementFactory::create('FORM', [ |
||
133 | "action" => 'someurl', |
||
134 | "method" => 'post' |
||
135 | ], [ |
||
136 | "input" => [ |
||
137 | "username" => [ |
||
138 | "type" => 'text', |
||
139 | "maxlength" => 15, |
||
140 | "minlength" => 5, |
||
141 | "required" => false # could be ommited, this is the default behaviour |
||
142 | ], |
||
143 | ] |
||
144 | ]); |
||
145 | |||
146 | $validator = new FormValidator($form, 'en'); |
||
147 | $validator->validate(); |
||
148 | |||
149 | # testing validation, no element has been declared as 'required' |
||
150 | $this->assertTrue($validator->isValid()); |
||
151 | |||
152 | $errors = $validator->getErrors(); |
||
153 | $this->assertEquals(0, count($errors)); |
||
154 | |||
155 | /* |
||
156 | * Required element not filled |
||
157 | */ |
||
158 | |||
159 | $form = ElementFactory::create('FORM', [ |
||
160 | "action" => 'someurl', |
||
161 | "method" => 'post' |
||
162 | ], [ |
||
163 | "input" => [ |
||
164 | "username" => [ |
||
165 | "type" => 'text', |
||
166 | "maxlength" => 15, |
||
167 | "minlength" => 5, |
||
168 | "required" => true |
||
169 | ], |
||
170 | ] |
||
171 | ]); |
||
172 | |||
173 | $validator = new FormValidator($form, 'en'); |
||
174 | $validator->validate(); |
||
175 | |||
176 | # testing validation, the element has been declared as 'required' |
||
177 | $this->assertNotTrue($validator->isValid()); |
||
178 | |||
179 | $errors = $validator->getErrors(); |
||
180 | $this->assertEquals(1, count($errors)); |
||
181 | |||
182 | /* |
||
183 | * Not Required element filled |
||
184 | */ |
||
185 | |||
186 | $form = ElementFactory::create('FORM', [ |
||
187 | "action" => 'someurl', |
||
188 | "method" => 'post' |
||
189 | ], [ |
||
190 | "input" => [ |
||
191 | "username" => [ |
||
192 | "type" => 'text', |
||
193 | "maxlength" => 15, |
||
194 | "minlength" => 5, |
||
195 | "required" => false |
||
196 | ], |
||
197 | ] |
||
198 | ]); |
||
199 | |||
200 | $form->fill([ |
||
201 | "username" => 'jobs', # wrong because of minlength attr |
||
202 | ]); |
||
203 | |||
204 | $validator = new FormValidator($form, 'en'); |
||
205 | $validator->validate(); |
||
206 | |||
207 | # testing validation, no element has been declared as 'required' but is fille, so it'll be validated |
||
208 | $this->assertNotTrue($validator->isValid()); |
||
209 | |||
210 | $errors = $validator->getErrors(); |
||
211 | $this->assertEquals(1, count($errors)); |
||
212 | } |
||
329 | } |