Issues

ZF-9519: Zend_Currency::setValue() fails when value param is scalar and currency param passed

Description

If the $value param for setValue() is not an instance of Zend_Currency, setValue() call results in exception in Zend_Currency::_checkParams().

Code:


class ExchangerService implements Zend_Currency_CurrencyInterface
{
    public function getRate($from, $to)
    {
        if ('RUB' == $from && 'USD' == $to)
            return 0.033333;
        elseif ('USD' == $from && 'RUB' == $to)
            return 30;

        return 1;
    }
}

$zc = new Zend_Currency('RUB', 'ru_RU');
$zc->setService(new ExchangerService());
$zc->setValue(100, 'USD');
echo $zc->toCurrency();

Result:


Zend_Currency_Exception: No region found within the locale ''
thrown in E:\WWW\libs\Zend\Currency.php on line 310

Stack trace:
#0 E:\WWW\libs\Zend\Currency.php(366): Zend_Currency->_checkParams('USD', NULL)
#1 E:\WWW\libs\Zend\Currency.php(752): Zend_Currency->getShortName('USD')
#2 E:\WWW\libs\Zend\Currency.php(593): Zend_Currency->_exchangeCurrency(100, 'USD')
#3 E:\SomeSourceFile.php(8): Zend_Currency->setValue(100, 'USD')

The same exception for the following code:


$zc->setValue(100, new Zend_Currency('USD', 'ru_RU'));

tested on 1.10.2, r21613

Comments

Fixed with r21616

short name assertion in test is erroneous, setValue shouldn't change currency, it's just set numeric value, so short name should stay unchanged — RUB.


E:\Dev\_SVN\Checkout\Zend\tests\Zend>phpunit CurrencyTest.php
PHPUnit 3.4.11 by Sebastian Bergmann.

................................F

Time: 33 seconds, Memory: 6.25Mb

There was 1 failure:

1) Zend_CurrencyTest::testSetValueWithoutLocale
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-USD
+RUB

E:\Dev\_SVN\Checkout\Zend\tests\Zend\CurrencyTest.php:817

FAILURES!
Tests: 33, Assertions: 149, Failures: 1.