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 |
||
| 9 | class ExclusionPolicyTest extends SerializerTestCase |
||
| 10 | { |
||
| 11 | public function testSerializeDefault() |
||
| 12 | { |
||
| 13 | $data = $this->serialize(new ExcludeDefault(true)); |
||
| 14 | |||
| 15 | $this->assertFalse(isset($data['model'])); |
||
| 16 | $this->assertTrue(isset($data['size'])); |
||
| 17 | $this->assertTrue(isset($data['color'])); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function testSerializeNone() |
||
| 21 | { |
||
| 22 | $data = $this->serialize(new ExcludeNone(true)); |
||
| 23 | |||
| 24 | $this->assertFalse(isset($data['model'])); |
||
| 25 | $this->assertTrue(isset($data['size'])); |
||
| 26 | $this->assertTrue(isset($data['color'])); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function testSerializeAll() |
||
| 30 | { |
||
| 31 | $data = $this->serialize(new ExcludeAll(true)); |
||
| 32 | |||
| 33 | $this->assertFalse(isset($data['model'])); |
||
| 34 | $this->assertTrue(isset($data['size'])); |
||
| 35 | $this->assertFalse(isset($data['color'])); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function testDeserializeDefault() |
||
| 39 | { |
||
| 40 | $data = ['model' => 'model_value', 'size' => 'size_value', 'color' => 'color_value']; |
||
| 41 | $obj = $this->deserialize($data, ExcludeDefault::class); |
||
| 42 | |||
| 43 | $this->assertPropertyValue($obj, 'model', null); |
||
| 44 | $this->assertPropertyValue($obj, 'size', 'size_value'); |
||
| 45 | $this->assertPropertyValue($obj, 'color', 'color_value'); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function testDeserializeNone() |
||
| 49 | { |
||
| 50 | $data = ['model' => 'model_value', 'size' => 'size_value', 'color' => 'color_value']; |
||
| 51 | $obj = $this->deserialize($data, ExcludeNone::class); |
||
| 52 | |||
| 53 | $this->assertPropertyValue($obj, 'model', null); |
||
| 54 | $this->assertPropertyValue($obj, 'size', 'size_value'); |
||
| 55 | $this->assertPropertyValue($obj, 'color', 'color_value'); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testDeserializeAll() |
||
| 59 | { |
||
| 60 | $data = ['model' => 'model_value', 'size' => 'size_value', 'color' => 'color_value']; |
||
| 61 | $obj = $this->deserialize($data, ExcludeAll::class); |
||
| 62 | |||
| 63 | $this->assertPropertyValue($obj, 'model', null); |
||
| 64 | $this->assertPropertyValue($obj, 'size', 'size_value'); |
||
| 65 | $this->assertPropertyValue($obj, 'color', null); |
||
| 66 | } |
||
| 67 | } |
||
| 68 |