1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SteffenBrand\CurrCurr\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use GuzzleHttp\Psr7\Stream; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use SteffenBrand\CurrCurr\Exception\ExchangeRatesRequestFailedException; |
9
|
|
|
use SteffenBrand\CurrCurr\Mapper\ExchangeRatesMapper; |
10
|
|
|
use SteffenBrand\CurrCurr\Model\ExchangeRate; |
11
|
|
|
|
12
|
|
|
class EcbClientMock implements EcbClientInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
const VALID_RESPONSE = 'ValidResponse'; |
16
|
|
|
const USD_MISSING_RESPONSE = 'UsdMissingResponse'; |
17
|
|
|
const DATE_MISSING_RESPONSE = 'DateMissingResponse'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ResponseInterface |
21
|
|
|
*/ |
22
|
|
|
private $response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $expectedResponse |
26
|
|
|
*/ |
27
|
|
|
public function __construct(string $expectedResponse) |
28
|
|
|
{ |
29
|
|
|
switch ($expectedResponse) { |
30
|
|
|
case self::VALID_RESPONSE: |
31
|
|
|
$this->response = $this->createResponseFromFile(__DIR__ . '/../../resources/eurofxref-daily-valid.xml'); |
32
|
|
|
break; |
33
|
|
|
case self::USD_MISSING_RESPONSE: |
34
|
|
|
$this->response = $this->createResponseFromFile(__DIR__ . '/../../resources/eurofxref-daily-usd-missing.xml'); |
35
|
|
|
break; |
36
|
|
|
case self::DATE_MISSING_RESPONSE: |
37
|
|
|
$this->response = $this->createResponseFromFile(__DIR__ . '/../../resources/eurofxref-daily-date-missing.xml'); |
38
|
|
|
break; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws ExchangeRatesRequestFailedException |
44
|
|
|
* @return ExchangeRate[] |
45
|
|
|
*/ |
46
|
|
|
public function getExchangeRates(): array |
47
|
|
|
{ |
48
|
|
|
$mapper = new ExchangeRatesMapper(); |
49
|
|
|
return $mapper->map($this->response); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $file |
54
|
|
|
* @return Response |
55
|
|
|
*/ |
56
|
|
|
private function createResponseFromFile(string $file) |
57
|
|
|
{ |
58
|
|
|
return new Response(200, [], file_get_contents($file)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |