FormatterTest.php 6.83 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
namespace yiiunit\framework\base;

use yii\base\Formatter;
use yiiunit\TestCase;

/**
13
 * @group base
Qiang Xue committed
14 15 16 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
 */
class FormatterTest extends TestCase
{
	/**
	 * @var Formatter
	 */
	protected $formatter;

	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
		$this->formatter = new Formatter();
	}

	protected function tearDown()
	{
		parent::tearDown();
		$this->formatter = null;
	}

	public function testAsRaw()
	{
		$value = '123';
		$this->assertSame($value, $this->formatter->asRaw($value));
		$value = 123;
		$this->assertSame($value, $this->formatter->asRaw($value));
		$value = '<>';
		$this->assertSame($value, $this->formatter->asRaw($value));
43
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null));
Qiang Xue committed
44 45 46 47 48 49 50 51 52 53
	}

	public function testAsText()
	{
		$value = '123';
		$this->assertSame($value, $this->formatter->asText($value));
		$value = 123;
		$this->assertSame("$value", $this->formatter->asText($value));
		$value = '<>';
		$this->assertSame('&lt;&gt;', $this->formatter->asText($value));
54
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null));
Qiang Xue committed
55 56 57 58 59 60 61 62 63 64 65 66
	}

	public function testAsNtext()
	{
		$value = '123';
		$this->assertSame($value, $this->formatter->asNtext($value));
		$value = 123;
		$this->assertSame("$value", $this->formatter->asNtext($value));
		$value = '<>';
		$this->assertSame('&lt;&gt;', $this->formatter->asNtext($value));
		$value = "123\n456";
		$this->assertSame("123<br />\n456", $this->formatter->asNtext($value));
67
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null));
Qiang Xue committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	}

	public function testAsParagraphs()
	{
		$value = '123';
		$this->assertSame("<p>$value</p>", $this->formatter->asParagraphs($value));
		$value = 123;
		$this->assertSame("<p>$value</p>", $this->formatter->asParagraphs($value));
		$value = '<>';
		$this->assertSame('<p>&lt;&gt;</p>', $this->formatter->asParagraphs($value));
		$value = "123\n456";
		$this->assertSame("<p>123\n456</p>", $this->formatter->asParagraphs($value));
		$value = "123\n\n456";
		$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));
84
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asParagraphs(null));
Qiang Xue committed
85 86 87 88
	}

	public function testAsHtml()
	{
Qiang Xue committed
89
		// todo: dependency on HtmlPurifier
Qiang Xue committed
90 91 92 93 94 95
	}

	public function testAsEmail()
	{
		$value = 'test@sample.com';
		$this->assertSame("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value));
96
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
Qiang Xue committed
97 98 99 100 101
	}

	public function testAsImage()
	{
		$value = 'http://sample.com/img.jpg';
102
		$this->assertSame("<img src=\"$value\" alt=\"\">", $this->formatter->asImage($value));
103
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
Qiang Xue committed
104
	}
Qiang Xue committed
105 106 107 108 109 110 111 112 113 114 115

	public function testAsBoolean()
	{
		$value = true;
		$this->assertSame('Yes', $this->formatter->asBoolean($value));
		$value = false;
		$this->assertSame('No', $this->formatter->asBoolean($value));
		$value = "111";
		$this->assertSame('Yes', $this->formatter->asBoolean($value));
		$value = "";
		$this->assertSame('No', $this->formatter->asBoolean($value));
116
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
Qiang Xue committed
117 118 119 120 121 122 123
	}

	public function testAsDate()
	{
		$value = time();
		$this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value));
		$this->assertSame(date('Y-m-d', $value), $this->formatter->asDate($value, 'Y-m-d'));
124
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
Qiang Xue committed
125 126 127 128 129 130 131
	}

	public function testAsTime()
	{
		$value = time();
		$this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value));
		$this->assertSame(date('h:i:s', $value), $this->formatter->asTime($value, 'h:i:s'));
132
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
Qiang Xue committed
133 134 135 136 137 138 139
	}

	public function testAsDatetime()
	{
		$value = time();
		$this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value));
		$this->assertSame(date('Y-m-d h:i:s', $value), $this->formatter->asDatetime($value, 'Y-m-d h:i:s'));
140
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
Qiang Xue committed
141 142 143 144 145 146 147 148 149 150
	}

	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));
151 152 153 154
		$value = -123.23;
		$this->assertSame("-123", $this->formatter->asInteger($value));
		$value = "-123abc";
		$this->assertSame("-123", $this->formatter->asInteger($value));
155
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asInteger(null));
Qiang Xue committed
156 157 158 159 160 161 162
	}

	public function testAsDouble()
	{
		$value = 123.12;
		$this->assertSame("123.12", $this->formatter->asDouble($value));
		$this->assertSame("123.1", $this->formatter->asDouble($value, 1));
163 164 165 166 167 168 169 170 171 172
		$this->assertSame("123", $this->formatter->asDouble($value, 0));
		$value = 123;
		$this->assertSame("123.00", $this->formatter->asDouble($value));
		$this->formatter->decimalSeparator = ',';
		$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));
173
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asDouble(null));
Qiang Xue committed
174 175 176 177 178 179
	}

	public function testAsNumber()
	{
		$value = 123123.123;
		$this->assertSame("123,123", $this->formatter->asNumber($value));
180 181 182 183 184 185 186 187
		$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));
188
		$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNumber(null));
Qiang Xue committed
189
	}
190 191 192 193 194 195

	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'));
Alexander Makarov committed
196
		$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, ['date', 'Y-m-d']));
197 198 199
		$this->setExpectedException('\yii\base\InvalidParamException');
		$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data'));
	}
Qiang Xue committed
200
}