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 |
||
| 12 | |||
| 13 | /** |
||
| 14 | * |
||
| 15 | * |
||
| 16 | * BEFORE TESTTING MAKE SURE |
||
| 17 | * TO WRITE CURRENT DATETIME INTO THE FIRST RECORDS OF CurrencyratesFixture in tests/Fixture/CurrencyratesFixture |
||
| 18 | * |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | class CurrencyConverterComponentTest extends TestCase |
||
| 22 | { |
||
| 23 | |||
| 24 | public $fixtures = ['plugin.CurrencyConverter.Currencyrates']; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Component being tested |
||
| 28 | * |
||
| 29 | * @var \CurrencyConverter\Controller\Component\CurrencyConverterComponent |
||
| 30 | */ |
||
| 31 | public $CurrencyConverter; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \Cake\Http\ServerRequest |
||
| 35 | */ |
||
| 36 | protected $request; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \Cake\Http\Response |
||
| 40 | */ |
||
| 41 | protected $response; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \Cake\Controller\Controller |
||
| 45 | */ |
||
| 46 | protected $controller; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \Cake\Controller\ComponentRegistry |
||
| 50 | */ |
||
| 51 | protected $registry; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \Cake\ORM\Table |
||
| 55 | */ |
||
| 56 | protected $Table; |
||
| 57 | |||
| 58 | public function setUp() |
||
| 59 | { |
||
| 60 | // Configuration de notre component et de notre faux controller de test. |
||
| 61 | $request = new ServerRequest(); |
||
| 62 | $response = new Response(); |
||
| 63 | $this->Controller = new Controller($request, $response); |
||
| 64 | $this->Registry = new ComponentRegistry($this->Controller); |
||
| 65 | $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []); |
||
| 66 | |||
| 67 | $table = TableRegistry::get('Currencyrates'); |
||
| 68 | $this->Table = $table; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function testConfig() |
||
| 72 | { |
||
| 73 | $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []); |
||
| 74 | $expected = [ |
||
| 75 | 'database' => 2, |
||
| 76 | 'refresh' => 24, |
||
| 77 | 'decimal' => 2 |
||
| 78 | ]; |
||
| 79 | $this->assertEquals($expected, $this->CurrencyConverter->getConfig()); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function testConvertSameCurrency() |
||
| 83 | { |
||
| 84 | $amount = 20.00; |
||
| 85 | $fromCurrency = 'EUR'; |
||
| 86 | $toCurrency = 'EUR'; |
||
| 87 | |||
| 88 | $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
||
| 89 | $expected = 20.00; |
||
| 90 | $this->assertEquals($expected, $result); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testConvertWithComma() |
||
| 94 | { |
||
| 95 | $amount = '20.00'; |
||
| 96 | $fromCurrency = 'EUR'; |
||
| 97 | $toCurrency = 'EUR'; |
||
| 98 | |||
| 99 | $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
||
| 100 | $expected = 20.00; |
||
| 101 | $this->assertEquals($expected, $result); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function testConvertNumberFormatting() |
||
| 105 | { |
||
| 106 | $amount = 20.123456; |
||
| 107 | $fromCurrency = 'EUR'; |
||
| 108 | $toCurrency = 'EUR'; |
||
| 109 | |||
| 110 | $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
||
| 111 | $expected = 20.12; |
||
| 112 | $this->assertEquals($expected, $result); |
||
| 113 | |||
| 114 | $amount = 20.123456; |
||
| 115 | $fromCurrency = 'EUR'; |
||
| 116 | $toCurrency = 'EUR'; |
||
| 117 | |||
| 118 | $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
||
| 119 | 'decimal' => 3 |
||
| 120 | ]); |
||
| 121 | $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
||
| 122 | $expected = 20.123; |
||
| 123 | $this->assertEquals($expected, $result); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testConvertUsingDatabaseWhenRateDoNotExistInDatabase() |
||
| 127 | { |
||
| 128 | $amount = 20.00; |
||
| 129 | $fromCurrency = 'EUR'; |
||
| 130 | $toCurrency = 'USD'; |
||
| 131 | |||
| 132 | $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
||
| 133 | $rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
||
| 134 | $expected = round(number_format($rate * 20.00, 2), 2); |
||
| 135 | |||
| 136 | $this->assertEquals($expected, $result); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() |
||
| 140 | { |
||
| 141 | $amount = 20.00; |
||
| 142 | $fromCurrency = 'EUR'; |
||
| 143 | $toCurrency = 'GBP'; |
||
| 144 | |||
| 145 | $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
||
| 146 | $expected = round(number_format(0.8 * 20.00, 2), 2); |
||
| 147 | |||
| 148 | $this->assertEquals($expected, $result); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
||
| 152 | { |
||
| 153 | $amount = 20.00; |
||
| 154 | $fromCurrency = 'EUR'; |
||
| 155 | $toCurrency = 'GBP'; |
||
| 156 | |||
| 255 |