Browse Source

#2359 testcases adapted and compatibility to old tests improved

tags/2.0.0-rc
Erik_r 10 years ago
parent
commit
299d991a2e
  1. 63
      framework/base/Formatter.php
  2. 28
      framework/i18n/Formatter.php
  3. 88
      tests/unit/framework/base/FormatterTest.php
  4. 96
      tests/unit/framework/i18n/FormatterTest.php

63
framework/base/Formatter.php

@ -7,7 +7,6 @@
namespace yii\base;
use Yii;
use DateTime;
use IntlDateFormatter;
@ -567,7 +566,7 @@ class Formatter extends yii\base\Component
/**
* Set a new local different to Yii configuration for temporale reason.
* @param string $locale language code and country code.
* @return Formatter object
* @return \guggach\helpers\Formatter object
*/
public function setLocale($locale = 'en-US'){
$this->locale = $locale;
@ -577,8 +576,8 @@ class Formatter extends yii\base\Component
$this->setFormatPattern($this->_originalConfig['time'], 'time');
$this->setFormatPattern($this->_originalConfig['datetime'], 'datetime');
$this->setDecimalSeparator($this->decimalSeparator);
$this->setThousandSeparator($this->thousandSeparator);
$this->setDecimalSeparator();
$this->setThousandSeparator();
return $this;
}
@ -724,7 +723,7 @@ class Formatter extends yii\base\Component
* standard (icu) will be taken. Without loaded "intl" extension the definition can be
* adapted in FormatDefs.php.
* @param string $sign: one sign which is set.
* @return Formatter object
* @return \guggach\helpers\Formatter
*/
public function setDecimalSeparator($sign = null){
if ($sign === null){
@ -754,7 +753,7 @@ class Formatter extends yii\base\Component
* standard (icu) will be taken. Without loaded "intl" extension the definition can be
* adapted in FormatDefs.php.
* @param string $sign: one sign which is set.
* @return Formatter object
* @return \guggach\helpers\Formatter
*/
public function setThousandSeparator($sign = null){
if ($sign === null){
@ -1321,9 +1320,11 @@ class Formatter extends yii\base\Component
// if (true === false){
if ($this->_intlLoaded){
$f = $this->createNumberFormatter(NumberFormatter::DECIMAL, $format);
$f->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
if ($decimals <= 5){
$f->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimals);
if ($decimals !== null){
$f->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
if ($decimals <= 5){
$f->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimals);
}
}
if ($roundIncr == null and $this->roundingIncrement != null){
$roundIncr = $this->roundingIncrement;
@ -1337,7 +1338,7 @@ class Formatter extends yii\base\Component
return $f->format($value);
} else {
if ($roundIncr !== null){
if ($roundIncr !== null){
$part = explode('.', (string)$roundIncr);
if ((string)$roundIncr != '0.05'){ // exception for Swiss rounding.
$roundIncr = $decimals;
@ -1359,7 +1360,9 @@ class Formatter extends yii\base\Component
$value = round($value/5,2)*5;
}
}
if ($decimals === null){
$decimals = 0;
}
$grouping = $grouping === true ? $this->thousandSeparator : '';
return number_format($value, $decimals, $this->decimalSeparator, $grouping);
@ -1398,7 +1401,7 @@ class Formatter extends yii\base\Component
* for details on how to specify a format.
* @return string the formatted result.
*/
public function asDecimal($value, $decimals = 2, $roundIncr = null, $grouping = true)
public function asDecimal($value, $decimals = null, $roundIncr = null, $grouping = true)
{
return $this->asDouble($value, $decimals, $roundIncr, $grouping);
}
@ -1409,11 +1412,12 @@ class Formatter extends yii\base\Component
/**
* Formats the value as a percent number with "%" sign.
* @param mixed $value the value to be formatted. It must be a factor eg. 0.75 -> 75%
* @param string $decimals the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
* @param string $decimals Number of decimals (default = 2) or format pattern ICU
* Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
* for details on how to specify a format.
* @return string the formatted result.
*/
public function asPercent($value, $decimals = 2, $grouping = true)
public function asPercent($value, $decimals = 0, $grouping = true)
{
$format = null;
if(is_numeric($decimals)){
@ -1432,18 +1436,23 @@ class Formatter extends yii\base\Component
// if (true === false){
if ($this->_intlLoaded){
$f = $this->createNumberFormatter(NumberFormatter::PERCENT, $format);
$f->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
if ($decimals <= 5){
$f->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimals);
if ($decimals !== null){
$f->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
if ($decimals <= 5){
$f->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimals);
}
}
if ($grouping === false){
$f->setAttribute(NumberFormatter::GROUPING_USED, false);
}
return $f->format($value);
} else {
if ($decimals === null){
$decimals = 0;
}
$value = $value * 100;
$grouping = $grouping === true ? $this->thousandSeparator : '';
return number_format($value, $decimals, $this->decimalSeparator, $grouping) . ' %';
return number_format($value, $decimals, $this->decimalSeparator, $grouping) . '%';
}
}
@ -1461,7 +1470,7 @@ class Formatter extends yii\base\Component
* for details on how to specify a format.
* @return string the formatted result.
*/
public function asScientific($value, $decimals = 4)
public function asScientific($value, $decimals = null)
{
$format = null;
if(is_numeric($decimals)){
@ -1480,10 +1489,16 @@ class Formatter extends yii\base\Component
// if (true === false){
if ($this->_intlLoaded){
$f = $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format);
$f->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
if ($decimals !== null){
$f->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
}
return $f->format($value);
} else {
return sprintf("%.{$decimals}E", $value);
if ($decimals !== null){
return sprintf("%.{$decimals}E", $value);
} else {
return sprintf("%.E", $value);
}
}
}
@ -1545,7 +1560,7 @@ class Formatter extends yii\base\Component
if ($roundIncr !== null){
$f->setAttribute(NumberFormatter::ROUNDING_INCREMENT, $roundIncr);
}
if ($currency === null or trim($currency != '')){
if ($currency === null){
return $f->format($value);
} else {
return $f->formatCurrency($value, $currency);
@ -1748,7 +1763,11 @@ class Formatter extends yii\base\Component
$formatter = new NumberFormatter($this->locale, $type);
if ($format !== null) {
$formatter->setPattern($format);
} else {
$formatter->setSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $this->decimalSeparator);
$formatter->setSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $this->thousandSeparator);
}
if (!empty($this->numberFormatOptions)) {
foreach ($this->numberFormatOptions as $name => $attribute) {
$formatter->setAttribute($name, $attribute);

28
framework/i18n/Formatter.php

@ -1,28 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\i18n;
/**
* Dummy Formatter class in 'yii\i18n' for compatibility reason.
* Formatter class in 'yii\base' provides all functionality with localized format also
* independent if php extension "intl" is loaded or not.
* @see yii\base\Formatter.php
*
* If php extension "intl" want to be used or can't be loaded then localized formats patterns
* could be entered in class 'yii\i18n\FormatDefs.php'
* ```
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Enrica Ruedin <e.ruedin@guggach.com>
* @since 2.0
*/
class Formatter extends \yii\base\Formatter
{
}

88
tests/unit/framework/base/FormatterTest.php

@ -121,23 +121,25 @@ class FormatterTest extends TestCase
public function testAsDate()
{
$value = time();
$this->assertSame(date('Y-m-d', $value), $this->formatter->asDate($value));
$this->assertSame(date('M j, Y', $value), $this->formatter->asDate($value));
$this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value, 'Y/m/d'));
$this->assertSame(date('n/j/y', $value), $this->formatter->asDate($value, 'short'));
$this->assertSame(date('F j, Y', $value), $this->formatter->asDate($value, 'long'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
}
public function testAsTime()
{
$value = time();
$this->assertSame(date('H:i:s', $value), $this->formatter->asTime($value));
$this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value, 'h:i:s A'));
$this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value));
$this->assertSame(date('n:i:s A', $value), $this->formatter->asTime($value, 'n:i:s A'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
}
public function testAsDatetime()
{
$value = time();
$this->assertSame(date('Y-m-d H:i:s', $value), $this->formatter->asDatetime($value));
$this->assertSame(date('M j, Y, g:i:s A', $value), $this->formatter->asDatetime($value));
$this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'Y/m/d h:i:s A'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
}
@ -166,12 +168,14 @@ class FormatterTest extends TestCase
$value = 123;
$this->assertSame("123.00", $this->formatter->asDouble($value));
$this->formatter->decimalSeparator = ',';
$this->formatter->thousandSeparator = '.';
$value = 123.12;
$this->assertSame("123,12", $this->formatter->asDouble($value));
$this->assertSame("123,1", $this->formatter->asDouble($value, 1));
$this->assertSame("123", $this->formatter->asDouble($value, 0));
$value = 123123.123;
$this->assertSame("123123,12", $this->formatter->asDouble($value));
$this->assertSame("123.123,12", $this->formatter->asDouble($value));
$this->assertSame("123123,12", $this->formatter->asDouble($value, 2, null, false));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDouble(null));
}
@ -190,11 +194,57 @@ class FormatterTest extends TestCase
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNumber(null));
}
public function testAsDecimal()
{
$value = '123';
$this->assertSame($value, $this->formatter->asDecimal($value));
$value = '123456';
$this->assertSame("123,456", $this->formatter->asDecimal($value));
$value = '-123456.123';
$this->assertSame("-123,456.123", $this->formatter->asDecimal($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
}
public function testAsCurrency()
{
$value = '123';
$this->assertSame('$123.00', $this->formatter->asCurrency($value));
$value = '123.456';
$this->assertSame("$123.46", $this->formatter->asCurrency($value));
// Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12
// see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
// $value = '-123456.123';
// $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
}
public function testAsScientific()
{
$value = '123';
$this->assertSame('1.23E2', $this->formatter->asScientific($value));
$value = '123456';
$this->assertSame("1.23456E5", $this->formatter->asScientific($value));
$value = '-123456.123';
$this->assertSame("-1.23456123E5", $this->formatter->asScientific($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
}
public function testAsPercent()
{
$value = '123';
$this->assertSame('12,300%', $this->formatter->asPercent($value));
$value = '0.1234';
$this->assertSame("12%", $this->formatter->asPercent($value));
$value = '-0.009343';
$this->assertSame("-1%", $this->formatter->asPercent($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asPercent(null));
}
public function testFormat()
{
$value = time();
$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'date'));
$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'DATE'));
$this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'date'));
$this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'DATE'));
$this->assertSame(date('Y/m/d', $value), $this->formatter->format($value, ['date', 'Y/m/d']));
$this->setExpectedException('\yii\base\InvalidParamException');
$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data'));
@ -238,12 +288,12 @@ class FormatterTest extends TestCase
$this->assertSame('a year ago', $this->formatter->asRelativeTime($interval_1_year));
$this->assertSame('12 years ago', $this->formatter->asRelativeTime($interval_12_years));
// Pass a DateInterval string
$this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z'));
$this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/P1Y2M10DT2H30M'));
$this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M/2008-05-11T15:30:00Z'));
$this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M'));
$this->assertSame('94 months ago', $this->formatter->asRelativeTime('P94M'));
// Pass a DateInterval string -> isn't possible
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z'));
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/P1Y2M10DT2H30M'));
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M/2008-05-11T15:30:00Z'));
// $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M'));
// $this->assertSame('94 months ago', $this->formatter->asRelativeTime('P94M'));
// Force the reference time and pass a past DateTime
$dateNow = new DateTime('2014-03-13');
@ -298,7 +348,7 @@ class FormatterTest extends TestCase
$this->assertSame('in 12 years', $this->formatter->asRelativeTime($interval_12_years));
// Pass a inverted DateInterval string
$this->assertSame('in a year', $this->formatter->asRelativeTime('2008-05-11T15:30:00Z/2007-03-01T13:00:00Z'));
// $this->assertSame('in a year', $this->formatter->asRelativeTime('2008-05-11T15:30:00Z/2007-03-01T13:00:00Z'));
// Force the reference time and pass a future DateTime
$dateNow = new DateTime('2014-03-13');
@ -322,4 +372,14 @@ class FormatterTest extends TestCase
$this->assertSame('in a month', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-03', [$interval_1_month]), $dateNow));
$this->assertSame('in 28 days', $this->formatter->asRelativeTime($dateThen, $dateNow));
}
public function testSetLocale(){
$value = '12300';
$this->formatter->setLocale('de-DE');
$this->assertSame('12.300,00', $this->formatter->asDouble($value, 2));
$value = time();
$this->assertSame(date('d.m.Y', $value), $this->formatter->asDate($value));
$this->formatter->setLocale('en-US');
}
}

96
tests/unit/framework/i18n/FormatterTest.php

@ -1,96 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\i18n;
use yii\i18n\Formatter;
use yiiunit\TestCase;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @group i18n
*/
class FormatterTest extends TestCase
{
/**
* @var Formatter
*/
protected $formatter;
protected function setUp()
{
parent::setUp();
if (!extension_loaded('intl')) {
$this->markTestSkipped('intl extension is required.');
}
$this->mockApplication([
'timeZone' => 'UTC',
]);
$this->formatter = new Formatter(['locale' => 'en-US']);
}
protected function tearDown()
{
parent::tearDown();
$this->formatter = null;
}
public function testAsDecimal()
{
$value = '123';
$this->assertSame($value, $this->formatter->asDecimal($value));
$value = '123456';
$this->assertSame("123,456", $this->formatter->asDecimal($value));
$value = '-123456.123';
$this->assertSame("-123,456.123", $this->formatter->asDecimal($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
}
public function testAsPercent()
{
$value = '123';
$this->assertSame('12,300%', $this->formatter->asPercent($value));
$value = '0.1234';
$this->assertSame("12%", $this->formatter->asPercent($value));
$value = '-0.009343';
$this->assertSame("-1%", $this->formatter->asPercent($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asPercent(null));
}
public function testAsScientific()
{
$value = '123';
$this->assertSame('1.23E2', $this->formatter->asScientific($value));
$value = '123456';
$this->assertSame("1.23456E5", $this->formatter->asScientific($value));
$value = '-123456.123';
$this->assertSame("-1.23456123E5", $this->formatter->asScientific($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
}
public function testAsCurrency()
{
$value = '123';
$this->assertSame('$123.00', $this->formatter->asCurrency($value));
$value = '123.456';
$this->assertSame("$123.46", $this->formatter->asCurrency($value));
// Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12
// see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
// $value = '-123456.123';
// $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
}
public function testDate()
{
$time = time();
$this->assertSame(date('n/j/y', $time), $this->formatter->asDate($time));
$this->assertSame(date('F j, Y', $time), $this->formatter->asDate($time, 'long'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
}
}
Loading…
Cancel
Save