Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FixerIoTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Directory\Model\CurrencyFactory;
16 use Magento\Framework\HTTP\ZendClientFactory;
17 use PHPUnit_Framework_MockObject_MockObject as MockObject;
18 
22 class FixerIoTest extends \PHPUnit\Framework\TestCase
23 {
27  private $model;
28 
32  private $currencyFactory;
33 
37  private $httpClientFactory;
38 
42  private $scopeConfig;
43 
47  protected function setUp()
48  {
49  $this->currencyFactory = $this->getMockBuilder(CurrencyFactory::class)
50  ->disableOriginalConstructor()
51  ->setMethods(['create'])
52  ->getMock();
53  $this->httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
54  ->disableOriginalConstructor()
55  ->setMethods(['create'])
56  ->getMock();
57  $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
58  ->disableOriginalConstructor()
59  ->setMethods([])
60  ->getMock();
61 
62  $this->model = new FixerIo($this->currencyFactory, $this->scopeConfig, $this->httpClientFactory);
63  }
64 
70  public function testFetchRates(): void
71  {
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.";
78 
79  $this->scopeConfig->method('getValue')
80  ->withConsecutive(
81  ['currency/fixerio/api_key', 'store'],
82  ['currency/fixerio/timeout', 'store']
83  )
84  ->willReturnOnConsecutiveCalls('api_key', 100);
85 
87  $currency = $this->getMockBuilder(Currency::class)
88  ->disableOriginalConstructor()
89  ->getMock();
91  $httpClient = $this->getMockBuilder(ZendClient::class)
92  ->disableOriginalConstructor()
93  ->getMock();
95  $httpResponse = $this->getMockBuilder(DataObject::class)
96  ->disableOriginalConstructor()
97  ->setMethods(['getBody'])
98  ->getMock();
99 
100  $this->currencyFactory->method('create')
101  ->willReturn($currency);
102  $currency->method('getConfigBaseCurrencies')
103  ->willReturn($currencyFromList);
104  $currency->method('getConfigAllowCurrencies')
105  ->willReturn($currencyToList);
106 
107  $this->httpClientFactory->method('create')
108  ->willReturn($httpClient);
109  $httpClient->method('setUri')
110  ->willReturnSelf();
111  $httpClient->method('setConfig')
112  ->willReturnSelf();
113  $httpClient->method('request')
114  ->willReturn($httpResponse);
115  $httpResponse->method('getBody')
116  ->willReturn($responseBody);
117 
118  self::assertEquals($expectedCurrencyRateList, $this->model->fetchRates());
119 
120  $messages = $this->model->getMessages();
121  self::assertNotEmpty($messages);
122  self::assertTrue(is_array($messages));
123  self::assertEquals($message, (string)$messages[0]);
124  }
125 }
$message