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 |
||
| 11 | class UpdateContractTest extends TestCase |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var UpdateContract |
||
| 16 | */ |
||
| 17 | private $updateContract; |
||
| 18 | |||
| 19 | protected function setUp() |
||
| 20 | { |
||
| 21 | $this->updateContract = new UpdateContract('rootSchemaName'); |
||
| 22 | } |
||
| 23 | |||
| 24 | protected function tearDown() |
||
| 25 | { |
||
| 26 | $this->updateContract = null; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function testSetGetRootSchemaName() |
||
| 30 | { |
||
| 31 | $this->assertEquals('rootSchemaName', $this->updateContract->getRootSchemaName()); |
||
| 32 | $this->updateContract->setRootSchemaName('TestRootSchemaName'); |
||
| 33 | $this->assertEquals('TestRootSchemaName', $this->updateContract->getRootSchemaName()); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testSetGetOperationType() |
||
| 37 | { |
||
| 38 | $this->assertEquals(2, $this->updateContract->getOperationType()); |
||
| 39 | $this->updateContract->setOperationType(200); |
||
| 40 | $this->assertEquals(200, $this->updateContract->getOperationType()); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testSetGetContractType() |
||
| 44 | { |
||
| 45 | $this->assertEquals('UpdateQuery', $this->updateContract->getContractType()); |
||
| 46 | $this->updateContract->setContractType('TestUpdateQuery'); |
||
| 47 | $this->assertEquals('TestUpdateQuery', $this->updateContract->getContractType()); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @expectedException \Brownie\BpmOnline\Exception\ValidateException |
||
| 52 | */ |
||
| 53 | public function testValidateException() |
||
| 54 | { |
||
| 55 | $this->updateContract->setOperationType(1); |
||
| 56 | $this->assertNull($this->updateContract->validate()); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testValidate() |
||
| 60 | { |
||
| 61 | $columnFilter = $this->prophesize(ColumnFilter::class); |
||
| 62 | $columnFilterMethodToArray = new MethodProphecy( |
||
| 63 | $columnFilter, |
||
| 64 | 'toArray', |
||
| 65 | [] |
||
| 66 | ); |
||
| 67 | $columnFilter |
||
| 68 | ->addMethodProphecy( |
||
| 69 | $columnFilterMethodToArray->willReturn(['test' => 'Ok']) |
||
| 70 | ); |
||
| 71 | |||
| 72 | $columnFilterMethodValidate = new MethodProphecy( |
||
| 73 | $columnFilter, |
||
| 74 | 'validate', |
||
| 75 | [] |
||
| 76 | ); |
||
| 77 | $columnFilter |
||
| 78 | ->addMethodProphecy( |
||
| 79 | $columnFilterMethodValidate->willReturn(null) |
||
| 80 | ); |
||
| 81 | |||
| 82 | $updateContract = $this->updateContract->addFilter($columnFilter->reveal()); |
||
| 83 | |||
| 84 | $this->assertInstanceOf(UpdateContract::class, $updateContract); |
||
| 85 | $this->assertNull($this->updateContract->validate()); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testAddColumn() |
||
| 89 | { |
||
| 90 | $columnExpression = $this->prophesize(ColumnExpression::class); |
||
| 91 | $columnExpressionMethodToArray = new MethodProphecy( |
||
| 92 | $columnExpression, |
||
| 93 | 'toArray', |
||
| 94 | [] |
||
| 95 | ); |
||
| 96 | $columnExpression |
||
| 97 | ->addMethodProphecy( |
||
| 98 | $columnExpressionMethodToArray->willReturn(['test' => 'Ok']) |
||
| 99 | ); |
||
| 100 | |||
| 101 | $updateContract = $this->updateContract->addColumn('TestColumn', $columnExpression->reveal()); |
||
| 102 | |||
| 103 | $this->assertInstanceOf(UpdateContract::class, $updateContract); |
||
| 104 | $this->assertEquals([ |
||
| 105 | 'test' => 'Ok', |
||
| 106 | ], $this->updateContract->toArray()['ColumnValues']['Items']['TestColumn']); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testAddFilter() |
||
| 110 | { |
||
| 111 | $columnFilter = $this->prophesize(ColumnFilter::class); |
||
| 112 | $columnFilterMethodToArray = new MethodProphecy( |
||
| 113 | $columnFilter, |
||
| 114 | 'toArray', |
||
| 115 | [] |
||
| 116 | ); |
||
| 117 | $columnFilter |
||
| 118 | ->addMethodProphecy( |
||
| 119 | $columnFilterMethodToArray->willReturn(['test' => 'Ok']) |
||
| 120 | ); |
||
| 121 | |||
| 122 | $updateContract = $this->updateContract->addFilter($columnFilter->reveal()); |
||
| 123 | |||
| 124 | $this->assertInstanceOf(UpdateContract::class, $updateContract); |
||
| 125 | $this->assertEquals([ |
||
| 126 | 'RootSchemaName' => 'rootSchemaName', |
||
| 127 | 'OperationType' => 2, |
||
| 128 | 'IsForceUpdate' => false, |
||
| 129 | 'ColumnValues' => [ |
||
| 130 | 'Items' => [], |
||
| 131 | ], |
||
| 132 | 'Filters' => [ |
||
| 133 | 'test' => 'Ok', |
||
| 134 | ], |
||
| 135 | ], $this->updateContract->toArray()); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function testToArray() |
||
| 139 | { |
||
| 140 | $this->assertEquals([ |
||
| 141 | 'RootSchemaName' => 'rootSchemaName', |
||
| 142 | 'OperationType' => 2, |
||
| 143 | 'IsForceUpdate' => false, |
||
| 144 | 'ColumnValues' => [ |
||
| 145 | 'Items' => [], |
||
| 146 | ], |
||
| 147 | 'Filters' => [], |
||
| 148 | ], $this->updateContract->toArray()); |
||
| 149 | } |
||
| 150 | } |
||
| 151 |