FormatterTest.php 2.6 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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
16
 * @group i18n
Qiang Xue committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 */
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();
		$this->formatter = new Formatter(array(
			'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));
51
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
Qiang Xue committed
52 53 54 55 56 57 58 59 60 61
	}

	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));
62
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asPercent(null));
Qiang Xue committed
63 64 65 66 67 68 69 70 71 72
	}

	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));
73
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
Qiang Xue committed
74 75 76 77 78 79 80 81 82 83
	}

	public function testAsCurrency()
	{
		$value = '123';
		$this->assertSame('$123.00', $this->formatter->asCurrency($value));
		$value = '123.456';
		$this->assertSame("$123.46", $this->formatter->asCurrency($value));
		$value = '-123456.123';
		$this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
84
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
Qiang Xue committed
85 86 87 88 89 90
	}

	public function testDate()
	{
		$time = time();
		$this->assertSame(date('n/j/y', $time), $this->formatter->asDate($time));
Qiang Xue committed
91
		$this->assertSame(date('F j, Y', $time), $this->formatter->asDate($time, 'long'));
92
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
Qiang Xue committed
93 94
	}
}