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

CurrencyConverterHelperTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 227
Duplicated Lines 71.81 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 7
dl 163
loc 227
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 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() 12 12 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
 * Copyright 2010, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CurrencyConverter\Test\TestCase\View\Helper;
13
14
use Cake\Controller\Controller;
15
use Cake\Network\Request;
16
use Cake\Routing\Router;
17
use Cake\TestSuite\TestCase;
18
use Cake\View\View;
19
use CurrencyConverter\View\Helper\CurrencyConverterHelper;
20
use Cake\ORM\TableRegistry;
21
22
/**
23
 *
24
 *
25
 * BEFORE TESTTING MAKE SURE
26
 * TO WRITE CURRENT DATETIME INTO THE FIRST RECORDS OF CurrencyratesFixture in tests/Fixture/CurrencyratesFixture
27
 *
28
 *
29
 */
30
class CurrencyConverterHelperTest extends TestCase {
31
32
	public $fixtures = ['plugin.CurrencyConverter.Currencyrates'];
33
34
	/**
35
	 * Helper being tested
36
	 *
37
	 * @var \Ratings\View\Helper\CurrencyConverter
38
	 */
39
	public $CurrencyConverter;
40
41
	/**
42
	 * @var \Cake\Http\ServerRequest
43
	 */
44
	protected $request;
45
46
	/**
47
	 * @var \Cake\View\View
48
	 */
49
	protected $View;
50
51
	/**
52
	 * @var \Cake\ORM\Table
53
	 */
54
	protected $Table;
55
56
	/**
57
	 * (non-PHPdoc)
58
	 *
59
	 * @return void
60
	 */
61
	public function setUp() {
62
		parent::setUp();
63
		$this->request = new Request();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cake\Network\Request() of type object<Cake\Network\Request> is incompatible with the declared type object<Cake\Http\ServerRequest> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
		$this->Controller = new Controller();
0 ignored issues
show
Bug introduced by
The property Controller does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
		$this->View = new View($this->request);
66
		$this->CurrencyConverter = new CurrencyConverterHelper($this->View);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CurrencyConverter\V...rterHelper($this->View) of type object<CurrencyConverter...urrencyConverterHelper> is incompatible with the declared type object<Ratings\View\Helper\CurrencyConverter> of property $CurrencyConverter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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->Component = new CurrencyConverterHelper($this->View, []);
0 ignored issues
show
Bug introduced by
The property Component does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
        $expected = [
76
            'database' => 2,
77
            'refresh' => 24,
78
            'decimal' => 2
79
        ];
80
        $this->assertEquals($expected, $this->Component->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 CurrencyConverterHelper($this->View, [
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CurrencyConverter\V... array('decimal' => 3)) of type object<CurrencyConverter...urrencyConverterHelper> is incompatible with the declared type object<Ratings\View\Helper\CurrencyConverter> of property $CurrencyConverter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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
153 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...
154
    {
155
        $amount = 20.00;
156
        $fromCurrency = 'EUR';
157
        $toCurrency = 'GBP';
158
159
        $this->CurrencyConverter = new CurrencyConverterHelper($this->View, [
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CurrencyConverter\V... array('refresh' => 0)) of type object<CurrencyConverter...urrencyConverterHelper> is incompatible with the declared type object<Ratings\View\Helper\CurrencyConverter> of property $CurrencyConverter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
160
            'refresh' => 0
161
        ]);
162
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
163
        $rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
164
        $expected = round(number_format($rate * 20.00, 2), 2);
165
166
        $this->assertEquals($expected, $result);
167
    }
168
169 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...
170
    {
171
        $amount = 20.00;
172
        $fromCurrency = 'GBP';
173
        $toCurrency = 'EUR';
174
175
        $this->CurrencyConverter = new CurrencyConverterHelper($this->View, [
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CurrencyConverter\V...y('database' => false)) of type object<CurrencyConverter...urrencyConverterHelper> is incompatible with the declared type object<Ratings\View\Helper\CurrencyConverter> of property $CurrencyConverter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
176
            'database' => false
177
        ]);
178
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
179
180
        $this->assertGreaterThan(20, $result);
181
182
        $result = count($this->Table->find('all')->toArray());
183
        $this->assertEquals(1, $result);
184
    }
185
186 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...
187
    {
188
        $fromCurrency = 'EUR';
189
        $toCurrency = 'EUR';
190
191
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
192
        $expected = 1;
193
        $this->assertEquals($expected, $result);
194
    }
195
196 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...
197
    {
198
        $fromCurrency = 'EUR';
199
        $toCurrency = 'USD';
200
201
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
202
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate;
203
204
        $this->assertEquals($expected, $result);
205
    }
206
207 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...
208
    {
209
        $fromCurrency = 'EUR';
210
        $toCurrency = 'GBP';
211
212
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
213
        $expected = 0.8;
214
215
        $this->assertEquals($expected, $result);
216
    }
217
218 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...
219
    {
220
        $fromCurrency = 'EUR';
221
        $toCurrency = 'GBP';
222
223
        $this->CurrencyConverter = new CurrencyConverterHelper($this->View, [
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CurrencyConverter\V... array('refresh' => 0)) of type object<CurrencyConverter...urrencyConverterHelper> is incompatible with the declared type object<Ratings\View\Helper\CurrencyConverter> of property $CurrencyConverter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
224
            'refresh' => 0
225
        ]);
226
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
227
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
228
229
        $this->assertEquals($expected, $result);
230
    }
231
232 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...
233
    {
234
        $fromCurrency = 'GBP';
235
        $toCurrency = 'EUR';
236
237
        $this->CurrencyConverter = new CurrencyConverterHelper($this->View, [
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CurrencyConverter\V...y('database' => false)) of type object<CurrencyConverter...urrencyConverterHelper> is incompatible with the declared type object<Ratings\View\Helper\CurrencyConverter> of property $CurrencyConverter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
238
            'database' => false
239
        ]);
240
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
241
242
        $this->assertGreaterThan(1, $result);
243
244
        $result = count($this->Table->find('all')->toArray());
245
        $this->assertEquals(1, $result);
246
    }
247
248
    public function tearDown()
249
    {
250
        parent::tearDown();
251
        // Nettoie la Table
252
        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...
253
        // Nettoie les variables quand les tests sont finis.
254
		unset($this->CurrencyConverter);
255
    }
256
}