You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
7.1 KiB
195 lines
7.1 KiB
12 years ago
|
<?php
|
||
10 years ago
|
|
||
10 years ago
|
namespace yiiunit\framework\i18n;
|
||
10 years ago
|
|
||
10 years ago
|
use yii\i18n\Formatter;
|
||
10 years ago
|
use Yii;
|
||
12 years ago
|
use yiiunit\TestCase;
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Test for basic formatter functions
|
||
|
*
|
||
|
* See FormatterDateTest and FormatterNumberTest for date/number formatting.
|
||
|
*
|
||
10 years ago
|
* @group i18n
|
||
12 years ago
|
*/
|
||
|
class FormatterTest extends TestCase
|
||
|
{
|
||
11 years ago
|
/**
|
||
|
* @var Formatter
|
||
|
*/
|
||
|
protected $formatter;
|
||
|
|
||
|
protected function setUp()
|
||
|
{
|
||
|
parent::setUp();
|
||
10 years ago
|
|
||
10 years ago
|
IntlTestHelper::setIntlStatus($this);
|
||
10 years ago
|
|
||
|
$this->mockApplication([
|
||
10 years ago
|
'timeZone' => 'UTC',
|
||
|
'language' => 'ru-RU',
|
||
10 years ago
|
]);
|
||
|
$this->formatter = new Formatter(['locale' => 'en-US']);
|
||
11 years ago
|
}
|
||
|
|
||
|
protected function tearDown()
|
||
|
{
|
||
|
parent::tearDown();
|
||
10 years ago
|
IntlTestHelper::resetIntlStatus();
|
||
11 years ago
|
$this->formatter = null;
|
||
|
}
|
||
|
|
||
10 years ago
|
|
||
|
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'));
|
||
10 years ago
|
$this->assertSame(date('Y/m/d', $value), $this->formatter->format($value, ['date', 'php:Y/m/d']));
|
||
10 years ago
|
$this->setExpectedException('\yii\base\InvalidParamException');
|
||
|
$this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data'));
|
||
|
}
|
||
|
|
||
10 years ago
|
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);
|
||
|
}
|
||
|
|
||
10 years ago
|
|
||
11 years ago
|
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));
|
||
10 years ago
|
|
||
|
// null display
|
||
11 years ago
|
$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));
|
||
10 years ago
|
|
||
|
// null display
|
||
11 years ago
|
$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<br />\n456", $this->formatter->asNtext($value));
|
||
10 years ago
|
|
||
|
// null display
|
||
11 years ago
|
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null));
|
||
|
}
|
||
|
|
||
|
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><></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));
|
||
10 years ago
|
$value = "123\r\n456";
|
||
|
$this->assertSame("<p>123\r\n456</p>", $this->formatter->asParagraphs($value));
|
||
|
$value = "123\r\n\r\n456";
|
||
|
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
|
||
|
$value = "123\r\n\r\n\r\n456";
|
||
|
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
|
||
|
$value = "123\r456";
|
||
|
$this->assertSame("<p>123\r456</p>", $this->formatter->asParagraphs($value));
|
||
|
$value = "123\r\r456";
|
||
|
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
|
||
|
$value = "123\r\r\r456";
|
||
|
$this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
|
||
10 years ago
|
|
||
|
// null display
|
||
11 years ago
|
$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("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value));
|
||
10 years ago
|
$value = 'test@sample.com';
|
||
|
$this->assertSame("<a href=\"mailto:$value\" target=\"_blank\">$value</a>", $this->formatter->asEmail($value, ['target' => '_blank']));
|
||
10 years ago
|
|
||
|
// null display
|
||
11 years ago
|
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
|
||
|
}
|
||
|
|
||
10 years ago
|
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));
|
||
10 years ago
|
$value = 'http://www.yiiframework.com/';
|
||
|
$this->assertSame("<a href=\"$value\" target=\"_blank\">$value</a>", $this->formatter->asUrl($value, ['target' => '_blank']));
|
||
10 years ago
|
|
||
|
// null display
|
||
|
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(null));
|
||
|
}
|
||
|
|
||
11 years ago
|
public function testAsImage()
|
||
|
{
|
||
|
$value = 'http://sample.com/img.jpg';
|
||
|
$this->assertSame("<img src=\"$value\" alt=\"\">", $this->formatter->asImage($value));
|
||
10 years ago
|
$value = 'http://sample.com/img.jpg';
|
||
|
$alt = "Hello!";
|
||
|
$this->assertSame("<img src=\"$value\" alt=\"$alt\">", $this->formatter->asImage($value, ['alt' => $alt]));
|
||
10 years ago
|
|
||
|
// null display
|
||
11 years ago
|
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
|
||
|
}
|
||
|
|
||
|
public function testAsBoolean()
|
||
|
{
|
||
10 years ago
|
$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));
|
||
10 years ago
|
|
||
|
// null display
|
||
11 years ago
|
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
|
||
|
}
|
||
12 years ago
|
}
|