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
|
|
|
|
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.