-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #623 from magento-api/pull-request
[API] Bug Fixes
- Loading branch information
Showing
38 changed files
with
499 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
app/code/Magento/Directory/Model/Currency/Import/YahooFinance.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Directory\Model\Currency\Import; | ||
|
||
/** | ||
* Currency rate import model (From http://query.yahooapis.com/) | ||
*/ | ||
class YahooFinance extends \Magento\Directory\Model\Currency\Import\AbstractImport | ||
{ | ||
/** | ||
* Currency converter url string | ||
* | ||
* @var string | ||
*/ | ||
private $currencyConverterUrl = 'http://query.yahooapis.com/v1/public/yql?format=json&q={{YQL_STRING}}' | ||
.'&env=store://datatables.org/alltableswithkeys'; | ||
|
||
/** | ||
* Config path for service timeout | ||
* | ||
* @var string | ||
*/ | ||
private $timeoutConfigPath = 'currency/yahoofinance/timeout'; | ||
|
||
/** | ||
* Http Client Factory | ||
* | ||
* @var \Magento\Framework\HTTP\ZendClientFactory | ||
*/ | ||
protected $httpClientFactory; | ||
|
||
/** | ||
* Core scope config | ||
* | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* Initialize dependencies | ||
* | ||
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory | ||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig | ||
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory | ||
*/ | ||
public function __construct( | ||
\Magento\Directory\Model\CurrencyFactory $currencyFactory, | ||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | ||
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory | ||
) { | ||
parent::__construct($currencyFactory); | ||
$this->scopeConfig = $scopeConfig; | ||
$this->httpClientFactory = $httpClientFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fetchRates() | ||
{ | ||
$data = []; | ||
$currencies = $this->_getCurrencyCodes(); | ||
$defaultCurrencies = $this->_getDefaultCurrencyCodes(); | ||
|
||
foreach ($defaultCurrencies as $currencyFrom) { | ||
if (!isset($data[$currencyFrom])) { | ||
$data[$currencyFrom] = []; | ||
} | ||
$data = $this->convertBatch($data, $currencyFrom, $currencies); | ||
ksort($data[$currencyFrom]); | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* Return currencies convert rates in batch mode | ||
* | ||
* @param array $data | ||
* @param string $currencyFrom | ||
* @param array $currenciesTo | ||
* @return array | ||
*/ | ||
private function convertBatch($data, $currencyFrom, $currenciesTo) | ||
{ | ||
$url = $this->buildUrl($currencyFrom, $currenciesTo); | ||
set_time_limit(0); | ||
try { | ||
$response = $this->getServiceResponse($url); | ||
} finally { | ||
ini_restore('max_execution_time'); | ||
} | ||
|
||
foreach ($currenciesTo as $currencyTo) { | ||
if ($currencyFrom == $currencyTo) { | ||
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(1); | ||
} else { | ||
if (empty($response[$currencyFrom . $currencyTo])) { | ||
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $currencyTo); | ||
$data[$currencyFrom][$currencyTo] = null; | ||
} else { | ||
$data[$currencyFrom][$currencyTo] = $this->_numberFormat( | ||
(double)$response[$currencyFrom . $currencyTo] | ||
); | ||
} | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* Get Fixer.io service response | ||
* | ||
* @param string $url | ||
* @param int $retry | ||
* @return array | ||
*/ | ||
private function getServiceResponse($url, $retry = 0) | ||
{ | ||
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */ | ||
$httpClient = $this->httpClientFactory->create(); | ||
$response = []; | ||
try { | ||
$jsonResponse = $httpClient->setUri( | ||
$url | ||
)->setConfig( | ||
[ | ||
'timeout' => $this->scopeConfig->getValue( | ||
$this->timeoutConfigPath, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
), | ||
] | ||
)->request( | ||
'GET' | ||
)->getBody(); | ||
|
||
$jsonResponse = json_decode($jsonResponse, true); | ||
if (!empty($jsonResponse['query']['results']['rate'])) { | ||
$response = array_column($jsonResponse['query']['results']['rate'], 'Rate', 'id'); | ||
} | ||
} catch (\Exception $e) { | ||
if ($retry == 0) { | ||
$response = $this->getServiceResponse($url, 1); | ||
} | ||
} | ||
return $response; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function _convert($currencyFrom, $currencyTo) | ||
{ | ||
} | ||
|
||
/** | ||
* Build url for Currency Service | ||
* | ||
* @param string $currencyFrom | ||
* @param string[] $currenciesTo | ||
* @return string | ||
*/ | ||
private function buildUrl($currencyFrom, $currenciesTo) | ||
{ | ||
$query = urlencode('select ') . '*' . urlencode(' from yahoo.finance.xchange where pair in ('); | ||
$query .= | ||
urlencode( | ||
implode( | ||
',', | ||
array_map( | ||
function ($currencyTo) use ($currencyFrom) { | ||
return '"' . $currencyFrom . $currencyTo . '"'; | ||
}, | ||
$currenciesTo | ||
) | ||
) | ||
); | ||
$query .= ')'; | ||
return str_replace('{{YQL_STRING}}', $query, $this->currencyConverterUrl); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
app/code/Magento/Directory/Test/Unit/Model/Currency/Import/YahooFinanceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Directory\Test\Unit\Model\Currency\Import; | ||
|
||
class YahooFinanceTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Magento\Directory\Model\Currency\Import\YahooFinance | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $currencyFactoryMock; | ||
|
||
/** | ||
* @var \Magento\Framework\HTTP\ZendClientFactory|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $httpClientFactoryMock; | ||
|
||
protected function setUp() | ||
{ | ||
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); | ||
|
||
$this->currencyFactoryMock = $this->getMockBuilder('Magento\Directory\Model\CurrencyFactory') | ||
->disableOriginalConstructor() | ||
->setMethods(['create']) | ||
->getMock(); | ||
$this->httpClientFactoryMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClientFactory') | ||
->disableOriginalConstructor() | ||
->setMethods(['create']) | ||
->getMock(); | ||
$scopeMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface') | ||
->disableOriginalConstructor() | ||
->setMethods([]) | ||
->getMock(); | ||
|
||
$this->model = $objectManagerHelper->getObject( | ||
'Magento\Directory\Model\Currency\Import\YahooFinance', | ||
[ | ||
'currencyFactory' => $this->currencyFactoryMock, | ||
'scopeConfig' => $scopeMock, | ||
'httpClientFactory' => $this->httpClientFactoryMock | ||
] | ||
); | ||
} | ||
|
||
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.