|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests; |
|
4
|
|
|
|
|
5
|
|
|
use CurrencyConverter\CurrencyConverter; |
|
6
|
|
|
use CurrencyConverter\Rates; |
|
7
|
|
|
|
|
8
|
|
|
class CurrencyConverterTest extends \PHPUnit\Framework\TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @expectedException \Exception |
|
12
|
|
|
*/ |
|
13
|
|
|
public function testConvertWithNotExistingApiKey() |
|
14
|
|
|
{ |
|
15
|
|
|
$currencyConverter = new CurrencyConverter('apiKey'); |
|
16
|
|
|
$currencyConverter->convert('foo', 'bar', random_int(1, 999999)); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function testConvertWithinValidCurrencyProvided() |
|
20
|
|
|
{ |
|
21
|
|
|
$fromCurrency = 'EUR'; |
|
22
|
|
|
$toCurrency = 'USD'; |
|
23
|
|
|
$expected = 100; |
|
24
|
|
|
|
|
25
|
|
|
$rates = $this->prophesize(Rates::class); |
|
26
|
|
|
$rates->getRates($fromCurrency, $toCurrency)->willReturn($expected); |
|
27
|
|
|
|
|
28
|
|
|
$currencyConverter = new CurrencyConverter('apiKey'); |
|
29
|
|
|
$currencyConverter->setRates($rates->reveal()); |
|
30
|
|
|
$result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999)); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertNotEquals($expected, $result); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function testConvertWithNotValidResponseReturn0() |
|
36
|
|
|
{ |
|
37
|
|
|
$fromCurrency = 'EUR'; |
|
38
|
|
|
$toCurrency = 'USD'; |
|
39
|
|
|
$value = 0; |
|
40
|
|
|
|
|
41
|
|
|
$rates = $this->prophesize(Rates::class); |
|
42
|
|
|
$rates->getRates($fromCurrency, $toCurrency)->willReturn($value); |
|
43
|
|
|
|
|
44
|
|
|
$currencyConverter = new CurrencyConverter('apiKey'); |
|
45
|
|
|
$currencyConverter->setRates($rates->reveal()); |
|
46
|
|
|
$result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999)); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals(0, $result); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
public function testGetRates() |
|
52
|
|
|
{ |
|
53
|
|
|
$fromCurrency = 'EUR'; |
|
54
|
|
|
$toCurrency = 'USD'; |
|
55
|
|
|
$value = 100; |
|
56
|
|
|
|
|
57
|
|
|
$rates = $this->prophesize(Rates::class); |
|
58
|
|
|
$rates->getRates($fromCurrency, $toCurrency)->willReturn($value); |
|
59
|
|
|
|
|
60
|
|
|
$currencyConverter = new CurrencyConverter('apiKey'); |
|
61
|
|
|
$currencyConverter->setRates($rates->reveal()); |
|
62
|
|
|
$result = $currencyConverter->getRates($fromCurrency, $toCurrency); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals($value, $result); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setTestApiCaller() |
|
68
|
|
|
{ |
|
69
|
|
|
$apiCallerMock = $this->prophesize(ApiCaller::class); |
|
70
|
|
|
|
|
71
|
|
|
$currencyConverter = new CurrencyConverter('apiKey'); |
|
72
|
|
|
$currencyConverter->setApiCaller($apiCaller->reveal()); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$apiCaller = $currencyConverter->getApiCaller(); |
|
75
|
|
|
$this->assertEquals($apiCallerMock, $apiCaller); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.