Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Y
yii2bootstrap
Project
Project
Details
Activity
Releases
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
夏彬
yii2bootstrap
Commits
8c667821
Commit
8c667821
authored
Mar 29, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of git.yiisoft.com:yii2
parents
e7295ad5
99238886
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
281 additions
and
1 deletion
+281
-1
Model.php
framework/base/Model.php
+1
-1
InvalidRulesModel.php
tests/unit/data/base/InvalidRulesModel.php
+17
-0
Singer.php
tests/unit/data/base/Singer.php
+21
-0
Speaker.php
tests/unit/data/base/Speaker.php
+39
-0
ModelTest.php
tests/unit/framework/base/ModelTest.php
+203
-0
No files found.
framework/base/Model.php
View file @
8c667821
...
...
@@ -329,7 +329,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
foreach
(
$this
->
rules
()
as
$rule
)
{
if
(
$rule
instanceof
Validator
)
{
$validators
->
add
(
$rule
);
}
elseif
(
isset
(
$rule
[
0
],
$rule
[
1
]))
{
// attributes, validator type
}
elseif
(
is
_array
(
$rule
)
&&
is
set
(
$rule
[
0
],
$rule
[
1
]))
{
// attributes, validator type
$validator
=
Validator
::
createValidator
(
$rule
[
1
],
$this
,
$rule
[
0
],
array_slice
(
$rule
,
2
));
$validators
->
add
(
$validator
);
}
else
{
...
...
tests/unit/data/base/InvalidRulesModel.php
0 → 100644
View file @
8c667821
<?php
namespace
yiiunit\data\base
;
use
yii\base\Model
;
/**
* InvalidRulesModel
*/
class
InvalidRulesModel
extends
Model
{
public
function
rules
()
{
return
array
(
array
(
'test'
),
);
}
}
tests/unit/data/base/Singer.php
0 → 100644
View file @
8c667821
<?php
namespace
yiiunit\data\base
;
use
yii\base\Model
;
/**
* Singer
*/
class
Singer
extends
Model
{
public
$fistName
;
public
$lastName
;
public
function
rules
()
{
return
array
(
array
(
'lastName'
,
'default'
,
'value'
=>
'Lennon'
),
array
(
'lastName'
,
'required'
),
array
(
'underscore_style'
,
'yii\validators\CaptchaValidator'
),
);
}
}
\ No newline at end of file
tests/unit/data/base/Speaker.php
0 → 100644
View file @
8c667821
<?php
namespace
yiiunit\data\base
;
use
yii\base\Model
;
/**
* Speaker
*/
class
Speaker
extends
Model
{
public
$firstName
;
public
$lastName
;
public
$customLabel
;
public
$underscore_style
;
protected
$protectedProperty
;
private
$_privateProperty
;
public
function
attributeLabels
()
{
return
array
(
'customLabel'
=>
'This is the custom label'
,
);
}
public
function
rules
()
{
return
array
(
);
}
public
function
scenarios
()
{
return
array
(
'test'
=>
array
(
'firstName'
,
'lastName'
,
'!underscore_style'
),
);
}
}
tests/unit/framework/base/ModelTest.php
0 → 100644
View file @
8c667821
<?php
namespace
yiiunit\framework\base
;
use
yii\base\Model
;
use
yiiunit\TestCase
;
use
yiiunit\data\base\Speaker
;
use
yiiunit\data\base\Singer
;
use
yiiunit\data\base\InvalidRulesModel
;
/**
* ModelTest
*/
class
ModelTest
extends
TestCase
{
public
function
testGetAttributeLalel
()
{
$speaker
=
new
Speaker
();
$this
->
assertEquals
(
'First Name'
,
$speaker
->
getAttributeLabel
(
'firstName'
));
$this
->
assertEquals
(
'This is the custom label'
,
$speaker
->
getAttributeLabel
(
'customLabel'
));
$this
->
assertEquals
(
'Underscore Style'
,
$speaker
->
getAttributeLabel
(
'underscore_style'
));
}
public
function
testGetAttributes
()
{
$speaker
=
new
Speaker
();
$speaker
->
firstName
=
'Qiang'
;
$speaker
->
lastName
=
'Xue'
;
$this
->
assertEquals
(
array
(
'firstName'
=>
'Qiang'
,
'lastName'
=>
'Xue'
,
'customLabel'
=>
null
,
'underscore_style'
=>
null
,
),
$speaker
->
getAttributes
());
$this
->
assertEquals
(
array
(
'firstName'
=>
'Qiang'
,
'lastName'
=>
'Xue'
,
),
$speaker
->
getAttributes
(
array
(
'firstName'
,
'lastName'
)));
$this
->
assertEquals
(
array
(
'firstName'
=>
'Qiang'
,
'lastName'
=>
'Xue'
,
),
$speaker
->
getAttributes
(
null
,
array
(
'customLabel'
,
'underscore_style'
)));
$this
->
assertEquals
(
array
(
'firstName'
=>
'Qiang'
,
),
$speaker
->
getAttributes
(
array
(
'firstName'
,
'lastName'
),
array
(
'lastName'
,
'customLabel'
,
'underscore_style'
)));
}
public
function
testSetAttributes
()
{
// by default mass assignment doesn't work at all
$speaker
=
new
Speaker
();
$speaker
->
setAttributes
(
array
(
'firstName'
=>
'Qiang'
,
'underscore_style'
=>
'test'
));
$this
->
assertNull
(
$speaker
->
firstName
);
$this
->
assertNull
(
$speaker
->
underscore_style
);
// in the test scenario
$speaker
=
new
Speaker
();
$speaker
->
setScenario
(
'test'
);
$speaker
->
setAttributes
(
array
(
'firstName'
=>
'Qiang'
,
'underscore_style'
=>
'test'
));
$this
->
assertNull
(
$speaker
->
underscore_style
);
$this
->
assertEquals
(
'Qiang'
,
$speaker
->
firstName
);
$speaker
->
setAttributes
(
array
(
'firstName'
=>
'Qiang'
,
'underscore_style'
=>
'test'
),
false
);
$this
->
assertEquals
(
'test'
,
$speaker
->
underscore_style
);
$this
->
assertEquals
(
'Qiang'
,
$speaker
->
firstName
);
}
public
function
testActiveAttributes
()
{
// by default mass assignment doesn't work at all
$speaker
=
new
Speaker
();
$this
->
assertEmpty
(
$speaker
->
activeAttributes
());
$speaker
=
new
Speaker
();
$speaker
->
setScenario
(
'test'
);
$this
->
assertEquals
(
array
(
'firstName'
,
'lastName'
,
'underscore_style'
),
$speaker
->
activeAttributes
());
}
public
function
testIsAttributeSafe
()
{
// by default mass assignment doesn't work at all
$speaker
=
new
Speaker
();
$this
->
assertFalse
(
$speaker
->
isAttributeSafe
(
'firstName'
));
$speaker
=
new
Speaker
();
$speaker
->
setScenario
(
'test'
);
$this
->
assertTrue
(
$speaker
->
isAttributeSafe
(
'firstName'
));
}
public
function
testErrors
()
{
$speaker
=
new
Speaker
();
$this
->
assertEmpty
(
$speaker
->
getErrors
());
$this
->
assertEmpty
(
$speaker
->
getErrors
(
'firstName'
));
$this
->
assertEmpty
(
$speaker
->
getFirstErrors
());
$this
->
assertFalse
(
$speaker
->
hasErrors
());
$this
->
assertFalse
(
$speaker
->
hasErrors
(
'firstName'
));
$speaker
->
addError
(
'firstName'
,
'Something is wrong!'
);
$this
->
assertEquals
(
array
(
'firstName'
=>
array
(
'Something is wrong!'
)),
$speaker
->
getErrors
());
$this
->
assertEquals
(
array
(
'Something is wrong!'
),
$speaker
->
getErrors
(
'firstName'
));
$speaker
->
addError
(
'firstName'
,
'Totally wrong!'
);
$this
->
assertEquals
(
array
(
'firstName'
=>
array
(
'Something is wrong!'
,
'Totally wrong!'
)),
$speaker
->
getErrors
());
$this
->
assertEquals
(
array
(
'Something is wrong!'
,
'Totally wrong!'
),
$speaker
->
getErrors
(
'firstName'
));
$this
->
assertTrue
(
$speaker
->
hasErrors
());
$this
->
assertTrue
(
$speaker
->
hasErrors
(
'firstName'
));
$this
->
assertFalse
(
$speaker
->
hasErrors
(
'lastName'
));
$this
->
assertEquals
(
array
(
'Something is wrong!'
),
$speaker
->
getFirstErrors
());
$this
->
assertEquals
(
'Something is wrong!'
,
$speaker
->
getFirstError
(
'firstName'
));
$this
->
assertNull
(
$speaker
->
getFirstError
(
'lastName'
));
$speaker
->
addError
(
'lastName'
,
'Another one!'
);
$this
->
assertEquals
(
array
(
'firstName'
=>
array
(
'Something is wrong!'
,
'Totally wrong!'
,
),
'lastName'
=>
array
(
'Another one!'
),
),
$speaker
->
getErrors
());
$speaker
->
clearErrors
(
'firstName'
);
$this
->
assertEquals
(
array
(
'lastName'
=>
array
(
'Another one!'
),
),
$speaker
->
getErrors
());
$speaker
->
clearErrors
();
$this
->
assertEmpty
(
$speaker
->
getErrors
());
$this
->
assertFalse
(
$speaker
->
hasErrors
());
}
public
function
testArraySyntax
()
{
$speaker
=
new
Speaker
();
// get
$this
->
assertNull
(
$speaker
[
'firstName'
]);
// isset
$this
->
assertFalse
(
isset
(
$speaker
[
'firstName'
]));
// set
$speaker
[
'firstName'
]
=
'Qiang'
;
$this
->
assertEquals
(
'Qiang'
,
$speaker
[
'firstName'
]);
$this
->
assertTrue
(
isset
(
$speaker
[
'firstName'
]));
// iteration
$attributes
=
array
();
foreach
(
$speaker
as
$key
=>
$attribute
)
{
$attributes
[
$key
]
=
$attribute
;
}
$this
->
assertEquals
(
array
(
'firstName'
=>
'Qiang'
,
'lastName'
=>
null
,
'customLabel'
=>
null
,
'underscore_style'
=>
null
,
),
$attributes
);
// unset
unset
(
$speaker
[
'firstName'
]);
// exception isn't expected here
$this
->
assertNull
(
$speaker
[
'firstName'
]);
$this
->
assertFalse
(
isset
(
$speaker
[
'firstName'
]));
}
public
function
testDefaults
()
{
$singer
=
new
Model
();
$this
->
assertEquals
(
array
(),
$singer
->
rules
());
$this
->
assertEquals
(
array
(),
$singer
->
attributeLabels
());
}
public
function
testDefaultScenarios
()
{
$singer
=
new
Singer
();
$this
->
assertEquals
(
array
(
'default'
=>
array
(
'lastName'
,
'underscore_style'
)),
$singer
->
scenarios
());
}
public
function
testIsAttributeRequired
()
{
$singer
=
new
Singer
();
$this
->
assertFalse
(
$singer
->
isAttributeRequired
(
'firstName'
));
$this
->
assertTrue
(
$singer
->
isAttributeRequired
(
'lastName'
));
}
public
function
testCreateValidators
()
{
$this
->
setExpectedException
(
'yii\base\InvalidConfigException'
,
'Invalid validation rule: a rule must be an array specifying both attribute names and validator type.'
);
$invalid
=
new
InvalidRulesModel
();
$invalid
->
createValidators
();
}
}
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