Refactored DateValidator to support ICU format

This commit is contained in:
Carsten Brandt
2014-09-16 22:05:12 +02:00
parent 9edf938bf1
commit 3ff548a442
10 changed files with 329 additions and 109 deletions

View File

@ -1,25 +1,11 @@
<?php
// override information about intl
namespace yii\helpers {
use yiiunit\framework\i18n\FormatterTest;
if (!function_exists('yii\helpers\extension_loaded')) {
function extension_loaded($name)
{
if ($name === 'intl' && FormatterTest::$enableIntl !== null) {
return FormatterTest::$enableIntl;
}
return \extension_loaded($name);
}
}
}
namespace yiiunit\framework\helpers {
namespace yiiunit\framework\helpers;
use Yii;
use yii\helpers\FormatConverter;
use yii\i18n\Formatter;
use yiiunit\framework\i18n\FormatterTest;
use yiiunit\framework\i18n\IntlTestHelper;
use yiiunit\TestCase;
/**
@ -31,17 +17,7 @@ class FormatConverterTest extends TestCase
{
parent::setUp();
// emulate disabled intl extension
// enable it only for tests prefixed with testIntl
FormatterTest::$enableIntl = null;
if (strncmp($this->getName(false), 'testIntl', 8) === 0) {
if (!extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not installed.');
}
FormatterTest::$enableIntl = true;
} else {
FormatterTest::$enableIntl = false;
}
IntlTestHelper::setIntlStatus($this);
$this->mockApplication([
'timeZone' => 'UTC',
@ -52,7 +28,7 @@ class FormatConverterTest extends TestCase
protected function tearDown()
{
parent::tearDown();
FormatterTest::$enableIntl = null;
IntlTestHelper::resetIntlStatus();
}
public function testIntlIcuToPhpShortForm()
@ -75,4 +51,3 @@ class FormatConverterTest extends TestCase
$this->assertEquals('24.8.2014', $formatter->asDate('2014-8-24', 'd.M.yyyy'));
}
}
}

View File

@ -1,35 +1,7 @@
<?php
// override information about intl
namespace yii\helpers {
use yiiunit\framework\i18n\FormatterTest;
if (!function_exists('yii\helpers\extension_loaded')) {
function extension_loaded($name)
{
if ($name === 'intl' && FormatterTest::$enableIntl !== null) {
return FormatterTest::$enableIntl;
}
return \extension_loaded($name);
}
}
}
namespace yiiunit\framework\i18n;
// override information about intl
namespace yii\i18n {
use yiiunit\framework\i18n\FormatterTest;
function extension_loaded($name)
{
if ($name === 'intl' && FormatterTest::$enableIntl !== null) {
return FormatterTest::$enableIntl;
}
return \extension_loaded($name);
}
}
namespace yiiunit\framework\i18n {
use yii\base\InvalidParamException;
use yii\i18n\Formatter;
use yiiunit\TestCase;
use DateTime;
@ -40,8 +12,6 @@ use DateInterval;
*/
class FormatterTest extends TestCase
{
public static $enableIntl;
/**
* @var Formatter
*/
@ -51,17 +21,7 @@ class FormatterTest extends TestCase
{
parent::setUp();
// 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;
}
IntlTestHelper::setIntlStatus($this);
$this->mockApplication([
'timeZone' => 'UTC',
@ -73,7 +33,7 @@ class FormatterTest extends TestCase
protected function tearDown()
{
parent::tearDown();
static::$enableIntl = null;
IntlTestHelper::resetIntlStatus();
$this->formatter = null;
}
@ -857,4 +817,3 @@ class FormatterTest extends TestCase
$this->assertSame("1023 bytes", $this->formatter->asSize(1023));
}
}
}

View File

@ -0,0 +1,77 @@
<?php
// override information about intl
namespace yiiunit\framework\i18n {
use yiiunit\TestCase;
class IntlTestHelper {
public static $enableIntl;
/**
* emulate disabled intl extension
*
* enable it only for tests prefixed with testIntl
* @param Testcase $test
*/
public static function setIntlStatus($test)
{
static::$enableIntl = null;
if (strncmp($test->getName(false), 'testIntl', 8) === 0) {
if (!extension_loaded('intl')) {
$test->markTestSkipped('intl extension is not installed.');
}
static::$enableIntl = true;
} else {
static::$enableIntl = false;
}
}
public static function resetIntlStatus()
{
static::$enableIntl = null;
}
}
}
namespace yii\i18n {
use yiiunit\framework\i18n\IntlTestHelper;
if (!function_exists('yii\i18n\extension_loaded')) {
function extension_loaded($name)
{
if ($name === 'intl' && IntlTestHelper::$enableIntl !== null) {
return IntlTestHelper::$enableIntl;
}
return \extension_loaded($name);
}
}
}
namespace yii\helpers {
use yiiunit\framework\i18n\IntlTestHelper;
if (!function_exists('yii\helpers\extension_loaded')) {
function extension_loaded($name)
{
if ($name === 'intl' && IntlTestHelper::$enableIntl !== null) {
return IntlTestHelper::$enableIntl;
}
return \extension_loaded($name);
}
}
}
namespace yii\validators {
use yiiunit\framework\i18n\IntlTestHelper;
if (!function_exists('yii\validators\extension_loaded')) {
function extension_loaded($name)
{
if ($name === 'intl' && IntlTestHelper::$enableIntl !== null) {
return IntlTestHelper::$enableIntl;
}
return \extension_loaded($name);
}
}
}

View File

@ -5,6 +5,7 @@ namespace yiiunit\framework\validators;
use DateTime;
use yii\validators\DateValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\framework\i18n\IntlTestHelper;
use yiiunit\TestCase;
/**
@ -15,7 +16,19 @@ class DateValidatorTest extends TestCase
protected function setUp()
{
parent::setUp();
$this->mockApplication();
IntlTestHelper::setIntlStatus($this);
$this->mockApplication([
'timeZone' => 'UTC',
'language' => 'ru-RU',
]);
}
protected function tearDown()
{
parent::tearDown();
IntlTestHelper::resetIntlStatus();
}
public function testEnsureMessageIsSet()
@ -24,26 +37,79 @@ class DateValidatorTest extends TestCase
$this->assertTrue($val->message !== null && strlen($val->message) > 1);
}
public function testIntlValidateValue()
{
$this->testValidateValue();
$this->mockApplication([
'language' => 'en-GB',
'components' => [
'formatter' => [
'dateFormat' => 'short',
]
]
]);
$val = new DateValidator();
$this->assertTrue($val->validate('31/5/2017'));
$this->assertFalse($val->validate('5/31/2017'));
$val = new DateValidator(['format' => 'short', 'locale' => 'en-GB']);
$this->assertTrue($val->validate('31/5/2017'));
$this->assertFalse($val->validate('5/31/2017'));
$this->mockApplication([
'language' => 'de-DE',
'components' => [
'formatter' => [
'dateFormat' => 'short',
]
]
]);
$val = new DateValidator();
$this->assertTrue($val->validate('31.5.2017'));
$this->assertFalse($val->validate('5.31.2017'));
$val = new DateValidator(['format' => 'short', 'locale' => 'de-DE']);
$this->assertTrue($val->validate('31.5.2017'));
$this->assertFalse($val->validate('5.31.2017'));
}
public function testValidateValue()
{
$val = new DateValidator;
// test PHP format
$val = new DateValidator(['format' => 'php:Y-m-d']);
$this->assertFalse($val->validate('3232-32-32'));
$this->assertTrue($val->validate('2013-09-13'));
$this->assertFalse($val->validate('31.7.2013'));
$this->assertFalse($val->validate('31-7-2013'));
$this->assertFalse($val->validate(time()));
$val->format = 'U';
$val->format = 'php:U';
$this->assertTrue($val->validate(time()));
$val->format = 'd.m.Y';
$val->format = 'php:d.m.Y';
$this->assertTrue($val->validate('31.7.2013'));
$val->format = 'Y-m-!d H:i:s';
$val->format = 'php:Y-m-!d H:i:s';
$this->assertTrue($val->validate('2009-02-15 15:16:17'));
// test ICU format
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
$this->assertFalse($val->validate('3232-32-32'));
$this->assertTrue($val->validate('2013-09-13'));
$this->assertFalse($val->validate('31.7.2013'));
$this->assertFalse($val->validate('31-7-2013'));
$this->assertFalse($val->validate(time()));
$val->format = 'dd.MM.yyyy';
$this->assertTrue($val->validate('31.7.2013'));
$val->format = 'yyyy-MM-dd HH:mm:ss';
$this->assertTrue($val->validate('2009-02-15 15:16:17'));
}
public function testValidateAttribute()
public function testIntlValidateAttributePHP()
{
$this->testValidateAttributePHPFormat();
}
public function testValidateAttributePHPFormat()
{
// error-array-add
$val = new DateValidator;
$val = new DateValidator(['format' => 'php:Y-m-d']);
$model = new FakedValidationModel;
$model->attr_date = '2013-09-13';
$val->validateAttribute($model, 'attr_date');
@ -53,7 +119,7 @@ class DateValidatorTest extends TestCase
$val->validateAttribute($model, 'attr_date');
$this->assertTrue($model->hasErrors('attr_date'));
//// timestamp attribute
$val = new DateValidator(['timestampAttribute' => 'attr_timestamp']);
$val = new DateValidator(['format' => 'php:Y-m-d', 'timestampAttribute' => 'attr_timestamp']);
$model = new FakedValidationModel;
$model->attr_date = '2013-09-13';
$model->attr_timestamp = true;
@ -61,10 +127,48 @@ class DateValidatorTest extends TestCase
$this->assertFalse($model->hasErrors('attr_date'));
$this->assertFalse($model->hasErrors('attr_timestamp'));
$this->assertEquals(
DateTime::createFromFormat($val->format, '2013-09-13')->getTimestamp(),
mktime(0, 0, 0, 9, 13, 2013), // 2013-09-13
// DateTime::createFromFormat('Y-m-d', '2013-09-13')->getTimestamp(),
$model->attr_timestamp
);
$val = new DateValidator();
$val = new DateValidator(['format' => 'php:Y-m-d']);
$model = FakedValidationModel::createWithAttributes(['attr_date' => []]);
$val->validateAttribute($model, 'attr_date');
$this->assertTrue($model->hasErrors('attr_date'));
}
public function testIntlValidateAttributeICU()
{
$this->testValidateAttributeICUFormat();
}
public function testValidateAttributeICUFormat()
{
// error-array-add
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
$model = new FakedValidationModel;
$model->attr_date = '2013-09-13';
$val->validateAttribute($model, 'attr_date');
$this->assertFalse($model->hasErrors('attr_date'));
$model = new FakedValidationModel;
$model->attr_date = '1375293913';
$val->validateAttribute($model, 'attr_date');
$this->assertTrue($model->hasErrors('attr_date'));
//// timestamp attribute
$val = new DateValidator(['format' => 'yyyy-MM-dd', 'timestampAttribute' => 'attr_timestamp']);
$model = new FakedValidationModel;
$model->attr_date = '2013-09-13';
$model->attr_timestamp = true;
$val->validateAttribute($model, 'attr_date');
$this->assertFalse($model->hasErrors('attr_date'));
$this->assertFalse($model->hasErrors('attr_timestamp'));
$this->assertEquals(
mktime(0, 0, 0, 9, 13, 2013), // 2013-09-13
// DateTime::createFromFormat('Y-m-d', '2013-09-13')->getTimestamp(),
$model->attr_timestamp
);
$val = new DateValidator(['format' => 'yyyy-MM-dd']);
$model = FakedValidationModel::createWithAttributes(['attr_date' => []]);
$val->validateAttribute($model, 'attr_date');
$this->assertTrue($model->hasErrors('attr_date'));