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