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 |
||
10 | class DeleteContractTest extends TestCase |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var DeleteContract |
||
15 | */ |
||
16 | private $deleteContract; |
||
17 | |||
18 | protected function setUp() |
||
19 | { |
||
20 | $this->deleteContract = new DeleteContract('rootSchemaName'); |
||
21 | } |
||
22 | |||
23 | protected function tearDown() |
||
24 | { |
||
25 | $this->deleteContract = null; |
||
26 | } |
||
27 | |||
28 | public function testSetGetRootSchemaName() |
||
29 | { |
||
30 | $this->assertEquals('rootSchemaName', $this->deleteContract->getRootSchemaName()); |
||
31 | $this->deleteContract->setRootSchemaName('TestRootSchemaName'); |
||
32 | $this->assertEquals('TestRootSchemaName', $this->deleteContract->getRootSchemaName()); |
||
33 | } |
||
34 | |||
35 | public function testSetGetOperationType() |
||
36 | { |
||
37 | $this->assertEquals(3, $this->deleteContract->getOperationType()); |
||
38 | $this->deleteContract->setOperationType(300); |
||
39 | $this->assertEquals(300, $this->deleteContract->getOperationType()); |
||
40 | } |
||
41 | |||
42 | public function testSetGetContractType() |
||
43 | { |
||
44 | $this->assertEquals('DeleteQuery', $this->deleteContract->getContractType()); |
||
45 | $this->deleteContract->setContractType('TestDeleteQuery'); |
||
46 | $this->assertEquals('TestDeleteQuery', $this->deleteContract->getContractType()); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @expectedException \Brownie\BpmOnline\Exception\ValidateException |
||
51 | */ |
||
52 | public function testValidateException() |
||
53 | { |
||
54 | $this->deleteContract->setOperationType(1); |
||
55 | $this->assertNull($this->deleteContract->validate()); |
||
56 | } |
||
57 | |||
58 | public function testValidate() |
||
59 | { |
||
60 | $columnFilter = $this->prophesize(ColumnFilter::class); |
||
61 | $columnFilterMethodToArray = new MethodProphecy( |
||
62 | $columnFilter, |
||
63 | 'toArray', |
||
64 | [] |
||
65 | ); |
||
66 | $columnFilter |
||
67 | ->addMethodProphecy( |
||
68 | $columnFilterMethodToArray->willReturn(['test' => 'Ok']) |
||
69 | ); |
||
70 | |||
71 | $columnFilterMethodValidate = new MethodProphecy( |
||
72 | $columnFilter, |
||
73 | 'validate', |
||
74 | [] |
||
75 | ); |
||
76 | $columnFilter |
||
77 | ->addMethodProphecy( |
||
78 | $columnFilterMethodValidate->willReturn(null) |
||
79 | ); |
||
80 | |||
81 | $deleteContract = $this->deleteContract->addFilter($columnFilter->reveal()); |
||
82 | |||
83 | $this->assertInstanceOf(DeleteContract::class, $deleteContract); |
||
84 | $this->assertNull($this->deleteContract->validate()); |
||
85 | } |
||
86 | |||
87 | public function testAddFilter() |
||
88 | { |
||
89 | $columnFilter = $this->prophesize(ColumnFilter::class); |
||
90 | $columnFilterMethodToArray = new MethodProphecy( |
||
91 | $columnFilter, |
||
92 | 'toArray', |
||
93 | [] |
||
94 | ); |
||
95 | $columnFilter |
||
96 | ->addMethodProphecy( |
||
97 | $columnFilterMethodToArray->willReturn(['test' => 'Ok']) |
||
98 | ); |
||
99 | |||
100 | $deleteContract = $this->deleteContract->addFilter($columnFilter->reveal()); |
||
101 | |||
102 | $this->assertInstanceOf(DeleteContract::class, $deleteContract); |
||
103 | $this->assertEquals([ |
||
104 | 'RootSchemaName' => 'rootSchemaName', |
||
105 | 'OperationType' => 3, |
||
106 | 'Filters' => [ |
||
107 | 'test' => 'Ok', |
||
108 | ], |
||
109 | ], $this->deleteContract->toArray()); |
||
110 | } |
||
111 | |||
112 | public function testToArray() |
||
113 | { |
||
114 | $this->assertEquals([ |
||
115 | 'RootSchemaName' => 'rootSchemaName', |
||
116 | 'OperationType' => 3, |
||
117 | 'Filters' => [], |
||
118 | ], $this->deleteContract->toArray()); |
||
119 | } |
||
120 | } |
||
121 |