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 |
||
| 37 | class ServiceTest extends UnitTest |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Apache_Solr_Document |
||
| 42 | */ |
||
| 43 | protected $documentMock; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * the service |
||
| 47 | * |
||
| 48 | * @var Service |
||
| 49 | */ |
||
| 50 | protected $service; |
||
| 51 | |||
| 52 | public function setUp() |
||
| 53 | { |
||
| 54 | date_default_timezone_set('Europe/Berlin'); |
||
| 55 | $this->documentMock = new \Apache_Solr_Document(); |
||
| 56 | $this->service = new Service(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @test |
||
| 61 | */ |
||
| 62 | public function transformsStringToUppercaseOnSingleValuedField() |
||
| 63 | { |
||
| 64 | $this->documentMock->setField('stringField', 'stringvalue'); |
||
| 65 | $configuration = array('stringField' => 'uppercase'); |
||
| 66 | |||
| 67 | $this->service->processDocument($this->documentMock, $configuration); |
||
| 68 | $value = $this->documentMock->getField('stringField'); |
||
| 69 | $this->assertEquals( |
||
| 70 | $value['value'], |
||
| 71 | 'STRINGVALUE', |
||
| 72 | 'field was not processed with uppercase' |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @test |
||
| 78 | */ |
||
| 79 | public function transformsStringToUppercaseOnMultiValuedField() |
||
| 80 | { |
||
| 81 | $this->documentMock->addField('stringField', 'stringvalue_1'); |
||
| 82 | $this->documentMock->addField('stringField', 'stringvalue_2'); |
||
| 83 | $configuration = array('stringField' => 'uppercase'); |
||
| 84 | |||
| 85 | $this->service->processDocument($this->documentMock, $configuration); |
||
| 86 | $value = $this->documentMock->getField('stringField'); |
||
| 87 | $this->assertEquals( |
||
| 88 | $value['value'], |
||
| 89 | array('STRINGVALUE_1', 'STRINGVALUE_2'), |
||
| 90 | 'field was not processed with uppercase' |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @test |
||
| 96 | */ |
||
| 97 | public function transformsUnixTimestampToIsoDateOnSingleValuedField() |
||
| 98 | { |
||
| 99 | $this->documentMock->setField('dateField', |
||
| 100 | '1262343600'); // 2010-01-01 12:00 |
||
| 101 | $configuration = array('dateField' => 'timestampToIsoDate'); |
||
| 102 | |||
| 103 | $this->service->processDocument($this->documentMock, $configuration); |
||
| 104 | $value = $this->documentMock->getField('dateField'); |
||
| 105 | $this->assertEquals( |
||
| 106 | $value['value'], |
||
| 107 | '2010-01-01T12:00:00Z', |
||
| 108 | 'field was not processed with timestampToIsoDate' |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @test |
||
| 114 | */ |
||
| 115 | public function transformsUnixTimestampToIsoDateOnMultiValuedField() |
||
| 116 | { |
||
| 117 | $this->documentMock->addField('dateField', |
||
| 118 | '1262343600'); // 2010-01-01 12:00 |
||
| 119 | $this->documentMock->addField('dateField', |
||
| 120 | '1262343601'); // 2010-01-01 12:01 |
||
| 121 | $configuration = array('dateField' => 'timestampToIsoDate'); |
||
| 122 | |||
| 123 | $this->service->processDocument($this->documentMock, $configuration); |
||
| 124 | $value = $this->documentMock->getField('dateField'); |
||
| 125 | $this->assertEquals( |
||
| 126 | $value['value'], |
||
| 127 | array('2010-01-01T12:00:00Z', '2010-01-01T12:00:01Z'), |
||
| 128 | 'field was not processed with timestampToIsoDate' |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | } |
||
| 132 |