Skip to content

Commit c371687

Browse files
author
Oleksii Korshenko
authored
MAGETWO-85659: 6965: \Magento\Directory\Model\PriceCurrency::format() fails without conversion rate #1022
2 parents ea1ebff + 1c77011 commit c371687

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

app/code/Magento/Directory/Model/PriceCurrency.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public function format(
7777
$scope = null,
7878
$currency = null
7979
) {
80-
return $this->getCurrency($scope, $currency)
81-
->formatPrecision($amount, $precision, [], $includeContainer);
80+
return $this->createCurrency($scope, $currency)->formatPrecision($amount, $precision, [], $includeContainer);
8281
}
8382

8483
/**
@@ -101,20 +100,7 @@ public function convertAndFormat(
101100
*/
102101
public function getCurrency($scope = null, $currency = null)
103102
{
104-
if ($currency instanceof Currency) {
105-
$currentCurrency = $currency;
106-
} elseif (is_string($currency)) {
107-
$currency = $this->currencyFactory->create()
108-
->load($currency);
109-
$baseCurrency = $this->getStore($scope)
110-
->getBaseCurrency();
111-
$currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency;
112-
} else {
113-
$currentCurrency = $this->getStore($scope)
114-
->getCurrentCurrency();
115-
}
116-
117-
return $currentCurrency;
103+
return $this->createCurrency($scope, $currency, true);
118104
}
119105

120106
/**
@@ -157,4 +143,30 @@ public function round($price)
157143
{
158144
return round($price, 2);
159145
}
146+
147+
/**
148+
* Get currency considering currency rate configuration.
149+
*
150+
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
151+
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
152+
* @param bool $includeRate
153+
*
154+
* @return Currency
155+
*/
156+
private function createCurrency($scope, $currency, bool $includeRate = false)
157+
{
158+
if ($currency instanceof Currency) {
159+
$currentCurrency = $currency;
160+
} elseif (is_string($currency)) {
161+
$currentCurrency = $this->currencyFactory->create()->load($currency);
162+
if ($includeRate) {
163+
$baseCurrency = $this->getStore($scope)->getBaseCurrency();
164+
$currentCurrency = $baseCurrency->getRate($currentCurrency) ? $currentCurrency : $baseCurrency;
165+
}
166+
} else {
167+
$currentCurrency = $this->getStore($scope)->getCurrentCurrency();
168+
}
169+
170+
return $currentCurrency;
171+
}
160172
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Directory\Model;
8+
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* Provide tests for PriceCurrency model.
14+
*/
15+
class PriceCurrencyTest extends TestCase
16+
{
17+
/**
18+
* Test subject.
19+
*
20+
* @var PriceCurrency
21+
*/
22+
private $priceCurrency;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
$this->priceCurrency = Bootstrap::getObjectManager()->get(PriceCurrency::class);
30+
}
31+
32+
/**
33+
* Check PriceCurrency::format() doesn't depend on currency rate configuration.
34+
* @return void
35+
*/
36+
public function testFormat()
37+
{
38+
self::assertSame(
39+
'<span class="price">AFN10.00</span>',
40+
$this->priceCurrency->format(10, true, 2, null, 'AFN')
41+
);
42+
}
43+
}

0 commit comments

Comments
 (0)