From bae79cd76c6459198e0d5b79cbb6b09e08b089e1 Mon Sep 17 00:00:00 2001 From: Suralc Date: Tue, 13 Aug 2013 16:51:53 +0200 Subject: [PATCH] Added ValidatorTest --- tests/unit/data/validators/TestValidator.php | 44 ++++ .../validators/models/FakedValidationModel.php | 15 +- tests/unit/framework/validators/ValidatorTest.php | 227 +++++++++++++++++++++ 3 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 tests/unit/data/validators/TestValidator.php create mode 100644 tests/unit/framework/validators/ValidatorTest.php diff --git a/tests/unit/data/validators/TestValidator.php b/tests/unit/data/validators/TestValidator.php new file mode 100644 index 0000000..13f12dc --- /dev/null +++ b/tests/unit/data/validators/TestValidator.php @@ -0,0 +1,44 @@ +markAttributeValidated($attribute); + if ($this->_setErrorOnValidateAttribute == true) { + $this->addError($object, $attribute, sprintf('%s##%s', $attribute, get_class($object))); + } + } + + protected function markAttributeValidated($attr, $increaseBy = 1) + { + if (!isset($this->_validatedAttributes[$attr])) { + $this->_validatedAttributes[$attr] = 1; + } else { + $this->_validatedAttributes[$attr] = $this->_validatedAttributes[$attr] + $increaseBy; + } + } + + public function countAttributeValidations($attr) + { + return isset($this->_validatedAttributes[$attr]) ? $this->_validatedAttributes[$attr] : 0; + } + + public function isAttributeValidated($attr) + { + return isset($this->_validatedAttributes[$attr]); + } + + public function enableErrorOnValidateAttribute() + { + $this->_setErrorOnValidateAttribute = true; + } +} \ No newline at end of file diff --git a/tests/unit/data/validators/models/FakedValidationModel.php b/tests/unit/data/validators/models/FakedValidationModel.php index 1e3366c..d2e3f52 100644 --- a/tests/unit/data/validators/models/FakedValidationModel.php +++ b/tests/unit/data/validators/models/FakedValidationModel.php @@ -9,13 +9,24 @@ use yii\base\Model; */ class FakedValidationModel extends Model { - private $attr = array(); - public $val_attr_a; public $val_attr_b; public $val_attr_c; public $val_attr_d; + private $attr = array(); + /** + * @param array $attributes + * @return self + */ + public static function createWithAttributes($attributes = array()) + { + $m = new static(); + foreach ($attributes as $attribute => $value) { + $m->$attribute = $value; + } + return $m; + } public function rules() { diff --git a/tests/unit/framework/validators/ValidatorTest.php b/tests/unit/framework/validators/ValidatorTest.php new file mode 100644 index 0000000..f0a114b --- /dev/null +++ b/tests/unit/framework/validators/ValidatorTest.php @@ -0,0 +1,227 @@ + true, 'attr_runMe2' => true, 'attr_skip' => true), + $additionalAttributes + ); + return FakedValidationModel::createWithAttributes($attributes); + } + + public function testCreateValidator() + { + $model = FakedValidationModel::createWithAttributes(array('attr_test1' => 'abc', 'attr_test2' => '2013')); + /** @var $numberVal NumberValidator */ + $numberVal = TestValidator::createValidator('number', $model, array('attr_test1')); + $this->assertInstanceOf(NumberValidator::className(), $numberVal); + $numberVal = TestValidator::createValidator('integer', $model, array('attr_test2')); + $this->assertInstanceOf(NumberValidator::className(), $numberVal); + $this->assertTrue($numberVal->integerOnly); + $val = TestValidator::createValidator( + 'boolean', + $model, + 'attr_test1, attr_test2', + array('on' => array('a', 'b')) + ); + $this->assertInstanceOf(BooleanValidator::className(), $val); + $this->assertSame(array('a', 'b'), $val->on); + $this->assertSame(array('attr_test1', 'attr_test2'), $val->attributes); + $val = TestValidator::createValidator( + 'boolean', + $model, + 'attr_test1, attr_test2', + array('on' => 'a, b', 'except' => 'c,d,e') + ); + $this->assertInstanceOf(BooleanValidator::className(), $val); + $this->assertSame(array('a', 'b'), $val->on); + $this->assertSame(array('c', 'd', 'e'), $val->except); + $val = TestValidator::createValidator('inlineVal', $model, 'val_attr_a'); + $this->assertInstanceOf(InlineValidator::className(), $val); + $this->assertSame('inlineVal', $val->method); + } + + public function testValidate() + { + $val = new TestValidator(array('attributes' => array('attr_runMe1', 'attr_runMe2'))); + $model = $this->getTestModel(); + $val->validate($model); + $this->assertTrue($val->isAttributeValidated('attr_runMe1')); + $this->assertTrue($val->isAttributeValidated('attr_runMe2')); + $this->assertFalse($val->isAttributeValidated('attr_skip')); + } + + public function testValidateWithAttributeIntersect() + { + $val = new TestValidator(array('attributes' => array('attr_runMe1', 'attr_runMe2'))); + $model = $this->getTestModel(); + $val->validate($model, array('attr_runMe1')); + $this->assertTrue($val->isAttributeValidated('attr_runMe1')); + $this->assertFalse($val->isAttributeValidated('attr_runMe2')); + $this->assertFalse($val->isAttributeValidated('attr_skip')); + } + + public function testValidateWithEmptyAttributes() + { + $val = new TestValidator(); + $model = $this->getTestModel(); + $val->validate($model, array('attr_runMe1')); + $this->assertFalse($val->isAttributeValidated('attr_runMe1')); + $this->assertFalse($val->isAttributeValidated('attr_runMe2')); + $this->assertFalse($val->isAttributeValidated('attr_skip')); + $val->validate($model); + $this->assertFalse($val->isAttributeValidated('attr_runMe1')); + $this->assertFalse($val->isAttributeValidated('attr_runMe2')); + $this->assertFalse($val->isAttributeValidated('attr_skip')); + } + + public function testValidateWithError() + { + $val = new TestValidator(array('attributes' => array('attr_runMe1', 'attr_runMe2'), 'skipOnError' => false)); + $model = $this->getTestModel(); + $val->validate($model); + $this->assertTrue($val->isAttributeValidated('attr_runMe1')); + $this->assertTrue($val->isAttributeValidated('attr_runMe2')); + $this->assertFalse($val->isAttributeValidated('attr_skip')); + $this->assertEquals(1, $val->countAttributeValidations('attr_runMe2')); + $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); + $val->validate($model, array('attr_runMe2')); + $this->assertEquals(2, $val->countAttributeValidations('attr_runMe2')); + $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); + $this->assertEquals(0, $val->countAttributeValidations('attr_skip')); + $val = new TestValidator(array('attributes' => array('attr_runMe1', 'attr_runMe2'), 'skipOnError' => true)); + $model = $this->getTestModel(); + $val->enableErrorOnValidateAttribute(); + $val->validate($model); + $this->assertTrue($val->isAttributeValidated('attr_runMe1')); + $this->assertTrue($val->isAttributeValidated('attr_runMe2')); + $this->assertFalse($val->isAttributeValidated('attr_skip')); + $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); + $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); + $this->assertEquals(0, $val->countAttributeValidations('attr_skip')); + $val->validate($model, array('attr_runMe2')); + $this->assertEquals(1, $val->countAttributeValidations('attr_runMe2')); + $this->assertEquals(1, $val->countAttributeValidations('attr_runMe1')); + $this->assertEquals(0, $val->countAttributeValidations('attr_skip')); + } + + public function testValidateWithEmpty() + { + $val = new TestValidator(array( + 'attributes' => array( + 'attr_runMe1', + 'attr_runMe2', + 'attr_empty1', + 'attr_empty2' + ), + 'skipOnEmpty' => true, + )); + $model = $this->getTestModel(array('attr_empty1' => '', 'attr_emtpy2' => ' ')); + $val->validate($model); + $this->assertTrue($val->isAttributeValidated('attr_runMe1')); + $this->assertTrue($val->isAttributeValidated('attr_runMe2')); + $this->assertFalse($val->isAttributeValidated('attr_empty1')); + $this->assertFalse($val->isAttributeValidated('attr_empty2')); + $model->attr_empty1 = 'not empty anymore'; + $val->validate($model); + $this->assertTrue($val->isAttributeValidated('attr_empty1')); + $this->assertFalse($val->isAttributeValidated('attr_empty2')); + $val = new TestValidator(array( + 'attributes' => array( + 'attr_runMe1', + 'attr_runMe2', + 'attr_empty1', + 'attr_empty2' + ), + 'skipOnEmpty' => false, + )); + $model = $this->getTestModel(array('attr_empty1' => '', 'attr_emtpy2' => ' ')); + $val->validate($model); + $this->assertTrue($val->isAttributeValidated('attr_runMe1')); + $this->assertTrue($val->isAttributeValidated('attr_runMe2')); + $this->assertTrue($val->isAttributeValidated('attr_empty1')); + $this->assertTrue($val->isAttributeValidated('attr_empty2')); + } + + public function testIsEmpty() + { + $val = new TestValidator(); + $this->assertTrue($val->isEmpty(null)); + $this->assertTrue($val->isEmpty(array())); + $this->assertTrue($val->isEmpty('')); + $this->assertFalse($val->isEmpty(5)); + $this->assertFalse($val->isEmpty(0)); + $this->assertFalse($val->isEmpty(new \stdClass())); + $this->assertFalse($val->isEmpty(' ')); + // trim + $this->assertTrue($val->isEmpty(' ', true)); + $this->assertTrue($val->isEmpty('', true)); + $this->assertTrue($val->isEmpty(" \t\n\r\0\x0B", true)); + $this->assertTrue($val->isEmpty('', true)); + $this->assertFalse($val->isEmpty('0', true)); + $this->assertFalse($val->isEmpty(0, true)); + $this->assertFalse($val->isEmpty('this ain\'t an empty value', true)); + } + + public function testValidateValue() + { + $this->setExpectedException( + 'yii\base\NotSupportedException', + TestValidator::className() . ' does not support validateValue().' + ); + $val = new TestValidator(); + $val->validateValue('abc'); + } + + public function testClientValidateAttribute() + { + $val = new TestValidator(); + $this->assertNull( + $val->clientValidateAttribute($this->getTestModel(), 'attr_runMe1', array()) + ); //todo pass a view instead of array + } + + public function testIsActive() + { + $val = new TestValidator(); + $this->assertTrue($val->isActive('scenA')); + $this->assertTrue($val->isActive('scenB')); + $val->except = array('scenB'); + $this->assertTrue($val->isActive('scenA')); + $this->assertFalse($val->isActive('scenB')); + $val->on = array('scenC'); + $this->assertFalse($val->isActive('scenA')); + $this->assertFalse($val->isActive('scenB')); + $this->assertTrue($val->isActive('scenC')); + } + + public function testAddError() + { + $val = new TestValidator(); + $m = $this->getTestModel(array('attr_msg_val' => 'abc')); + $val->addError($m, 'attr_msg_val', '{attribute}::{value}'); + $errors = $m->getErrors('attr_msg_val'); + $this->assertEquals('attr_msg_val::abc', $errors[0]); + $m = $this->getTestModel(array('attr_msg_val' => array('bcc'))); + $val->addError($m, 'attr_msg_val', '{attribute}::{value}'); + $errors = $m->getErrors('attr_msg_val'); + $this->assertEquals('attr_msg_val::array()', $errors[0]); + $m = $this->getTestModel(array('attr_msg_val' => 'abc')); + $val->addError($m, 'attr_msg_val', '{attribute}::{value}::{param}', array('{param}' => 'param_value')); + $errors = $m->getErrors('attr_msg_val'); + $this->assertEquals('attr_msg_val::abc::param_value', $errors[0]); + } +} \ No newline at end of file