Commit d7b4ea85 by Carsten Brandt

refactored date formatting functions

- removed unformat methods, they do not belong here - removed db format, which is also not purpose of this class - refactored the whole set of methods to be simpler and better maintainable More unit tests needed.
parent 330b8c25
......@@ -4,7 +4,6 @@ Yii Framework 2 Change Log
2.0.0-rc under development
--------------------------
- Enh #2359: Refactored formatter class. One class with or without intl extension and PHP format pattern as standard. (Erik_r)
- Bug #1263: Fixed the issue that Gii and Debug modules might be affected by incompatible asset manager configuration (qiangxue)
- Bug #2314: Gii model generator does not generate correct relation type in some special case (qiangxue)
- Bug #2563: Theming is not working if the path map of the theme contains ".." or "." in the paths (qiangxue)
......@@ -100,6 +99,8 @@ Yii Framework 2 Change Log
- Enh: Added param `hideOnSinglePage` to `yii\widgets\LinkPager` (arturf)
- Enh: Added support for array attributes in `in` validator (creocoder)
- Enh: Improved `yii\helpers\Inflector::slug` to support more cases for Russian, Hebrew and special characters (samdark)
- Chg #2359: Refactored formatter class. One class with or without intl extension and PHP format pattern as standard (Erik_r, cebe)
- `yii\i18n\Formatter` functionality has been merged into `yii\base\Formatter`
- Chg #2898: `yii\console\controllers\AssetController` is now using hashes instead of timestamps (samdark)
- Chg #2913: RBAC `DbManager` is now initialized via migration (samdark)
- Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue)
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -4,6 +4,20 @@
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
// override information about intl
use yiiunit\framework\base\FormatterTest;
function extension_loaded($name)
{
if ($name === 'intl' && FormatterTest::$enableIntl !== null) {
return FormatterTest::$enableIntl;
}
return \extension_loaded($name);
}
namespace yiiunit\framework\base;
use yii\base\Formatter;
......@@ -16,6 +30,8 @@ use DateInterval;
*/
class FormatterTest extends TestCase
{
public static $enableIntl;
/**
* @var Formatter
*/
......@@ -24,8 +40,23 @@ class FormatterTest extends TestCase
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->formatter = new Formatter();
// emulate disabled intl extension
// enable it only for tests prefixed with testIntl
static::$enableIntl = null;
if (strncmp($this->getName(false), 'testIntl', 8) === 0) {
if (!extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not installed.');
}
static::$enableIntl = true;
} else {
static::$enableIntl = false;
}
$this->mockApplication([
'timeZone' => 'UTC'
]);
$this->formatter = new Formatter(['locale' => 'en-US']);
}
protected function tearDown()
......@@ -34,6 +65,28 @@ class FormatterTest extends TestCase
$this->formatter = null;
}
public function testFormat()
{
$value = time();
$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'));
}
// 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');
//
// }
public function testAsRaw()
{
$value = '123';
......@@ -42,6 +95,8 @@ class FormatterTest extends TestCase
$this->assertSame($value, $this->formatter->asRaw($value));
$value = '<>';
$this->assertSame($value, $this->formatter->asRaw($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null));
}
......@@ -53,6 +108,8 @@ class FormatterTest extends TestCase
$this->assertSame("$value", $this->formatter->asText($value));
$value = '<>';
$this->assertSame('&lt;&gt;', $this->formatter->asText($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null));
}
......@@ -66,6 +123,8 @@ class FormatterTest extends TestCase
$this->assertSame('&lt;&gt;', $this->formatter->asNtext($value));
$value = "123\n456";
$this->assertSame("123<br />\n456", $this->formatter->asNtext($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null));
}
......@@ -83,6 +142,8 @@ class FormatterTest extends TestCase
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
$value = "123\n\n\n456";
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asParagraphs(null));
}
......@@ -95,13 +156,32 @@ class FormatterTest extends TestCase
{
$value = 'test@sample.com';
$this->assertSame("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
}
public function testAsUrl()
{
$value = 'http://www.yiiframework.com/';
$this->assertSame("<a href=\"$value\">$value</a>", $this->formatter->asUrl($value));
$value = 'https://www.yiiframework.com/';
$this->assertSame("<a href=\"$value\">$value</a>", $this->formatter->asUrl($value));
$value = 'www.yiiframework.com/';
$this->assertSame("<a href=\"http://$value\">$value</a>", $this->formatter->asUrl($value));
$value = 'https://www.yiiframework.com/?name=test&value=5"';
$this->assertSame("<a href=\"https://www.yiiframework.com/?name=test&amp;value=5&quot;\">https://www.yiiframework.com/?name=test&amp;value=5&quot;</a>", $this->formatter->asUrl($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(null));
}
public function testAsImage()
{
$value = 'http://sample.com/img.jpg';
$this->assertSame("<img src=\"$value\" alt=\"\">", $this->formatter->asImage($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
}
......@@ -115,15 +195,35 @@ class FormatterTest extends TestCase
$this->assertSame('Yes', $this->formatter->asBoolean($value));
$value = "";
$this->assertSame('No', $this->formatter->asBoolean($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
}
// date format
public function testIntlAsDate()
{
$this->testAsDate();
}
public function testIntlAsTime()
{
$this->testAsTime();
}
public function testIntlAsDatetime()
{
$this->testAsDatetime();
}
public function testAsDate()
{
$value = time();
// $this->assertSame(date('M j, Y', $value), $this->formatter->asDate($value));
// test fails for "en-US" because travis has another version of ICU = other format
$this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value, 'Y/m/d'));
$this->assertSame(date('M j, Y', $value), $this->formatter->asDate($value));
$this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value, 'php: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));
......@@ -133,7 +233,7 @@ class FormatterTest extends TestCase
{
$value = time();
$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(date('n:i:s A', $value), $this->formatter->asTime($value, 'php:n:i:s A'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
}
......@@ -141,115 +241,21 @@ class FormatterTest extends TestCase
{
$value = time();
$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(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
}
public function testAsInteger()
public function testAsTimestamp()
{
$value = 123;
$this->assertSame("$value", $this->formatter->asInteger($value));
$value = 123.23;
$this->assertSame("123", $this->formatter->asInteger($value));
$value = 'a';
$this->assertSame("0", $this->formatter->asInteger($value));
$value = -123.23;
$this->assertSame("-123", $this->formatter->asInteger($value));
$value = "-123abc";
$this->assertSame("-123", $this->formatter->asInteger($value));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asInteger(null));
}
public function testAsDouble()
{
$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 = 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("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));
}
public function testAsNumber()
{
$value = 123123.123;
$this->assertSame("123,123", $this->formatter->asNumber($value));
$this->assertSame("123,123.12", $this->formatter->asNumber($value, 2));
$this->formatter->decimalSeparator = ',';
$this->formatter->thousandSeparator = ' ';
$this->assertSame("123 123", $this->formatter->asNumber($value));
$this->assertSame("123 123,12", $this->formatter->asNumber($value, 2));
$this->formatter->thousandSeparator = '';
$this->assertSame("123123", $this->formatter->asNumber($value));
$this->assertSame("123123,12", $this->formatter->asNumber($value, 2));
$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));
}
$value = time();
$this->assertSame("$value", $this->formatter->asTimestamp($value));
$this->assertSame("$value", $this->formatter->asTimestamp((string) $value));
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));
}
$this->assertSame("$value", $this->formatter->asTimestamp(date('Y-m-d H:i:s', $value)));
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));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTimestamp(null));
}
public function testFormat()
{
$value = time();
$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'));
}
private function buildDateSubIntervals($referenceDate, $intervals)
{
......@@ -289,12 +295,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 -> 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'));
// 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');
......@@ -349,7 +355,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');
......@@ -373,14 +379,104 @@ 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');
}
// number format
// public function testAsInteger()
// {
// $value = 123;
// $this->assertSame("$value", $this->formatter->asInteger($value));
// $value = 123.23;
// $this->assertSame("123", $this->formatter->asInteger($value));
// $value = 'a';
// $this->assertSame("0", $this->formatter->asInteger($value));
// $value = -123.23;
// $this->assertSame("-123", $this->formatter->asInteger($value));
// $value = "-123abc";
// $this->assertSame("-123", $this->formatter->asInteger($value));
// $this->assertSame($this->formatter->nullDisplay, $this->formatter->asInteger(null));
// }
//
// public function testAsDouble()
// {
// $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 = 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("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));
// }
//
// public function testAsNumber()
// {
// $value = 123123.123;
// $this->assertSame("123,123", $this->formatter->asNumber($value));
// $this->assertSame("123,123.12", $this->formatter->asNumber($value, 2));
// $this->formatter->decimalSeparator = ',';
// $this->formatter->thousandSeparator = ' ';
// $this->assertSame("123 123", $this->formatter->asNumber($value));
// $this->assertSame("123 123,12", $this->formatter->asNumber($value, 2));
// $this->formatter->thousandSeparator = '';
// $this->assertSame("123123", $this->formatter->asNumber($value));
// $this->assertSame("123123,12", $this->formatter->asNumber($value, 2));
// $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));
// }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment