Skip to content

Commit

Permalink
MAGETWO-85659: 6965: \Magento\Directory\Model\PriceCurrency::format()…
Browse files Browse the repository at this point in the history
… fails without conversion rate #1022
  • Loading branch information
Oleksii Korshenko authored Dec 22, 2017
2 parents ea1ebff + 1c77011 commit c371687
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
44 changes: 28 additions & 16 deletions app/code/Magento/Directory/Model/PriceCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public function format(
$scope = null,
$currency = null
) {
return $this->getCurrency($scope, $currency)
->formatPrecision($amount, $precision, [], $includeContainer);
return $this->createCurrency($scope, $currency)->formatPrecision($amount, $precision, [], $includeContainer);
}

/**
Expand All @@ -101,20 +100,7 @@ public function convertAndFormat(
*/
public function getCurrency($scope = null, $currency = null)
{
if ($currency instanceof Currency) {
$currentCurrency = $currency;
} elseif (is_string($currency)) {
$currency = $this->currencyFactory->create()
->load($currency);
$baseCurrency = $this->getStore($scope)
->getBaseCurrency();
$currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency;
} else {
$currentCurrency = $this->getStore($scope)
->getCurrentCurrency();
}

return $currentCurrency;
return $this->createCurrency($scope, $currency, true);
}

/**
Expand Down Expand Up @@ -157,4 +143,30 @@ public function round($price)
{
return round($price, 2);
}

/**
* Get currency considering currency rate configuration.
*
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
* @param bool $includeRate
*
* @return Currency
*/
private function createCurrency($scope, $currency, bool $includeRate = false)
{
if ($currency instanceof Currency) {
$currentCurrency = $currency;
} elseif (is_string($currency)) {
$currentCurrency = $this->currencyFactory->create()->load($currency);
if ($includeRate) {
$baseCurrency = $this->getStore($scope)->getBaseCurrency();
$currentCurrency = $baseCurrency->getRate($currentCurrency) ? $currentCurrency : $baseCurrency;
}
} else {
$currentCurrency = $this->getStore($scope)->getCurrentCurrency();
}

return $currentCurrency;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Directory\Model;

use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Provide tests for PriceCurrency model.
*/
class PriceCurrencyTest extends TestCase
{
/**
* Test subject.
*
* @var PriceCurrency
*/
private $priceCurrency;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->priceCurrency = Bootstrap::getObjectManager()->get(PriceCurrency::class);
}

/**
* Check PriceCurrency::format() doesn't depend on currency rate configuration.
* @return void
*/
public function testFormat()
{
self::assertSame(
'<span class="price">AFN10.00</span>',
$this->priceCurrency->format(10, true, 2, null, 'AFN')
);
}
}

0 comments on commit c371687

Please sign in to comment.