|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests; |
|
4
|
|
|
|
|
5
|
|
|
use CurrencyConverter\ApiCaller; |
|
6
|
|
|
use CurrencyConverter\CurrencyConverter; |
|
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
|
|
|
$apiCaller = $this->prophesize(ApiCaller::class); |
|
26
|
|
|
$apiCaller->convert($fromCurrency, $toCurrency)->willReturn($expected); |
|
27
|
|
|
$apiCaller->isLastCallEmpty()->willReturn(false); |
|
28
|
|
|
$apiCaller->getLastResponse()->willReturn(json_encode([$fromCurrency . '_' . $toCurrency => $expected])); |
|
29
|
|
|
|
|
30
|
|
|
$currencyConverter = new CurrencyConverter('apiKey'); |
|
31
|
|
|
$currencyConverter->setApiCaller($apiCaller->reveal()); |
|
32
|
|
|
$result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999)); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertNotEquals($expected, $result); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testConvertWithNotValidResponseReturn0() |
|
38
|
|
|
{ |
|
39
|
|
|
$fromCurrency = 'EUR'; |
|
40
|
|
|
$toCurrency = 'USD'; |
|
41
|
|
|
$value = 100; |
|
42
|
|
|
|
|
43
|
|
|
$apiCaller = $this->prophesize(ApiCaller::class); |
|
44
|
|
|
$apiCaller->convert($fromCurrency, $toCurrency)->willReturn($value); |
|
45
|
|
|
$apiCaller->isLastCallEmpty()->willReturn(false); |
|
46
|
|
|
$apiCaller->getLastResponse()->willReturn(json_encode(['notEsistKey' => $value])); |
|
47
|
|
|
|
|
48
|
|
|
$currencyConverter = new CurrencyConverter('apiKey'); |
|
49
|
|
|
$currencyConverter->setApiCaller($apiCaller->reveal()); |
|
50
|
|
|
$result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999)); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEquals(0, $result); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|