public function testFetchRates() { $currencyFromList = ['USD']; $currencyToList = ['EUR', 'UAH']; $responseBody = '{"base":"USD","date":"2015-10-07","rates":{"EUR":0.9022}}'; $expectedCurrencyRateList = ['USD' => ['EUR' => 0.9022, 'UAH' => null]]; $message = "We can't retrieve a rate from http://api.fixer.io/latest?base=USD&symbols=EUR,UAH for UAH."; /** @var \Magento\Directory\Model\Currency|\PHPUnit_Framework_MockObject_MockObject $currencyMock */ $currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->setMethods([])->getMock(); /** @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject $currencyMock */ $httpClientMock = $this->getMockBuilder('Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->setMethods([])->getMock(); /** @var \Zend_Http_Response|\PHPUnit_Framework_MockObject_MockObject $currencyMock */ $httpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->setMethods([])->getMock(); $this->currencyFactoryMock->expects($this->any())->method('create')->willReturn($currencyMock); $currencyMock->expects($this->once())->method('getConfigBaseCurrencies')->willReturn($currencyFromList); $currencyMock->expects($this->once())->method('getConfigAllowCurrencies')->willReturn($currencyToList); $this->httpClientFactoryMock->expects($this->any())->method('create')->willReturn($httpClientMock); $httpClientMock->expects($this->atLeastOnce())->method('setUri')->willReturnSelf(); $httpClientMock->expects($this->atLeastOnce())->method('setConfig')->willReturnSelf(); $httpClientMock->expects($this->atLeastOnce())->method('request')->willReturn($httpResponseMock); $httpResponseMock->expects($this->any())->method('getBody')->willReturn($responseBody); $this->assertEquals($expectedCurrencyRateList, $this->model->fetchRates()); $messages = $this->model->getMessages(); $this->assertNotEmpty($messages); $this->assertTrue(is_array($messages)); $this->assertEquals($message, (string) $messages[0]); }
public function testFetchRates() { $currencyFromList = ['USD']; $currencyToList = ['EUR', 'UAH']; $responseBody = '{"query":{"count":7,"created":"2016-04-05T16:46:55Z","lang":"en-US","results":{"rate":' . '[{"id":"USDEUR","Name":"USD/EUR","Rate":"0.9022","Date":"4/5/2016"}]}}}'; $expectedCurrencyRateList = ['USD' => ['EUR' => 0.9022, 'UAH' => null]]; $message = "We can't retrieve a rate from http://query.yahooapis.com/v1/public/yql?format=json" . "&q=select+*+from+yahoo.finance.xchange+where+pair+in+%28%22USDEUR%22%2C%22USDUAH%22)" . "&env=store://datatables.org/alltableswithkeys for UAH."; /** @var \Magento\Directory\Model\Currency|\PHPUnit_Framework_MockObject_MockObject $currencyMock */ $currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->setMethods([])->getMock(); /** @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject $currencyMock */ $httpClientMock = $this->getMockBuilder('Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->setMethods([])->getMock(); /** @var \Zend_Http_Response|\PHPUnit_Framework_MockObject_MockObject $currencyMock */ $httpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->setMethods([])->getMock(); $this->currencyFactoryMock->expects($this->any())->method('create')->willReturn($currencyMock); $currencyMock->expects($this->once())->method('getConfigBaseCurrencies')->willReturn($currencyFromList); $currencyMock->expects($this->once())->method('getConfigAllowCurrencies')->willReturn($currencyToList); $this->httpClientFactoryMock->expects($this->any())->method('create')->willReturn($httpClientMock); $httpClientMock->expects($this->atLeastOnce())->method('setUri')->willReturnSelf(); $httpClientMock->expects($this->atLeastOnce())->method('setConfig')->willReturnSelf(); $httpClientMock->expects($this->atLeastOnce())->method('request')->willReturn($httpResponseMock); $httpResponseMock->expects($this->any())->method('getBody')->willReturn($responseBody); $this->assertEquals($expectedCurrencyRateList, $this->model->fetchRates()); $messages = $this->model->getMessages(); $this->assertNotEmpty($messages); $this->assertTrue(is_array($messages)); $this->assertEquals($message, (string) $messages[0]); }
public function testConvertWithCurrencyString() { $amount = 5.6; $currency = 'ru'; $convertedAmount = 9.300000000000001; $currentCurrency = $this->getCurrentCurrencyMock(); $currentCurrency->expects($this->once())->method('load')->with($currency)->will($this->returnSelf()); $this->currencyFactory->expects($this->once())->method('create')->will($this->returnValue($currentCurrency)); $baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency); $baseCurrency->expects($this->once())->method('getRate')->with($currentCurrency)->will($this->returnValue(1.2)); $store = $this->getStoreMock($baseCurrency); $this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency)); }
public function testScheduledUpdateCurrencyRates() { $this->scopeConfig->expects($this->at(0))->method('getValue')->with(Observer::IMPORT_ENABLE, ScopeInterface::SCOPE_STORE)->will($this->returnValue(1)); $this->scopeConfig->expects($this->at(1))->method('getValue')->with(Observer::CRON_STRING_PATH, ScopeInterface::SCOPE_STORE)->will($this->returnValue('cron-path')); $this->scopeConfig->expects($this->at(2))->method('getValue')->with(Observer::IMPORT_SERVICE, ScopeInterface::SCOPE_STORE)->will($this->returnValue('import-service')); $importInterfaceMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency\\Import\\Webservicex')->disableOriginalConstructor()->setMethods(['fetchRates', 'getMessages'])->getMock(); $importInterfaceMock->expects($this->once())->method('fetchRates')->will($this->returnValue([])); $importInterfaceMock->expects($this->once())->method('getMessages')->will($this->returnValue([])); $this->importFactory->expects($this->once())->method('create')->with('import-service')->will($this->returnValue($importInterfaceMock)); $currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->setMethods(['saveRates', '__wakeup', '__sleep'])->getMock(); $currencyMock->expects($this->once())->method('saveRates')->will($this->returnValue(null)); $this->currencyFactory->expects($this->once())->method('create')->will($this->returnValue($currencyMock)); $this->observer->scheduledUpdateCurrencyRates(null); }