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 PasswordTest extends FormTestCase { |
||
| 6 | |||
| 7 | /** @test */ |
||
| 8 | public function it_can_be_rendered() |
||
| 9 | { |
||
| 10 | $form = $this->form(); |
||
| 11 | $form->password('foo'); |
||
| 12 | $this->assertContains('<input name="foo" type="password"', $form->render()); |
||
| 13 | } |
||
| 14 | |||
| 15 | /** @test */ |
||
| 16 | public function it_will_not_render_a_default_value() |
||
| 17 | { |
||
| 18 | $form = $this->form(); |
||
| 19 | $form->password('foo')->default('bar'); |
||
| 20 | $this->assertNotContains('value="bar"', $form->render()); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** @test */ |
||
| 24 | public function it_will_not_render_a_model_value() |
||
| 25 | { |
||
| 26 | $form = $this->form(); |
||
| 27 | $form->defaults($this->model(['foo'=>'bar'])); |
||
| 28 | $form->password('foo'); |
||
| 29 | $this->assertNotContains('value="bar"', $form->render()); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** @test */ |
||
| 33 | public function it_cannot_fill_model_values_using_default() |
||
| 34 | { |
||
| 35 | $model = $this->model(['foo'=>'']); |
||
| 36 | |||
| 37 | $form = $this->form(); |
||
| 38 | $form->password('foo')->default('bar'); |
||
| 39 | $form->fill($model); |
||
| 40 | |||
| 41 | $form->assertEmpty($model->foo); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** @test */ |
||
| 45 | public function it_can_fill_model_values_using_request() |
||
| 46 | { |
||
| 47 | $model = $this->model(['foo'=>'']); |
||
| 48 | |||
| 49 | $request = $this->request(); |
||
| 50 | $request->method('all')->will($this->returnValue(['foo'=>'bar', 'baz'=>true])); |
||
| 51 | $request->method('get')->will($this->returnValueMap([ |
||
| 52 | ['foo', 'bar'], |
||
| 53 | ['baz', true], |
||
| 54 | ])); |
||
| 55 | |||
| 56 | $form = $this->form($request); |
||
| 57 | $form->password('foo'); |
||
| 58 | $form->button('baz'); |
||
| 59 | |||
| 60 | $form->fill($model); |
||
| 61 | |||
| 62 | $this->assertNotEmpty($model->foo); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** @test */ |
||
| 66 | public function it_encrypts_passwords_automatically() |
||
| 67 | { |
||
| 68 | $model = $this->model(['foo'=>'']); |
||
| 69 | |||
| 70 | $request = $this->request(); |
||
| 71 | $request->method('all')->will($this->returnValue(['foo'=>'bar', 'baz'=>true])); |
||
| 72 | $request->method('get')->will($this->returnValueMap([ |
||
| 73 | ['foo', 'bar'], |
||
| 74 | ['baz', true], |
||
| 75 | ])); |
||
| 76 | |||
| 77 | $form = $this->form($request); |
||
| 78 | $form->password('foo'); |
||
| 79 | $form->button('baz'); |
||
| 80 | |||
| 81 | $form->fill($model); |
||
| 82 | |||
| 83 | $this->assertNotSame($model->foo, 'bar'); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** @test */ |
||
| 87 | public function it_can_check_if_password_matches() |
||
| 88 | { |
||
| 89 | $model = $this->model(['foo'=>'']); |
||
| 90 | |||
| 91 | $request = $this->request(); |
||
| 92 | $request->method('all')->will($this->returnValue(['foo'=>'bar', 'baz'=>true])); |
||
| 93 | $request->method('get')->will($this->returnValueMap([ |
||
| 94 | ['foo', 'bar'], |
||
| 95 | ['baz', true], |
||
| 96 | ])); |
||
| 97 | |||
| 98 | $form = $this->form($request); |
||
| 99 | $form->password('foo'); |
||
| 100 | $form->button('baz'); |
||
| 101 | |||
| 102 | $form->fill($model); |
||
| 103 | |||
| 104 | $hash = $form->password('foo')->hash('bar'); |
||
| 105 | $invalid_hash = $form->password('foo')->hash('invalid'); |
||
| 106 | $empty_hash = $form->password('foo')->hash(''); |
||
| 107 | |||
| 108 | $this->assertTrue($form->password('foo')->matches($hash)); |
||
| 109 | $this->assertFalse($form->password('foo')->matches($invalid_hash)); |
||
| 110 | $this->assertFalse($form->password('foo')->matches($empty_hash)); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** @test */ |
||
| 114 | public function it_provides_expected_values() |
||
| 115 | { |
||
| 116 | $form = $this->form(); |
||
| 117 | $form->password('foo'); |
||
| 118 | |||
| 119 | $this->assertSame($form->get('foo'), ''); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** @test */ |
||
| 123 | public function it_will_not_provide_default_values() |
||
| 124 | { |
||
| 125 | $form = $this->form(); |
||
| 126 | $form->password('foo')->default('bar'); |
||
| 127 | |||
| 128 | $this->assertSame($form->get('foo'), ''); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** @test */ |
||
| 132 | public function it_validates_required() |
||
| 133 | { |
||
| 134 | $this->assertValid(function($form) { $form->password('foo'); }); |
||
| 135 | |||
| 136 | $this->assertNotValid(function($form) { $form->password('foo')->required(); }); |
||
| 137 | } |
||
| 138 | |||
| 139 | } |
||
| 140 |