Yii2 framework backup
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.

301 lines
12 KiB

<?php
Added php-cs-fixer coding standards validation to Travis CI (#14100) * php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
7 years ago
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\validators;
use yii\validators\NumberValidator;
use yii\web\View;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class NumberValidatorTest extends TestCase
{
private $commaDecimalLocales = ['fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252'];
private $pointDecimalLocales = ['en_US.UTF-8', 'en_US.UTF8', 'en_US.utf-8', 'en_US.utf8', 'English_United States.1252'];
private $oldLocale;
private function setCommaDecimalLocale()
{
if ($this->oldLocale === false) {
$this->markTestSkipped('Your platform does not support locales.');
}
if (setlocale(LC_NUMERIC, $this->commaDecimalLocales) === false) {
$this->markTestSkipped('Could not set any of required locales: ' . implode(', ', $this->commaDecimalLocales));
}
}
private function setPointDecimalLocale()
{
if ($this->oldLocale === false) {
$this->markTestSkipped('Your platform does not support locales.');
}
if (setlocale(LC_NUMERIC, $this->pointDecimalLocales) === false) {
$this->markTestSkipped('Could not set any of required locales: ' . implode(', ', $this->pointDecimalLocales));
}
}
private function restoreLocale()
{
setlocale(LC_NUMERIC, $this->oldLocale);
}
protected function setUp()
{
parent::setUp();
$this->oldLocale = setlocale(LC_NUMERIC, 0);
// destroy application, Validator must work without Yii::$app
$this->destroyApplication();
}
public function testEnsureMessageOnInit()
{
Added php-cs-fixer coding standards validation to Travis CI (#14100) * php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
7 years ago
$val = new NumberValidator();
$this->assertInternalType('string', $val->message);
$this->assertTrue($val->max === null);
$val = new NumberValidator(['min' => -1, 'max' => 20, 'integerOnly' => true]);
Added php-cs-fixer coding standards validation to Travis CI (#14100) * php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
7 years ago
$this->assertInternalType('string', $val->message);
$this->assertInternalType('string', $val->tooSmall);
$this->assertInternalType('string', $val->tooBig);
}
public function testValidateValueSimple()
{
$val = new NumberValidator();
$this->assertTrue($val->validate(20));
$this->assertTrue($val->validate(0));
$this->assertTrue($val->validate(-20));
$this->assertTrue($val->validate('20'));
$this->assertTrue($val->validate(25.45));
$this->setPointDecimalLocale();
$this->assertFalse($val->validate('25,45'));
$this->setCommaDecimalLocale();
$this->assertTrue($val->validate('25,45'));
$this->restoreLocale();
$this->assertFalse($val->validate('12:45'));
$val = new NumberValidator(['integerOnly' => true]);
$this->assertTrue($val->validate(20));
$this->assertTrue($val->validate(0));
$this->assertFalse($val->validate(25.45));
$this->assertTrue($val->validate('20'));
$this->assertFalse($val->validate('25,45'));
$this->assertTrue($val->validate('020'));
$this->assertTrue($val->validate(0x14));
$this->assertFalse($val->validate('0x14')); // todo check this
}
public function testValidateValueAdvanced()
{
$val = new NumberValidator();
$this->assertTrue($val->validate('-1.23')); // signed float
$this->assertTrue($val->validate('-4.423e-12')); // signed float + exponent
$this->assertTrue($val->validate('12E3')); // integer + exponent
$this->assertFalse($val->validate('e12')); // just exponent
$this->assertFalse($val->validate('-e3'));
$this->assertFalse($val->validate('-4.534-e-12')); // 'signed' exponent
$this->assertFalse($val->validate('12.23^4')); // expression instead of value
$val = new NumberValidator(['integerOnly' => true]);
$this->assertFalse($val->validate('-1.23'));
$this->assertFalse($val->validate('-4.423e-12'));
$this->assertFalse($val->validate('12E3'));
$this->assertFalse($val->validate('e12'));
$this->assertFalse($val->validate('-e3'));
$this->assertFalse($val->validate('-4.534-e-12'));
$this->assertFalse($val->validate('12.23^4'));
}
public function testValidateValueWithLocaleWhereDecimalPointIsComma()
{
$val = new NumberValidator();
$this->setPointDecimalLocale();
$this->assertTrue($val->validate(.5));
$this->setCommaDecimalLocale();
$this->assertTrue($val->validate(.5));
$this->restoreLocale();
}
public function testValidateValueMin()
{
$val = new NumberValidator(['min' => 1]);
$this->assertTrue($val->validate(1));
$this->assertFalse($val->validate(-1));
$this->assertFalse($val->validate('22e-12'));
$this->assertTrue($val->validate(PHP_INT_MAX + 1));
$val = new NumberValidator(['min' => 1], ['integerOnly' => true]);
$this->assertTrue($val->validate(1));
$this->assertFalse($val->validate(-1));
$this->assertFalse($val->validate('22e-12'));
$this->assertTrue($val->validate(PHP_INT_MAX + 1));
}
public function testValidateValueMax()
{
$val = new NumberValidator(['max' => 1.25]);
$this->assertTrue($val->validate(1));
$this->assertFalse($val->validate(1.5));
$this->assertTrue($val->validate('22e-12'));
$this->assertTrue($val->validate('125e-2'));
$val = new NumberValidator(['max' => 1.25, 'integerOnly' => true]);
$this->assertTrue($val->validate(1));
$this->assertFalse($val->validate(1.5));
$this->assertFalse($val->validate('22e-12'));
$this->assertFalse($val->validate('125e-2'));
}
public function testValidateValueRange()
{
$val = new NumberValidator(['min' => -10, 'max' => 20]);
$this->assertTrue($val->validate(0));
$this->assertTrue($val->validate(-10));
$this->assertFalse($val->validate(-11));
$this->assertFalse($val->validate(21));
$val = new NumberValidator(['min' => -10, 'max' => 20, 'integerOnly' => true]);
$this->assertTrue($val->validate(0));
$this->assertFalse($val->validate(-11));
$this->assertFalse($val->validate(22));
$this->assertFalse($val->validate('20e-1'));
}
public function testValidateAttribute()
{
$val = new NumberValidator();
$model = new FakedValidationModel();
$model->attr_number = '5.5e1';
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));
$model->attr_number = '43^32'; //expression
$val->validateAttribute($model, 'attr_number');
$this->assertTrue($model->hasErrors('attr_number'));
$val = new NumberValidator(['min' => 10]);
$model = new FakedValidationModel();
$model->attr_number = 10;
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));
$model->attr_number = 5;
$val->validateAttribute($model, 'attr_number');
$this->assertTrue($model->hasErrors('attr_number'));
$val = new NumberValidator(['max' => 10]);
$model = new FakedValidationModel();
$model->attr_number = 10;
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));
$model->attr_number = 15;
$val->validateAttribute($model, 'attr_number');
$this->assertTrue($model->hasErrors('attr_number'));
$val = new NumberValidator(['max' => 10, 'integerOnly' => true]);
$model = new FakedValidationModel();
$model->attr_number = 10;
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));
$model->attr_number = 3.43;
$val->validateAttribute($model, 'attr_number');
$this->assertTrue($model->hasErrors('attr_number'));
$val = new NumberValidator(['min' => 1]);
$model = FakedValidationModel::createWithAttributes(['attr_num' => [1, 2, 3]]);
$val->validateAttribute($model, 'attr_num');
$this->assertTrue($model->hasErrors('attr_num'));
// @see https://github.com/yiisoft/yii2/issues/11672
$model = new FakedValidationModel();
$model->attr_number = new \stdClass();
$val->validateAttribute($model, 'attr_number');
$this->assertTrue($model->hasErrors('attr_number'));
}
public function testValidateAttributeWithLocaleWhereDecimalPointIsComma()
{
$val = new NumberValidator();
$model = new FakedValidationModel();
$model->attr_number = 0.5;
$this->setPointDecimalLocale();
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));
$this->setCommaDecimalLocale();
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));
$this->restoreLocale();
}
public function testEnsureCustomMessageIsSetOnValidateAttribute()
{
$val = new NumberValidator([
'tooSmall' => '{attribute} is to small.',
Added php-cs-fixer coding standards validation to Travis CI (#14100) * php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
7 years ago
'min' => 5,
]);
$model = new FakedValidationModel();
$model->attr_number = 0;
$val->validateAttribute($model, 'attr_number');
$this->assertTrue($model->hasErrors('attr_number'));
$this->assertCount(1, $model->getErrors('attr_number'));
$msgs = $model->getErrors('attr_number');
$this->assertSame('attr_number is to small.', $msgs[0]);
}
/**
* @see https://github.com/yiisoft/yii2/issues/3118
*/
public function testClientValidateComparison()
{
$val = new NumberValidator([
'min' => 5,
'max' => 10,
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5', $js);
$this->assertContains('"max":10', $js);
$val = new NumberValidator([
'min' => '5',
'max' => '10',
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5', $js);
$this->assertContains('"max":10', $js);
$val = new NumberValidator([
'min' => 5.65,
'max' => 13.37,
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5.65', $js);
$this->assertContains('"max":13.37', $js);
$val = new NumberValidator([
'min' => '5.65',
'max' => '13.37',
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5.65', $js);
$this->assertContains('"max":13.37', $js);
}
public function testValidateObject()
{
$val = new NumberValidator();
$value = new \stdClass();
$this->assertFalse($val->validate($value));
}
}