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 |
||
8 | class ParameterTest extends TestCase |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @var Parameter |
||
13 | */ |
||
14 | private $parameter; |
||
15 | |||
16 | protected function setUp() |
||
17 | { |
||
18 | $this->parameter = new Parameter('Test', Parameter::TEXT); |
||
19 | } |
||
20 | |||
21 | protected function tearDown() |
||
22 | { |
||
23 | $this->parameter = null; |
||
24 | } |
||
25 | |||
26 | public function testGetKeyName() |
||
27 | { |
||
28 | $this->assertEquals('Parameter', $this->parameter->getKeyName()); |
||
29 | } |
||
30 | |||
31 | public function testSetGetValue() |
||
32 | { |
||
33 | $parameter = $this->parameter->setValue('TestString'); |
||
34 | $this->assertInstanceOf(Parameter::class, $parameter); |
||
35 | $this->assertEquals([ |
||
36 | 'DataValueType' => 1, |
||
37 | 'Value' => 'TestString' |
||
38 | ], $this->parameter->getValue()); |
||
39 | } |
||
40 | |||
41 | public function testArrayValue() |
||
42 | { |
||
43 | $parameter = $this->parameter->setArrayValue([ |
||
44 | 'test' => 'ok' |
||
45 | ]); |
||
46 | $this->assertInstanceOf(Parameter::class, $parameter); |
||
47 | $this->assertEquals([ |
||
48 | 'DataValueType' => 1, |
||
49 | 'Value' => 'Test', |
||
50 | 'ArrayValue' => [ |
||
51 | 'test' => 'ok', |
||
52 | ], |
||
53 | ], $this->parameter->getValue()); |
||
54 | } |
||
55 | |||
56 | public function testShouldSkipConvertion() |
||
57 | { |
||
58 | $parameter = $this->parameter->setShouldSkipConvertion(true); |
||
59 | $this->assertInstanceOf(Parameter::class, $parameter); |
||
60 | $this->assertEquals([ |
||
61 | 'DataValueType' => 1, |
||
62 | 'Value' => 'Test', |
||
63 | 'ShouldSkipConvertion' => true, |
||
64 | ], $this->parameter->getValue()); |
||
65 | } |
||
66 | } |
||
67 |