Test Failed
Pull Request — master (#14)
by
unknown
13:32
created

CurrencyConverterComponentTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 234
Duplicated Lines 69.23 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 9
dl 162
loc 234
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A testConfig() 10 10 1
A testConvertSameCurrency() 10 10 1
A testConvertWithComma() 10 10 1
A testConvertNumberFormatting() 21 21 1
A testConvertUsingDatabaseWhenRateDoNotExistInDatabase() 12 12 1
A testConvertUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() 11 11 1
A testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() 15 15 1
A testConvertNotUsingDatabse() 16 16 1
A testRateSameCurrency() 9 9 1
A testRateUsingDatabaseWhenRateDoNotExistInDatabase() 10 10 1
A testRateUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() 10 10 1
A testRateUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() 13 13 1
A testRateNotUsingDatabse() 15 15 1
A tearDown() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace CurrencyConverter\Test\TestCase\Controller\Component;
4
5
use CurrencyConverter\Controller\Component\CurrencyConverterComponent;
6
use Cake\Controller\Controller;
7
use Cake\Controller\ComponentRegistry;
8
use Cake\Event\Event;
9
use Cake\Http\ServerRequest;
10
use Cake\Http\Response;
11
use Cake\TestSuite\TestCase;
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);
0 ignored issues
show
Bug introduced by
The property Controller does not seem to exist. Did you mean controller?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
        $this->Registry = new ComponentRegistry($this->Controller);
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property Controller does not seem to exist. Did you mean controller?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
66
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []);
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
67
68
        $table = TableRegistry::get('Currencyrates');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\TableRegistry::get() has been deprecated with message: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
69
        $this->Table = $table;
70
    }
71
72 View Code Duplication
    public function testConfig()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []);
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
        $expected = [
76
            'database' => 2,
77
            'refresh' => 24,
78
            'decimal' => 2
79
        ];
80
        $this->assertEquals($expected, $this->CurrencyConverter->getConfig());
81
    }
82
83 View Code Duplication
    public function testConvertSameCurrency()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testConvertWithComma()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testConvertNumberFormatting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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, [
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
120
            'decimal' => 3
121
        ]);
122
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
123
        $expected = 20.123;
124
        $this->assertEquals($expected, $result);
125
    }
126
127 View Code Duplication
    public function testConvertUsingDatabaseWhenRateDoNotExistInDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        $amount = 20.00;
155
        $fromCurrency = 'EUR';
156
        $toCurrency = 'GBP';
157
158
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
159
            'refresh' => 0
160
        ]);
161
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
162
        $rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
163
        $expected = round(number_format($rate * 20.00, 2), 2);
164
165
        $this->assertEquals($expected, $result);
166
    }
167
168 View Code Duplication
    public function testConvertNotUsingDatabse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $amount = 20.00;
171
        $fromCurrency = 'GBP';
172
        $toCurrency = 'EUR';
173
174
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
175
            'database' => false
176
        ]);
177
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
178
179
        $this->assertGreaterThan(20, $result);
180
181
        $result = count($this->Table->find('all')->toArray());
182
        $this->assertEquals(1, $result);
183
    }
184
185 View Code Duplication
    public function testRateSameCurrency()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187
        $fromCurrency = 'EUR';
188
        $toCurrency = 'EUR';
189
190
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
191
        $expected = 1;
192
        $this->assertEquals($expected, $result);
193
    }
194
195 View Code Duplication
    public function testRateUsingDatabaseWhenRateDoNotExistInDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197
        $fromCurrency = 'EUR';
198
        $toCurrency = 'USD';
199
200
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
201
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate;
202
203
        $this->assertEquals($expected, $result);
204
    }
205
206 View Code Duplication
    public function testRateUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
    {
208
        $fromCurrency = 'EUR';
209
        $toCurrency = 'GBP';
210
211
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
212
        $expected = 0.8;
213
214
        $this->assertEquals($expected, $result);
215
    }
216
217 View Code Duplication
    public function testRateUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
    {
219
        $fromCurrency = 'EUR';
220
        $toCurrency = 'GBP';
221
222
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
223
            'refresh' => 0
224
        ]);
225
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
226
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
227
228
        $this->assertEquals($expected, $result);
229
    }
230
231 View Code Duplication
    public function testRateNotUsingDatabse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233
        $fromCurrency = 'GBP';
234
        $toCurrency = 'EUR';
235
236
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
0 ignored issues
show
Bug introduced by
The property Registry does not seem to exist. Did you mean registry?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
237
            'database' => false
238
        ]);
239
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
240
241
        $this->assertGreaterThan(1, $result);
242
243
        $result = count($this->Table->find('all')->toArray());
244
        $this->assertEquals(1, $result);
245
    }
246
247
    public function tearDown()
248
    {
249
        parent::tearDown();
250
        // Nettoie la Table
251
        TableRegistry::clear();
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\TableRegistry::clear() has been deprecated with message: 3.6.0 Use \Cake\ORM\Locator\TableLocator::clear() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
252
        // Nettoie les variables quand les tests sont finis.
253
        unset($this->CurrencyConverter, $this->Controller);
254
    }
255
}