-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
[WIP] Validator tests #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
cde9784
BooleanValidatorTest
suralc 09c5e61
fix test
suralc fc1d2af
UrlValidator Tests
suralc ec1dfea
Ignoring javascript generating methods in coverage for now.
suralc 1c6b64e
Added CompareValidatorTest and improved BooleanValidatorTest and UrlV…
suralc 777ff79
DateValidatorTest
suralc 1c57661
Merge branch 'add-tests' of github.com:suralc/yii2 into add-tests
suralc 1ce7344
Additional tests in EmailValidator.
suralc 467d88f
Added StringValidatorTest
suralc f7bb54b
Test fix for Php 5.3
suralc f78a77d
Merge branch 'master' of github.com:yiisoft/yii2 into add-tests
suralc 8f8de7c
Added NumberValidatorTest
suralc b5472c2
Typo fix
suralc a81480d
Improved ValidatorTest organization.
suralc d9f87f4
Added RequiredValidatorTest
suralc bae79cd
Added ValidatorTest
suralc 02c92fe
Merge remote-tracking branch 'upstream/master' into add-tests
suralc 8e48f01
Add RegularExpressionValidatorTest
suralc 0133069
Merge remote-tracking branch 'upstream/master' into add-tests
suralc 5c8927a
Added ExistValidatorTest and added new option to DatabaseTestCase
suralc 0c45765
Added tests for uncovered lines.
suralc 3469b2c
RangeValidator tests.
suralc 3b07e4a
Added FileValidator tests.
suralc 7d5bb08
FilterValidator test
suralc 24e2f88
Renamed validator test models to be less specific.
suralc c189e5a
UniqueValidator test.
suralc 06682d0
coverage improvements
suralc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
|
|
||
| namespace yiiunit\data\validators; | ||
|
|
||
|
|
||
| use yii\validators\Validator; | ||
|
|
||
| class TestValidator extends Validator | ||
| { | ||
| private $_validatedAttributes = array(); | ||
| private $_setErrorOnValidateAttribute = false; | ||
|
|
||
| public function validateAttribute($object, $attribute) | ||
| { | ||
| $this->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; | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
tests/unit/data/validators/models/FakedValidationModel.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
|
|
||
| namespace yiiunit\data\validators\models; | ||
|
|
||
| use yii\base\Model; | ||
|
|
||
| /** | ||
| * @codeCoverageIgnore | ||
| */ | ||
| class FakedValidationModel extends Model | ||
| { | ||
| 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() | ||
| { | ||
| return array( | ||
| array('val_attr_a, val_attr_b', 'required', 'on' => 'reqTest'), | ||
| array('val_attr_c', 'integer'), | ||
| ); | ||
| } | ||
|
|
||
| public function inlineVal($attribute, $params = array()) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function __get($name) | ||
| { | ||
| if (stripos($name, 'attr') === 0) { | ||
| return isset($this->attr[$name]) ? $this->attr[$name] : null; | ||
| } | ||
|
|
||
| return parent::__get($name); | ||
| } | ||
|
|
||
| public function __set($name, $value) | ||
| { | ||
| if (stripos($name, 'attr') === 0) { | ||
| $this->attr[$name] = $value; | ||
| } else { | ||
| parent::__set($name, $value); | ||
| } | ||
| } | ||
|
|
||
| public function getAttributeLabel($attr) | ||
| { | ||
| return $attr; | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
tests/unit/data/validators/models/ValidatorTestMainModel.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| namespace yiiunit\data\validators\models; | ||
|
|
||
|
|
||
| use yii\db\ActiveRecord; | ||
|
|
||
| class ValidatorTestMainModel extends ActiveRecord | ||
| { | ||
| public $testMainVal = 1; | ||
|
|
||
| public static function tableName() | ||
| { | ||
| return 'tbl_validator_main'; | ||
| } | ||
|
|
||
| public function getReferences() | ||
| { | ||
| return $this->hasMany(ValidatorTestRefModel::className(), array('ref' => 'id')); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
tests/unit/data/validators/models/ValidatorTestRefModel.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace yiiunit\data\validators\models; | ||
|
|
||
|
|
||
| use yii\db\ActiveRecord; | ||
|
|
||
| class ValidatorTestRefModel extends ActiveRecord | ||
| { | ||
|
|
||
| public $test_val = 2; | ||
| public $test_val_fail = 99; | ||
|
|
||
| public static function tableName() | ||
| { | ||
| return 'tbl_validator_ref'; | ||
| } | ||
|
|
||
| public function getMain() | ||
| { | ||
| return $this->hasOne(ValidatorTestMainModel::className(), array('id' => 'ref')); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about naming, or if this is to be avoided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems we don't need it. You can assign
ActiveRecord::$dbwith$this->getConnection(), likeActiveRecordTest.