6 declare(strict_types=1);
12 use Magento\Directory\Model\CurrencyFactory;
16 use Magento\Framework\HTTP\ZendClientFactory;
17 use PHPUnit_Framework_MockObject_MockObject as MockObject;
32 private $currencyFactory;
37 private $httpClientFactory;
49 $this->currencyFactory = $this->getMockBuilder(CurrencyFactory::class)
50 ->disableOriginalConstructor()
51 ->setMethods([
'create'])
53 $this->httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
54 ->disableOriginalConstructor()
55 ->setMethods([
'create'])
57 $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
58 ->disableOriginalConstructor()
62 $this->model =
new FixerIo($this->currencyFactory, $this->scopeConfig, $this->httpClientFactory);
70 public function testFetchRates(): void
72 $currencyFromList = [
'USD'];
73 $currencyToList = [
'EUR',
'UAH'];
74 $responseBody =
'{"success":"true","base":"USD","date":"2015-10-07","rates":{"EUR":0.9022}}';
75 $expectedCurrencyRateList = [
'USD' => [
'EUR' => 0.9022,
'UAH' =>
null]];
76 $message =
"We can't retrieve a rate from " 77 .
"http://data.fixer.io/api/latest?access_key=api_key&base=USD&symbols=EUR,UAH for UAH.";
79 $this->scopeConfig->method(
'getValue')
81 [
'currency/fixerio/api_key',
'store'],
82 [
'currency/fixerio/timeout',
'store']
84 ->willReturnOnConsecutiveCalls(
'api_key', 100);
87 $currency = $this->getMockBuilder(Currency::class)
88 ->disableOriginalConstructor()
91 $httpClient = $this->getMockBuilder(ZendClient::class)
92 ->disableOriginalConstructor()
95 $httpResponse = $this->getMockBuilder(DataObject::class)
96 ->disableOriginalConstructor()
97 ->setMethods([
'getBody'])
100 $this->currencyFactory->method(
'create')
101 ->willReturn($currency);
102 $currency->method(
'getConfigBaseCurrencies')
103 ->willReturn($currencyFromList);
104 $currency->method(
'getConfigAllowCurrencies')
105 ->willReturn($currencyToList);
107 $this->httpClientFactory->method(
'create')
108 ->willReturn($httpClient);
109 $httpClient->method(
'setUri')
111 $httpClient->method(
'setConfig')
113 $httpClient->method(
'request')
114 ->willReturn($httpResponse);
115 $httpResponse->method(
'getBody')
116 ->willReturn($responseBody);
118 self::assertEquals($expectedCurrencyRateList, $this->model->fetchRates());
120 $messages = $this->model->getMessages();
121 self::assertNotEmpty($messages);
122 self::assertTrue(is_array($messages));
123 self::assertEquals(
$message, (
string)$messages[0]);