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 TextTest extends FormTestCase { |
||
| 6 | |||
| 7 | /** @test */ |
||
| 8 | public function it_can_be_rendered() |
||
| 9 | { |
||
| 10 | $form = $this->form(); |
||
| 11 | $form->text('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->text('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->text('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->text('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->text('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->text('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->text('foo')->default('bar'); |
||
| 66 | |||
| 67 | $this->assertSame($form->get('foo'), 'bar'); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** @test */ |
||
| 71 | public function it_validates_required() |
||
| 72 | { |
||
| 73 | $this->assertValid(function($form) { $form->text('foo'); }); |
||
| 74 | |||
| 75 | $this->assertNotValid(function($form) { $form->text('foo')->required(); }); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** @test */ |
||
| 79 | public function it_validates_between() |
||
| 80 | { |
||
| 81 | $this->assertValid(function($form) { $form->text('foo')->between(5, 7); }); |
||
| 82 | $this->assertValid(function($form) { $form->text('foo')->between(5, 7)->default('123456'); }); |
||
| 83 | |||
| 84 | $this->assertNotValid(function($form) { $form->text('foo')->between(5, 7)->required(); }); |
||
| 85 | $this->assertNotValid(function($form) { $form->text('foo')->between(5, 7)->default('123'); }); |
||
| 86 | $this->assertNotValid(function($form) { $form->text('foo')->between(5, 7)->default('123456789'); }); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** @test */ |
||
| 90 | public function it_validates_min() |
||
| 91 | { |
||
| 92 | $this->assertValid(function($form) { $form->text('foo')->min(5); }); |
||
| 93 | $this->assertValid(function($form) { $form->text('foo')->min(5)->default('123456789'); }); |
||
| 94 | |||
| 95 | $this->assertNotValid(function($form) { $form->text('foo')->min(5)->required(); }); |
||
| 96 | $this->assertNotValid(function($form) { $form->text('foo')->min(5)->default('123'); }); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** @test */ |
||
| 100 | public function it_validates_max() |
||
| 101 | { |
||
| 102 | $this->assertValid(function($form) { $form->text('foo')->max(5); }); |
||
| 103 | $this->assertValid(function($form) { $form->text('foo')->max(5)->default('123'); }); |
||
| 104 | |||
| 105 | $this->assertNotValid(function($form) { $form->text('foo')->max(5)->required(); }); |
||
| 106 | $this->assertNotValid(function($form) { $form->text('foo')->max(5)->default('123456789'); }); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** @test */ |
||
| 110 | public function it_validates_alpha() |
||
| 111 | { |
||
| 112 | $this->assertValid(function($form) { $form->text('foo')->alpha(); }); |
||
| 113 | $this->assertValid(function($form) { $form->text('foo')->alpha()->default('valid'); }); |
||
| 114 | |||
| 115 | $this->assertNotValid(function($form) { $form->text('foo')->alpha()->required(); }); |
||
| 116 | $this->assertNotValid(function($form) { $form->text('foo')->alpha()->default('inv@lid'); }); |
||
| 117 | $this->assertNotValid(function($form) { $form->text('foo')->alpha()->default('123456'); }); |
||
| 118 | $this->assertNotValid(function($form) { $form->text('foo')->alpha()->default('abc123'); }); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** @test */ |
||
| 122 | public function it_validates_alpha_num() |
||
| 123 | { |
||
| 124 | $this->assertValid(function($form) { $form->text('foo')->alpha_num(); }); |
||
| 125 | $this->assertValid(function($form) { $form->text('foo')->alpha_num()->default('valid'); }); |
||
| 126 | $this->assertValid(function($form) { $form->text('foo')->alpha_num()->default('valid123'); }); |
||
| 127 | $this->assertValid(function($form) { $form->text('foo')->alpha_num()->default('123'); }); |
||
| 128 | |||
| 129 | $this->assertNotValid(function($form) { $form->text('foo')->alpha_num()->required(); }); |
||
| 130 | $this->assertNotValid(function($form) { $form->text('foo')->alpha_num()->default('valid 123'); }); |
||
| 131 | $this->assertNotValid(function($form) { $form->text('foo')->alpha_num()->default('valid_123'); }); |
||
| 132 | $this->assertNotValid(function($form) { $form->text('foo')->alpha_num()->default('inv@lid123'); }); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** @test */ |
||
| 136 | public function it_validates_alpha_dash() |
||
| 137 | { |
||
| 138 | $this->assertValid(function($form) { $form->text('foo')->alpha_dash(); }); |
||
| 139 | $this->assertValid(function($form) { $form->text('foo')->alpha_dash()->default('valid'); }); |
||
| 140 | $this->assertValid(function($form) { $form->text('foo')->alpha_dash()->default('valid123'); }); |
||
| 141 | $this->assertValid(function($form) { $form->text('foo')->alpha_dash()->default('valid-123'); }); |
||
| 142 | $this->assertValid(function($form) { $form->text('foo')->alpha_dash()->default('valid_123'); }); |
||
| 143 | $this->assertValid(function($form) { $form->text('foo')->alpha_dash()->default('123'); }); |
||
| 144 | |||
| 145 | $this->assertNotValid(function($form) { $form->text('foo')->alpha_dash()->required(); }); |
||
| 146 | $this->assertNotValid(function($form) { $form->text('foo')->alpha_dash()->default('valid 123'); }); |
||
| 147 | $this->assertNotValid(function($form) { $form->text('foo')->alpha_dash()->default('inv@lid123'); }); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** @test */ |
||
| 151 | public function it_validates_in() |
||
| 152 | { |
||
| 153 | $this->assertValid(function($form) { $form->text('foo')->in(); }); |
||
| 154 | $this->assertValid(function($form) { $form->text('foo')->in(['a', 'b','c']); }); |
||
| 155 | $this->assertValid(function($form) { $form->text('foo')->in(['a', 'b','c'])->default('a'); }); |
||
| 156 | |||
| 157 | $this->assertNotValid(function($form) { $form->text('foo')->in(['a', 'b','c'])->required(); }); |
||
| 158 | $this->assertNotValid(function($form) { $form->text('foo')->in(['a', 'b','c'])->default('d'); }); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** @test */ |
||
| 162 | public function it_validates_not_in() |
||
| 163 | { |
||
| 164 | $this->assertValid(function($form) { $form->text('foo')->not_in(); }); |
||
| 165 | $this->assertValid(function($form) { $form->text('foo')->not_in(['a', 'b','c']); }); |
||
| 166 | $this->assertValid(function($form) { $form->text('foo')->not_in(['a', 'b','c'])->default('d'); }); |
||
| 167 | |||
| 168 | $this->assertNotValid(function($form) { $form->text('foo')->not_in(['a', 'b','c'])->required(); }); |
||
| 169 | $this->assertNotValid(function($form) { $form->text('foo')->not_in(['a', 'b','c'])->default('a'); }); |
||
| 170 | } |
||
| 171 | |||
| 172 | } |
||
| 173 |