From cde97847f425840948a4bfbf267693063c14e103 Mon Sep 17 00:00:00 2001 From: Suralc Date: Mon, 29 Jul 2013 15:04:44 +0200 Subject: [PATCH 1/6] BooleanValidatorTest --- .../framework/validators/BooleanValidatorTest.php | 53 ++++++++++++++++++++++ .../validators/DefaultValueValidatorTest.php | 32 +++++++++++++ .../framework/validators/FakedValidationModel.php | 20 ++++++++ 3 files changed, 105 insertions(+) create mode 100644 tests/unit/framework/validators/BooleanValidatorTest.php create mode 100644 tests/unit/framework/validators/DefaultValueValidatorTest.php create mode 100644 tests/unit/framework/validators/FakedValidationModel.php diff --git a/tests/unit/framework/validators/BooleanValidatorTest.php b/tests/unit/framework/validators/BooleanValidatorTest.php new file mode 100644 index 0000000..e13635d --- /dev/null +++ b/tests/unit/framework/validators/BooleanValidatorTest.php @@ -0,0 +1,53 @@ +assertTrue($val->validateValue(true)); + $this->assertTrue($val->validateValue(false)); + $this->assertTrue($val->validateValue('0')); + $this->assertTrue($val->validateValue('1')); + $this->assertTrue($val->validateValue(null)); + $this->assertTrue($val->validateValue(array())); + $val->strict = true; + $this->assertTrue($val->validateValue('0')); + $this->assertTrue($val->validateValue('1')); + $this->assertFalse($val->validateValue(true)); + $this->assertFalse($val->validateValue(false)); + $val->trueValue = true; + $val->falseValue = false; + $this->assertFalse($val->validateValue('0')); + $this->assertFalse($val->validateValue(array())); + $this->assertTrue($val->validateValue(true)); + $this->assertTrue($val->validateValue(false)); + } + + public function testValidateAttributeAndError() + { + $obj = new FakedValidationModel; + $obj->attrA = true; + $obj->attrB = '1'; + $obj->attrC = '0'; + $obj->attrD = array(); + $val = new BooleanValidator; + $val->validateAttribute($obj, 'attrA'); + $this->assertFalse(isset($obj->errors['attrA'])); + $val->validateAttribute($obj, 'attrC'); + $this->assertFalse(isset($obj->errors['attrC'])); + $val->strict = true; + $val->validateAttribute($obj, 'attrB'); + $this->assertFalse(isset($obj->errors['attrB'])); + $val->validateAttribute($obj, 'attrD'); + $this->assertTrue(isset($obj->errors['attrD'])); + } +} diff --git a/tests/unit/framework/validators/DefaultValueValidatorTest.php b/tests/unit/framework/validators/DefaultValueValidatorTest.php new file mode 100644 index 0000000..05a71b5 --- /dev/null +++ b/tests/unit/framework/validators/DefaultValueValidatorTest.php @@ -0,0 +1,32 @@ +value = 'test_value'; + $obj = new \stdclass; + $obj->attrA = 'attrA'; + $obj->attrB = null; + $obj->attrC = ''; + // original values to chek which attritubes where modified + $objB = clone $obj; + $val->validateAttribute($obj, 'attrB'); + $this->assertEquals($val->value, $obj->attrB); + $this->assertEquals($objB->attrA, $obj->attrA); + $val->value = 'new_test_value'; + $obj = clone $objB; // get clean object + $val->validateAttribute($obj, 'attrC'); + $this->assertEquals('new_test_value', $obj->attrC); + $this->assertEquals($objB->attrA, $obj->attrA); + $val->validateAttribute($obj, 'attrA'); + $this->assertEquals($objB->attrA, $obj->attrA); + } +} diff --git a/tests/unit/framework/validators/FakedValidationModel.php b/tests/unit/framework/validators/FakedValidationModel.php new file mode 100644 index 0000000..d3c0424 --- /dev/null +++ b/tests/unit/framework/validators/FakedValidationModel.php @@ -0,0 +1,20 @@ +errors[$attribute] = $message; + } +} \ No newline at end of file From 09c5e6146273310ab3f885c595cbabd8f27c202a Mon Sep 17 00:00:00 2001 From: Suralc Date: Mon, 29 Jul 2013 15:20:07 +0200 Subject: [PATCH 2/6] fix test --- tests/unit/framework/validators/BooleanValidatorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/framework/validators/BooleanValidatorTest.php b/tests/unit/framework/validators/BooleanValidatorTest.php index e13635d..655ef58 100644 --- a/tests/unit/framework/validators/BooleanValidatorTest.php +++ b/tests/unit/framework/validators/BooleanValidatorTest.php @@ -17,8 +17,8 @@ class BooleanValidatorTest extends TestCase $this->assertTrue($val->validateValue(false)); $this->assertTrue($val->validateValue('0')); $this->assertTrue($val->validateValue('1')); - $this->assertTrue($val->validateValue(null)); - $this->assertTrue($val->validateValue(array())); + $this->assertFalse($val->validateValue(null)); + $this->assertFalse($val->validateValue(array())); $val->strict = true; $this->assertTrue($val->validateValue('0')); $this->assertTrue($val->validateValue('1')); From fc1d2af36c28ff853faa59fc799bbf05f1ab9410 Mon Sep 17 00:00:00 2001 From: Suralc Date: Mon, 29 Jul 2013 16:21:07 +0200 Subject: [PATCH 3/6] UrlValidator Tests --- framework/yii/validators/UrlValidator.php | 2 + .../framework/validators/FakedValidationModel.php | 5 ++ .../unit/framework/validators/UrlValidatorTest.php | 89 ++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 tests/unit/framework/validators/UrlValidatorTest.php diff --git a/framework/yii/validators/UrlValidator.php b/framework/yii/validators/UrlValidator.php index aa3dad1..c9b3a2b 100644 --- a/framework/yii/validators/UrlValidator.php +++ b/framework/yii/validators/UrlValidator.php @@ -54,7 +54,9 @@ class UrlValidator extends Validator { parent::init(); if ($this->enableIDN && !function_exists('idn_to_ascii')) { + // @codeCoverageIgnoreStart throw new InvalidConfigException('In order to use IDN validation intl extension must be installed and enabled.'); + // @codeCoverageIgnoreEnd } if ($this->message === null) { $this->message = Yii::t('yii', '{attribute} is not a valid URL.'); diff --git a/tests/unit/framework/validators/FakedValidationModel.php b/tests/unit/framework/validators/FakedValidationModel.php index d3c0424..89632c7 100644 --- a/tests/unit/framework/validators/FakedValidationModel.php +++ b/tests/unit/framework/validators/FakedValidationModel.php @@ -17,4 +17,9 @@ class FakedValidationModel { $this->errors[$attribute] = $message; } + + public function resetErrors() + { + $this->errors = array(); + } } \ No newline at end of file diff --git a/tests/unit/framework/validators/UrlValidatorTest.php b/tests/unit/framework/validators/UrlValidatorTest.php new file mode 100644 index 0000000..81ddf41 --- /dev/null +++ b/tests/unit/framework/validators/UrlValidatorTest.php @@ -0,0 +1,89 @@ +assertFalse($val->validateValue('google.de')); + $this->assertTrue($val->validateValue('http://google.de')); + $this->assertTrue($val->validateValue('https://google.de')); + $this->assertFalse($val->validateValue('htp://yiiframework.com')); + $this->assertTrue($val->validateValue('https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8' + .'&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr')); + $this->assertFalse($val->validateValue('ftp://ftp.ruhr-uni-bochum.de/')); + $this->assertFalse($val->validateValue('http://invalid,domain')); + $this->assertFalse($val->validateValue('http://äüö?=!"§$%&/()=}][{³²€.edu')); + } + + public function testValidateValueWithDefaultScheme() + { + $val = new UrlValidator(array('defaultScheme' => 'https')); + $this->assertTrue($val->validateValue('yiiframework.com')); + $this->assertTrue($val->validateValue('http://yiiframework.com')); + } + + public function testValidateWithCustomScheme() + { + $val = new UrlValidator(array( + 'validSchemes' => array('http', 'https', 'ftp', 'ftps'), + 'defaultScheme' => 'http', + )); + $this->assertTrue($val->validateValue('ftp://ftp.ruhr-uni-bochum.de/')); + $this->assertTrue($val->validateValue('google.de')); + $this->assertTrue($val->validateValue('http://google.de')); + $this->assertTrue($val->validateValue('https://google.de')); + $this->assertFalse($val->validateValue('htp://yiiframework.com')); + // relative urls not supported + $this->assertFalse($val->validateValue('//yiiframework.com')); + } + + public function testValidateWithIdn() + { + if(!function_exists('idn_to_ascii')) { + $this->markTestSkipped('intl package required'); + return; + } + $val = new UrlValidator(array( + 'enableIDN' => true, + )); + $this->assertTrue($val->validateValue('http://äüößìà.de')); + // converted via http://mct.verisign-grs.com/convertServlet + $this->assertTrue($val->validateValue('http://xn--zcack7ayc9a.de')); + } + + public function testValidateLength() + { + $url = 'http://' . str_pad('base', 2000, 'url') . '.de'; + $val = new UrlValidator; + $this->assertFalse($val->validateValue($url)); + } + + public function testValidateAttributeAndError() + { + $obj = new FakedValidationModel; + $obj->url = 'http://google.de'; + $val = new UrlValidator; + $val->validateAttribute($obj, 'url'); + $this->assertFalse(isset($obj->errors['url'])); + $this->assertSame('http://google.de', $obj->url); + $obj->resetErrors(); + $val->defaultScheme = 'http'; + $obj->url = 'google.de'; + $val->validateAttribute($obj, 'url'); + $this->assertFalse(isset($obj->errors['url'])); + $this->assertTrue(stripos($obj->url, 'http') !== false); + $obj->resetErrors(); + $obj->url = 'gttp;/invalid string'; + $val->validateAttribute($obj, 'url'); + $this->assertTrue(isset($obj->errors['url'])); + } +} From ec1dfeaa587878fc8ae0dd02fcb2b444d12419f2 Mon Sep 17 00:00:00 2001 From: Suralc Date: Mon, 29 Jul 2013 16:40:33 +0200 Subject: [PATCH 4/6] Ignoring javascript generating methods in coverage for now. --- framework/yii/validators/BooleanValidator.php | 1 + framework/yii/validators/CaptchaValidator.php | 1 + framework/yii/validators/CompareValidator.php | 1 + framework/yii/validators/EmailValidator.php | 1 + framework/yii/validators/NumberValidator.php | 1 + framework/yii/validators/RangeValidator.php | 1 + framework/yii/validators/RegularExpressionValidator.php | 1 + framework/yii/validators/RequiredValidator.php | 1 + framework/yii/validators/StringValidator.php | 1 + framework/yii/validators/UrlValidator.php | 1 + 10 files changed, 10 insertions(+) diff --git a/framework/yii/validators/BooleanValidator.php b/framework/yii/validators/BooleanValidator.php index d1e81b8..6b6fded 100644 --- a/framework/yii/validators/BooleanValidator.php +++ b/framework/yii/validators/BooleanValidator.php @@ -82,6 +82,7 @@ class BooleanValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/CaptchaValidator.php b/framework/yii/validators/CaptchaValidator.php index 2a16f57..379859c 100644 --- a/framework/yii/validators/CaptchaValidator.php +++ b/framework/yii/validators/CaptchaValidator.php @@ -97,6 +97,7 @@ class CaptchaValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/CompareValidator.php b/framework/yii/validators/CompareValidator.php index 8c9e587..9d427ff 100644 --- a/framework/yii/validators/CompareValidator.php +++ b/framework/yii/validators/CompareValidator.php @@ -181,6 +181,7 @@ class CompareValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @throws InvalidConfigException if CompareValidator::operator is invalid + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/EmailValidator.php b/framework/yii/validators/EmailValidator.php index c3d8480..dba1d7b 100644 --- a/framework/yii/validators/EmailValidator.php +++ b/framework/yii/validators/EmailValidator.php @@ -122,6 +122,7 @@ class EmailValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/NumberValidator.php b/framework/yii/validators/NumberValidator.php index 2fee133..2c96e49 100644 --- a/framework/yii/validators/NumberValidator.php +++ b/framework/yii/validators/NumberValidator.php @@ -117,6 +117,7 @@ class NumberValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/RangeValidator.php b/framework/yii/validators/RangeValidator.php index f89d420..5474d32 100644 --- a/framework/yii/validators/RangeValidator.php +++ b/framework/yii/validators/RangeValidator.php @@ -84,6 +84,7 @@ class RangeValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/RegularExpressionValidator.php b/framework/yii/validators/RegularExpressionValidator.php index 4ae2099..84fd292 100644 --- a/framework/yii/validators/RegularExpressionValidator.php +++ b/framework/yii/validators/RegularExpressionValidator.php @@ -83,6 +83,7 @@ class RegularExpressionValidator extends Validator * containing a model form with this validator applied. * @return string the client-side validation script. * @throws InvalidConfigException if the "pattern" is not a valid regular expression + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/RequiredValidator.php b/framework/yii/validators/RequiredValidator.php index 9ca0ecb..6cab6a9 100644 --- a/framework/yii/validators/RequiredValidator.php +++ b/framework/yii/validators/RequiredValidator.php @@ -105,6 +105,7 @@ class RequiredValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/StringValidator.php b/framework/yii/validators/StringValidator.php index 4cc9dba..ff8570e 100644 --- a/framework/yii/validators/StringValidator.php +++ b/framework/yii/validators/StringValidator.php @@ -129,6 +129,7 @@ class StringValidator extends Validator * @param \yii\base\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { diff --git a/framework/yii/validators/UrlValidator.php b/framework/yii/validators/UrlValidator.php index c9b3a2b..29663f1 100644 --- a/framework/yii/validators/UrlValidator.php +++ b/framework/yii/validators/UrlValidator.php @@ -121,6 +121,7 @@ class UrlValidator extends Validator * containing a model form with this validator applied. * @return string the client-side validation script. * @see \yii\Web\ActiveForm::enableClientValidation + * @codeCoverageIgnore */ public function clientValidateAttribute($object, $attribute, $view) { From 1c6b64e7172cedc2733bd37eb0f329bd1d3fd47a Mon Sep 17 00:00:00 2001 From: Suralc Date: Wed, 31 Jul 2013 19:42:20 +0200 Subject: [PATCH 5/6] Added CompareValidatorTest and improved BooleanValidatorTest and UrlValidatorTest --- .../framework/validators/BooleanValidatorTest.php | 11 +- .../framework/validators/CompareValidatorTest.php | 160 +++++++++++++++++++++ .../framework/validators/FakedValidationModel.php | 30 ++-- .../unit/framework/validators/UrlValidatorTest.php | 26 ++-- 4 files changed, 197 insertions(+), 30 deletions(-) create mode 100644 tests/unit/framework/validators/CompareValidatorTest.php diff --git a/tests/unit/framework/validators/BooleanValidatorTest.php b/tests/unit/framework/validators/BooleanValidatorTest.php index 655ef58..e3fa3e5 100644 --- a/tests/unit/framework/validators/BooleanValidatorTest.php +++ b/tests/unit/framework/validators/BooleanValidatorTest.php @@ -1,5 +1,6 @@ assertTrue($val->validateValue(true)); $this->assertTrue($val->validateValue(false)); } - + public function testValidateAttributeAndError() { $obj = new FakedValidationModel; @@ -41,13 +42,13 @@ class BooleanValidatorTest extends TestCase $obj->attrD = array(); $val = new BooleanValidator; $val->validateAttribute($obj, 'attrA'); - $this->assertFalse(isset($obj->errors['attrA'])); + $this->assertFalse($obj->hasErrors('attrA')); $val->validateAttribute($obj, 'attrC'); - $this->assertFalse(isset($obj->errors['attrC'])); + $this->assertFalse($obj->hasErrors('attrC')); $val->strict = true; $val->validateAttribute($obj, 'attrB'); - $this->assertFalse(isset($obj->errors['attrB'])); + $this->assertFalse($obj->hasErrors('attrB')); $val->validateAttribute($obj, 'attrD'); - $this->assertTrue(isset($obj->errors['attrD'])); + $this->assertTrue($obj->hasErrors('attrD')); } } diff --git a/tests/unit/framework/validators/CompareValidatorTest.php b/tests/unit/framework/validators/CompareValidatorTest.php new file mode 100644 index 0000000..c91e7d6 --- /dev/null +++ b/tests/unit/framework/validators/CompareValidatorTest.php @@ -0,0 +1,160 @@ +setExpectedException('yii\base\InvalidConfigException'); + $val = new CompareValidator; + $val->validateValue('val'); + } + + public function testValidateValue() + { + $value = 18449; + // default config + $val = new CompareValidator(array('compareValue' => $value)); + $this->assertTrue($val->validateValue($value)); + $this->assertTrue($val->validateValue((string)$value)); + $this->assertFalse($val->validateValue($value + 1)); + foreach ($this->getOperationTestData($value) as $op => $tests) { + $val = new CompareValidator(array('compareValue' => $value)); + $val->operator = $op; + foreach ($tests as $test) { + $this->assertEquals($test[1], $val->validateValue($test[0])); + } + } + } + + protected function getOperationTestData($value) + { + return array( + '===' => array( + array($value, true), + array((string)$value, false), + array((float)$value, false), + array($value + 1, false), + ), + '!=' => array( + array($value, false), + array((string)$value, false), + array((float)$value, false), + array($value + 0.00001, true), + array(false, true), + ), + '!==' => array( + array($value, false), + array((string)$value, true), + array((float)$value, true), + array(false, true), + ), + '>' => array( + array($value, false), + array($value + 1, true), + array($value - 1, false), + ), + '>=' => array( + array($value, true), + array($value + 1, true), + array($value - 1, false), + ), + '<' => array( + array($value, false), + array($value + 1, false), + array($value - 1, true), + ), + '<=' => array( + array($value, true), + array($value + 1, false), + array($value - 1, true), + ), + + ); + } + + public function testValidateAttribute() + { + // invalid-array + $val = new CompareValidator; + $model = new FakedValidationModel; + $model->attr = array('test_val'); + $val->validateAttribute($model, 'attr'); + $this->assertTrue($model->hasErrors('attr')); + $val = new CompareValidator(array('compareValue' => 'test-string')); + $model = new FakedValidationModel; + $model->attr_test = 'test-string'; + $val->validateAttribute($model, 'attr_test'); + $this->assertFalse($model->hasErrors('attr_test')); + $val = new CompareValidator(array('compareAttribute' => 'attr_test_val')); + $model = new FakedValidationModel; + $model->attr_test = 'test-string'; + $model->attr_test_val = 'test-string'; + $val->validateAttribute($model, 'attr_test'); + $this->assertFalse($model->hasErrors('attr_test')); + $this->assertFalse($model->hasErrors('attr_test_val')); + $val = new CompareValidator(array('compareAttribute' => 'attr_test_val')); + $model = new FakedValidationModel; + $model->attr_test = 'test-string'; + $model->attr_test_val = 'test-string-false'; + $val->validateAttribute($model, 'attr_test'); + $this->assertTrue($model->hasErrors('attr_test')); + $this->assertFalse($model->hasErrors('attr_test_val')); + // assume: _repeat + $val = new CompareValidator; + $model = new FakedValidationModel; + $model->attr_test = 'test-string'; + $model->attr_test_repeat = 'test-string'; + $val->validateAttribute($model, 'attr_test'); + $this->assertFalse($model->hasErrors('attr_test')); + $this->assertFalse($model->hasErrors('attr_test_repeat')); + $val = new CompareValidator; + $model = new FakedValidationModel; + $model->attr_test = 'test-string'; + $model->attr_test_repeat = 'test-string2'; + $val->validateAttribute($model, 'attr_test'); + $this->assertTrue($model->hasErrors('attr_test')); + $this->assertFalse($model->hasErrors('attr_test_repeat')); + } + + public function testValidateAttributeOperators() + { + $value = 55; + foreach ($this->getOperationTestData($value) as $operator => $tests) { + $val = new CompareValidator(array('operator' => $operator, 'compareValue' => $value)); + foreach ($tests as $test) { + $model = new FakedValidationModel; + $model->attr_test = $test[0]; + $val->validateAttribute($model, 'attr_test'); + $this->assertEquals($test[1], !$model->hasErrors('attr_test')); + } + + } + } + + public function testEnsureMessageSetOnInit() + { + foreach ($this->getOperationTestData(1337) as $operator => $tests) { + $val = new CompareValidator(array('operator' => $operator)); + $this->assertTrue(strlen($val->message) > 1); + } + try { + $val = new CompareValidator(array('operator' => '<>')); + } catch (InvalidConfigException $e) { + return; + } + catch (\Exception $e) { + $this->fail('InvalidConfigException expected' . get_class($e) . 'received'); + return; + } + $this->fail('InvalidConfigException expected none received'); + } +} \ No newline at end of file diff --git a/tests/unit/framework/validators/FakedValidationModel.php b/tests/unit/framework/validators/FakedValidationModel.php index 89632c7..15a2a94 100644 --- a/tests/unit/framework/validators/FakedValidationModel.php +++ b/tests/unit/framework/validators/FakedValidationModel.php @@ -2,24 +2,30 @@ namespace yiiunit\framework\validators; +use yii\base\Model; + /** * @codeCoverageIgnore */ -class FakedValidationModel +class FakedValidationModel extends Model { - public $errors = array(); - public function getAttributeLabel($attr) - { - return 'Attr-Label: '.$attr; - } - - public function addError($attribute, $message) + private $attr = array(); + + public function __get($name) { - $this->errors[$attribute] = $message; + if (stripos($name, 'attr') === 0) { + return isset($this->attr[$name]) ? $this->attr[$name] : null; + } + + return parent::__get($name); } - - public function resetErrors() + + public function __set($name, $value) { - $this->errors = array(); + if (stripos($name, 'attr') === 0) { + $this->attr[$name] = $value; + } else { + parent::__set($name, $value); + } } } \ No newline at end of file diff --git a/tests/unit/framework/validators/UrlValidatorTest.php b/tests/unit/framework/validators/UrlValidatorTest.php index 81ddf41..f585590 100644 --- a/tests/unit/framework/validators/UrlValidatorTest.php +++ b/tests/unit/framework/validators/UrlValidatorTest.php @@ -70,20 +70,20 @@ class UrlValidatorTest extends TestCase public function testValidateAttributeAndError() { $obj = new FakedValidationModel; - $obj->url = 'http://google.de'; + $obj->attr_url = 'http://google.de'; $val = new UrlValidator; - $val->validateAttribute($obj, 'url'); - $this->assertFalse(isset($obj->errors['url'])); - $this->assertSame('http://google.de', $obj->url); - $obj->resetErrors(); + $val->validateAttribute($obj, 'attr_url'); + $this->assertFalse($obj->hasErrors('attr_url')); + $this->assertSame('http://google.de', $obj->attr_url); + $obj = new FakedValidationModel; $val->defaultScheme = 'http'; - $obj->url = 'google.de'; - $val->validateAttribute($obj, 'url'); - $this->assertFalse(isset($obj->errors['url'])); - $this->assertTrue(stripos($obj->url, 'http') !== false); - $obj->resetErrors(); - $obj->url = 'gttp;/invalid string'; - $val->validateAttribute($obj, 'url'); - $this->assertTrue(isset($obj->errors['url'])); + $obj->attr_url = 'google.de'; + $val->validateAttribute($obj, 'attr_url'); + $this->assertFalse($obj->hasErrors('attr_url')); + $this->assertTrue(stripos($obj->attr_url, 'http') !== false); + $obj = new FakedValidationModel; + $obj->attr_url = 'gttp;/invalid string'; + $val->validateAttribute($obj, 'attr_url'); + $this->assertTrue($obj->hasErrors('attr_url')); } } From 777ff792c4694926ce8b2795b5627be3c9bd34f4 Mon Sep 17 00:00:00 2001 From: Suralc Date: Wed, 31 Jul 2013 20:12:45 +0200 Subject: [PATCH 6/6] DateValidatorTest --- .../framework/validators/DateValidatorTest.php | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/unit/framework/validators/DateValidatorTest.php diff --git a/tests/unit/framework/validators/DateValidatorTest.php b/tests/unit/framework/validators/DateValidatorTest.php new file mode 100644 index 0000000..445d57d --- /dev/null +++ b/tests/unit/framework/validators/DateValidatorTest.php @@ -0,0 +1,61 @@ +assertTrue($val->message !== null && strlen($val->message) > 1); + } + + public function testValidateValue() + { + $val = new DateValidator; + $this->assertTrue($val->validateValue('2013-09-13')); + $this->assertFalse($val->validateValue('31.7.2013')); + $this->assertFalse($val->validateValue('31-7-2013')); + $this->assertFalse($val->validateValue(time())); + $val->format = 'U'; + $this->assertTrue($val->validateValue(time())); + $val->format = 'd.m.Y'; + $this->assertTrue($val->validateValue('31.7.2013')); + $val->format = 'Y-m-!d H:i:s'; + $this->assertTrue($val->validateValue('2009-02-15 15:16:17')); + } + + public function testValidateAttribute() + { + // error-array-add + $val = new DateValidator; + $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(array('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( + DateTime::createFromFormat($val->format, '2013-09-13')->getTimestamp(), + $model->attr_timestamp + ); + + + } +} \ No newline at end of file