@ -4,6 +4,20 @@
* @copyright Copyright (c) 2008 Yii Software LLC
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @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;
namespace yiiunit\framework\base;
use yii\base\Formatter;
use yii\base\Formatter;
@ -16,6 +30,8 @@ use DateInterval;
*/
*/
class FormatterTest extends TestCase
class FormatterTest extends TestCase
{
{
public static $enableIntl;
/**
/**
* @var Formatter
* @var Formatter
*/
*/
@ -24,8 +40,23 @@ class FormatterTest extends TestCase
protected function setUp()
protected function setUp()
{
{
parent::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()
protected function tearDown()
@ -34,6 +65,28 @@ class FormatterTest extends TestCase
$this->formatter = null;
$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()
public function testAsRaw()
{
{
$value = '123';
$value = '123';
@ -42,6 +95,8 @@ class FormatterTest extends TestCase
$this->assertSame($value, $this->formatter->asRaw($value));
$this->assertSame($value, $this->formatter->asRaw($value));
$value = '< >';
$value = '< >';
$this->assertSame($value, $this->formatter->asRaw($value));
$this->assertSame($value, $this->formatter->asRaw($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null));
}
}
@ -53,6 +108,8 @@ class FormatterTest extends TestCase
$this->assertSame("$value", $this->formatter->asText($value));
$this->assertSame("$value", $this->formatter->asText($value));
$value = '< >';
$value = '< >';
$this->assertSame('< > ', $this->formatter->asText($value));
$this->assertSame('< > ', $this->formatter->asText($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null));
}
}
@ -66,6 +123,8 @@ class FormatterTest extends TestCase
$this->assertSame('< > ', $this->formatter->asNtext($value));
$this->assertSame('< > ', $this->formatter->asNtext($value));
$value = "123\n456";
$value = "123\n456";
$this->assertSame("123< br / > \n456", $this->formatter->asNtext($value));
$this->assertSame("123< br / > \n456", $this->formatter->asNtext($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null));
$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));
$this->assertSame("< p > 123< / p > \n< p > 456< / p > ", $this->formatter->asParagraphs($value));
$value = "123\n\n\n456";
$value = "123\n\n\n456";
$this->assertSame("< p > 123< / p > \n< p > 456< / p > ", $this->formatter->asParagraphs($value));
$this->assertSame("< p > 123< / p > \n< p > 456< / p > ", $this->formatter->asParagraphs($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asParagraphs(null));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asParagraphs(null));
}
}
@ -95,13 +156,32 @@ class FormatterTest extends TestCase
{
{
$value = 'test@sample.com';
$value = 'test@sample.com';
$this->assertSame("< a href = \"mailto:$value\" > $value< / a > ", $this->formatter->asEmail($value));
$this->assertSame("< a href = \"mailto:$value\" > $value< / a > ", $this->formatter->asEmail($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
$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&value=5"\" > https://www.yiiframework.com/?name=test& value=5" < / a > ", $this->formatter->asUrl($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(null));
}
public function testAsImage()
public function testAsImage()
{
{
$value = 'http://sample.com/img.jpg';
$value = 'http://sample.com/img.jpg';
$this->assertSame("< img src = \"$value\" alt = \"\" > ", $this->formatter->asImage($value));
$this->assertSame("< img src = \"$value\" alt = \"\" > ", $this->formatter->asImage($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
}
}
@ -115,15 +195,35 @@ class FormatterTest extends TestCase
$this->assertSame('Yes', $this->formatter->asBoolean($value));
$this->assertSame('Yes', $this->formatter->asBoolean($value));
$value = "";
$value = "";
$this->assertSame('No', $this->formatter->asBoolean($value));
$this->assertSame('No', $this->formatter->asBoolean($value));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
$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()
public function testAsDate()
{
{
$value = time();
$value = time();
// $this->assertSame(date('M j, Y', $value), $this->formatter->asDate($value));
$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, 'php:Y/m/d'));
$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('n/j/y', $value), $this->formatter->asDate($value, 'short'));
$this->assertSame(date('F j, Y', $value), $this->formatter->asDate($value, 'long'));
$this->assertSame(date('F j, Y', $value), $this->formatter->asDate($value, 'long'));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
@ -133,7 +233,7 @@ class FormatterTest extends TestCase
{
{
$value = time();
$value = time();
$this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value));
$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));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
}
}
@ -141,115 +241,21 @@ class FormatterTest extends TestCase
{
{
$value = time();
$value = time();
$this->assertSame(date('M j, Y g:i:s A', $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(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));
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
}
}
public function testAsInteger ()
public function testAsTimestamp ()
{
{
$value = 123;
$value = time();
$this->assertSame("$value", $this->formatter->asInteger($value));
$this->assertSame("$value", $this->formatter->asTimestamp($value));
$value = 123.23;
$this->assertSame("$value", $this->formatter->asTimestamp((string) $value));
$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()
$this->assertSame("$value", $this->formatter->asTimestamp(date('Y-m-d H:i:s', $value)));
{
$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()
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTimestamp(null));
{
$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('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)
private function buildDateSubIntervals($referenceDate, $intervals)
{
{
@ -374,13 +380,103 @@ class FormatterTest extends TestCase
$this->assertSame('in 28 days', $this->formatter->asRelativeTime($dateThen, $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));
// }
}
}