1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SteffenBrand\CurrCurr\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use SteffenBrand\CurrCurr\Exception\ExchangeRatesRequestFailedException; |
9
|
|
|
use SteffenBrand\CurrCurr\Mapper\ExchangeRatesMapper; |
10
|
|
|
use SteffenBrand\CurrCurr\Mapper\MapperInterface; |
11
|
|
|
use SteffenBrand\CurrCurr\Model\ExchangeRate; |
12
|
|
|
|
13
|
|
|
class EcbClientMock implements EcbClientInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @const string |
18
|
|
|
*/ |
19
|
|
|
const VALID_RESPONSE = 'ValidResponse'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @const string |
23
|
|
|
*/ |
24
|
|
|
const USD_MISSING_RESPONSE = 'UsdMissingResponse'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @const string |
28
|
|
|
*/ |
29
|
|
|
const DATE_MISSING_RESPONSE = 'DateMissingResponse'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ResponseInterface |
33
|
|
|
*/ |
34
|
|
|
private $response; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var MapperInterface |
38
|
|
|
*/ |
39
|
|
|
private $mapper; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $expectedResponse |
43
|
|
|
* @param CacheInterface $cache |
44
|
|
|
* @param int $cacheTimeInSeconds |
45
|
|
|
* @param MapperInterface $mapper |
46
|
|
|
*/ |
47
|
|
|
public function __construct(string $expectedResponse = self::VALID_RESPONSE, |
48
|
|
|
CacheInterface $cache = null, |
49
|
|
|
int $cacheTimeInSeconds = self::CACHE_UNTIL_MIDNIGHT, |
50
|
|
|
MapperInterface $mapper = null) |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
if (null === $mapper) { |
54
|
|
|
$mapper = new ExchangeRatesMapper(); |
55
|
|
|
} |
56
|
|
|
$this->mapper = $mapper; |
57
|
|
|
|
58
|
|
|
switch ($expectedResponse) { |
59
|
|
|
case self::VALID_RESPONSE: |
60
|
|
|
$this->response = $this->createResponseFromFile(__DIR__ . '/../../resources/eurofxref-daily-valid.xml'); |
61
|
|
|
break; |
62
|
|
|
case self::USD_MISSING_RESPONSE: |
63
|
|
|
$this->response = $this->createResponseFromFile(__DIR__ . '/../../resources/eurofxref-daily-usd-missing.xml'); |
64
|
|
|
break; |
65
|
|
|
case self::DATE_MISSING_RESPONSE: |
66
|
|
|
$this->response = $this->createResponseFromFile(__DIR__ . '/../../resources/eurofxref-daily-date-missing.xml'); |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws ExchangeRatesRequestFailedException |
73
|
|
|
* @return ExchangeRate[] |
74
|
|
|
*/ |
75
|
|
|
public function getExchangeRates(): array |
76
|
|
|
{ |
77
|
|
|
return $this->mapper->map($this->response); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $file |
82
|
|
|
* @return Response |
83
|
|
|
*/ |
84
|
|
|
private function createResponseFromFile(string $file): Response |
85
|
|
|
{ |
86
|
|
|
return new Response(200, [], file_get_contents($file)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |