mockApplication([ 'timeZone' => 'UTC', 'language' => 'ru-RU', ]); $this->formatter = new Formatter(['locale' => 'en-US']); } protected function tearDown() { parent::tearDown(); IntlTestHelper::resetIntlStatus(); $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', 'php:Y/m/d'])); $this->setExpectedException('\yii\base\InvalidParamException'); $this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data')); } public function testLocale() { // locale is configured explicitly $f = new Formatter(['locale' => 'en-US']); $this->assertEquals('en-US', $f->locale); // if not, take from application $f = new Formatter(); $this->assertEquals('ru-RU', $f->locale); } 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)); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null)); } public function testAsText() { $value = '123'; $this->assertSame($value, $this->formatter->asText($value)); $value = 123; $this->assertSame("$value", $this->formatter->asText($value)); $value = '<>'; $this->assertSame('<>', $this->formatter->asText($value)); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null)); } public function testAsNtext() { $value = '123'; $this->assertSame($value, $this->formatter->asNtext($value)); $value = 123; $this->assertSame("$value", $this->formatter->asNtext($value)); $value = '<>'; $this->assertSame('<>', $this->formatter->asNtext($value)); $value = "123\n456"; $this->assertSame("123
\n456", $this->formatter->asNtext($value)); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null)); } public function testAsParagraphs() { $value = '123'; $this->assertSame("

$value

", $this->formatter->asParagraphs($value)); $value = 123; $this->assertSame("

$value

", $this->formatter->asParagraphs($value)); $value = '<>'; $this->assertSame('

<>

', $this->formatter->asParagraphs($value)); $value = "123\n456"; $this->assertSame("

123\n456

", $this->formatter->asParagraphs($value)); $value = "123\n\n456"; $this->assertSame("

123

\n

456

", $this->formatter->asParagraphs($value)); $value = "123\n\n\n456"; $this->assertSame("

123

\n

456

", $this->formatter->asParagraphs($value)); $value = "123\r\n456"; $this->assertSame("

123\r\n456

", $this->formatter->asParagraphs($value)); $value = "123\r\n\r\n456"; $this->assertSame("

123

\n

456

", $this->formatter->asParagraphs($value)); $value = "123\r\n\r\n\r\n456"; $this->assertSame("

123

\n

456

", $this->formatter->asParagraphs($value)); $value = "123\r456"; $this->assertSame("

123\r456

", $this->formatter->asParagraphs($value)); $value = "123\r\r456"; $this->assertSame("

123

\n

456

", $this->formatter->asParagraphs($value)); $value = "123\r\r\r456"; $this->assertSame("

123

\n

456

", $this->formatter->asParagraphs($value)); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asParagraphs(null)); } public function testAsHtml() { // todo: dependency on HtmlPurifier } public function testAsEmail() { $value = 'test@sample.com'; $this->assertSame("$value", $this->formatter->asEmail($value)); $value = 'test@sample.com'; $this->assertSame("$value", $this->formatter->asEmail($value, ['target' => '_blank'])); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null)); } public function testAsUrl() { $value = 'http://www.yiiframework.com/'; $this->assertSame("$value", $this->formatter->asUrl($value)); $value = 'https://www.yiiframework.com/'; $this->assertSame("$value", $this->formatter->asUrl($value)); $value = 'www.yiiframework.com/'; $this->assertSame("$value", $this->formatter->asUrl($value)); $value = 'https://www.yiiframework.com/?name=test&value=5"'; $this->assertSame("https://www.yiiframework.com/?name=test&value=5"", $this->formatter->asUrl($value)); $value = 'http://www.yiiframework.com/'; $this->assertSame("$value", $this->formatter->asUrl($value, ['target' => '_blank'])); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(null)); } public function testAsImage() { $value = 'http://sample.com/img.jpg'; $this->assertSame("\"\"", $this->formatter->asImage($value)); $value = 'http://sample.com/img.jpg'; $alt = "Hello!"; $this->assertSame("\"$alt\"", $this->formatter->asImage($value, ['alt' => $alt])); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null)); } public function testAsBoolean() { $this->assertSame('Yes', $this->formatter->asBoolean(true)); $this->assertSame('No', $this->formatter->asBoolean(false)); $this->assertSame('Yes', $this->formatter->asBoolean("111")); $this->assertSame('No', $this->formatter->asBoolean("")); $this->assertSame('No', $this->formatter->asBoolean(0)); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null)); } }