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 | use Brownie\Util\StorageArray; |
||
6 | |||
7 | class StorageArrayTest extends \PHPUnit_Framework_TestCase |
||
8 | { |
||
9 | |||
10 | protected function setUp() |
||
11 | { |
||
12 | } |
||
13 | |||
14 | protected function tearDown() |
||
15 | { |
||
16 | } |
||
17 | |||
18 | public function testConstructorOk1() |
||
19 | { |
||
20 | $fields = array( |
||
21 | 'question1' => 'answer1', |
||
22 | 'question2' => 'answer2', |
||
23 | ); |
||
24 | $arrayList = new StorageArray($fields, true); |
||
25 | $this->assertEquals($fields, $arrayList->toArray()); |
||
26 | } |
||
27 | |||
28 | public function testConstructorOk2() |
||
29 | { |
||
30 | $fields = array(); |
||
31 | $arrayList = new StorageArray($fields, false); |
||
32 | $this->assertEquals($fields, $arrayList->toArray()); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @expectedException \Brownie\Util\Exception\UndefinedMethodException |
||
37 | */ |
||
38 | public function testConstructorError() |
||
39 | { |
||
40 | $fields = array( |
||
41 | 'question1' => 'answer1', |
||
42 | 'question2' => 'answer2', |
||
43 | ); |
||
44 | $arrayList = new StorageArray($fields); |
||
45 | $this->assertEquals($fields, $arrayList->toArray()); |
||
46 | } |
||
47 | |||
48 | public function testSetGetOk() |
||
49 | { |
||
50 | $fields = array( |
||
51 | 'question1' => 'answer1', |
||
52 | 'question2' => 'answer2', |
||
53 | ); |
||
54 | $newvalue = 'answer3'; |
||
55 | $arrayList = new StorageArray($fields, true); |
||
56 | $arrayList->setQuestion2('answer3'); |
||
57 | $fields['question2'] = $newvalue; |
||
58 | $this->assertEquals($fields, $arrayList->toArray()); |
||
59 | $this->assertEquals($newvalue, $arrayList->getQuestion2()); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @expectedException \Brownie\Util\Exception\UndefinedMethodException |
||
64 | */ |
||
65 | public function testSetError() |
||
66 | { |
||
67 | $fields = array( |
||
68 | 'question1' => 'answer1', |
||
69 | 'question2' => 'answer2', |
||
70 | ); |
||
71 | $arrayList = new StorageArray($fields, true); |
||
72 | $arrayList->setQuestion3('answer3'); |
||
73 | $this->assertEquals($fields, $arrayList->toArray()); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @expectedException \Brownie\Util\Exception\UndefinedMethodException |
||
78 | */ |
||
79 | public function testGetError() |
||
80 | { |
||
81 | $fields = array( |
||
82 | 'question1' => 'answer1', |
||
83 | 'question2' => 'answer2', |
||
84 | ); |
||
85 | $newvalue = 'answer3'; |
||
86 | $arrayList = new StorageArray($fields, true); |
||
87 | $this->assertEquals($newvalue, $arrayList->getQuestion3('answer3')); |
||
88 | } |
||
90 |