Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
467d88fa
Commit
467d88fa
authored
Jul 31, 2013
by
Suralc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added StringValidatorTest
parent
1ce7344e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
117 additions
and
0 deletions
+117
-0
FakedValidationModel.php
tests/unit/framework/validators/FakedValidationModel.php
+6
-0
StringValidatorTest.php
tests/unit/framework/validators/StringValidatorTest.php
+111
-0
No files found.
tests/unit/framework/validators/FakedValidationModel.php
View file @
467d88fa
...
@@ -28,4 +28,9 @@ class FakedValidationModel extends Model
...
@@ -28,4 +28,9 @@ class FakedValidationModel extends Model
parent
::
__set
(
$name
,
$value
);
parent
::
__set
(
$name
,
$value
);
}
}
}
}
public
function
getAttributeLabel
(
$attr
)
{
return
$attr
;
}
}
}
\ No newline at end of file
tests/unit/framework/validators/StringValidatorTest.php
0 → 100644
View file @
467d88fa
<?php
namespace
yiiunit\framework\validators
;
use
yii\validators\StringValidator
;
use
yiiunit\TestCase
;
class
StringValidatorTest
extends
TestCase
{
public
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateValue
()
{
$val
=
new
StringValidator
();
$this
->
assertFalse
(
$val
->
validateValue
(
array
(
'not a string'
)));
$this
->
assertTrue
(
$val
->
validateValue
(
'Just some string'
));
}
public
function
testValidateValueLength
()
{
$val
=
new
StringValidator
(
array
(
'length'
=>
25
));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
25
)));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'€'
,
25
)));
$this
->
assertFalse
(
$val
->
validateValue
(
str_repeat
(
'x'
,
125
)));
$this
->
assertFalse
(
$val
->
validateValue
(
''
));
$val
=
new
StringValidator
(
array
(
'length'
=>
array
(
25
)));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
25
)));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
1250
)));
$this
->
assertFalse
(
$val
->
validateValue
(
str_repeat
(
'Ä'
,
24
)));
$this
->
assertFalse
(
$val
->
validateValue
(
''
));
$val
=
new
StringValidator
(
array
(
'length'
=>
array
(
10
,
20
)));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
15
)));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
10
)));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
20
)));
$this
->
assertFalse
(
$val
->
validateValue
(
str_repeat
(
'x'
,
5
)));
$this
->
assertFalse
(
$val
->
validateValue
(
str_repeat
(
'x'
,
25
)));
$this
->
assertFalse
(
$val
->
validateValue
(
''
));
// make sure min/max are overridden
$val
=
new
StringValidator
(
array
(
'length'
=>
array
(
10
,
20
),
'min'
=>
25
,
'max'
=>
35
));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
15
)));
$this
->
assertFalse
(
$val
->
validateValue
(
str_repeat
(
'x'
,
30
)));
}
public
function
testValidateValueMinMax
()
{
$val
=
new
StringValidator
(
array
(
'min'
=>
10
));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'x'
,
10
)));
$this
->
assertFalse
(
$val
->
validateValue
(
'xxxx'
));
$val
=
new
StringValidator
(
array
(
'max'
=>
10
));
$this
->
assertTrue
(
$val
->
validateValue
(
'xxxx'
));
$this
->
assertFalse
(
$val
->
validateValue
(
str_repeat
(
'y'
,
20
)));
$val
=
new
StringValidator
(
array
(
'min'
=>
10
,
'max'
=>
20
));
$this
->
assertTrue
(
$val
->
validateValue
(
str_repeat
(
'y'
,
15
)));
$this
->
assertFalse
(
$val
->
validateValue
(
'abc'
));
$this
->
assertFalse
(
$val
->
validateValue
(
str_repeat
(
'b'
,
25
)));
}
public
function
testValidateAttribute
()
{
$val
=
new
StringValidator
();
$model
=
new
FakedValidationModel
();
$model
->
attr_string
=
'a tet string'
;
$val
->
validateAttribute
(
$model
,
'attr_string'
);
$this
->
assertFalse
(
$model
->
hasErrors
());
$val
=
new
StringValidator
(
array
(
'length'
=>
20
));
$model
=
new
FakedValidationModel
();
$model
->
attr_string
=
str_repeat
(
'x'
,
20
);
$val
->
validateAttribute
(
$model
,
'attr_string'
);
$this
->
assertFalse
(
$model
->
hasErrors
());
$model
=
new
FakedValidationModel
();
$model
->
attr_string
=
'abc'
;
$val
->
validateAttribute
(
$model
,
'attr_string'
);
$this
->
assertTrue
(
$model
->
hasErrors
(
'attr_string'
));
$val
=
new
StringValidator
(
array
(
'max'
=>
2
));
$model
=
new
FakedValidationModel
();
$model
->
attr_string
=
'a'
;
$val
->
validateAttribute
(
$model
,
'attr_string'
);
$this
->
assertFalse
(
$model
->
hasErrors
());
$model
=
new
FakedValidationModel
();
$model
->
attr_string
=
'abc'
;
$val
->
validateAttribute
(
$model
,
'attr_string'
);
$this
->
assertTrue
(
$model
->
hasErrors
(
'attr_string'
));
}
public
function
testEnsureMessagesOnInit
()
{
$val
=
new
StringValidator
(
array
(
'min'
=>
1
,
'max'
=>
2
));
$this
->
assertTrue
(
is_string
(
$val
->
message
));
$this
->
assertTrue
(
is_string
(
$val
->
tooLong
));
$this
->
assertTrue
(
is_string
(
$val
->
tooShort
));
}
public
function
testCustomErrorMessageInValidateAttribute
()
{
$val
=
new
StringValidator
(
array
(
'min'
=>
5
,
'tooShort'
=>
'{attribute} to short. Min is {min}'
,
));
$model
=
new
FakedValidationModel
();
$model
->
attr_string
=
'abc'
;
$val
->
validateAttribute
(
$model
,
'attr_string'
);
$this
->
assertTrue
(
$model
->
hasErrors
(
'attr_string'
));
$this
->
assertEquals
(
'attr_string to short. Min is 5'
,
$model
->
getErrors
(
'attr_string'
)[
0
]);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment