mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 22:09:48 +08:00
added more tests.
This commit is contained in:
@@ -62,7 +62,7 @@ class DynamicModel extends Model
|
||||
* @param array $attributes the dynamic attributes (name-value pairs, or names) being defined
|
||||
* @param array $config the configuration array to be applied to this object.
|
||||
*/
|
||||
public function __construct(array $attributes, $config = [])
|
||||
public function __construct(array $attributes = [], $config = [])
|
||||
{
|
||||
foreach ($attributes as $name => $value) {
|
||||
if (is_integer($name)) {
|
||||
|
||||
@@ -39,6 +39,34 @@ class DynamicModelTest extends TestCase
|
||||
$this->assertTrue($model->hasErrors('age'));
|
||||
}
|
||||
|
||||
public function testAddRule()
|
||||
{
|
||||
$model = new DynamicModel();
|
||||
$this->assertEquals(0, $model->getValidators()->count());
|
||||
$model->addRule('name', 'string', ['min' => 12]);
|
||||
$this->assertEquals(1, $model->getValidators()->count());
|
||||
$model->addRule('email', 'email');
|
||||
$this->assertEquals(2, $model->getValidators()->count());
|
||||
$model->addRule(['name', 'email'], 'required');
|
||||
$this->assertEquals(3, $model->getValidators()->count());
|
||||
}
|
||||
|
||||
public function testValidateWithAddRule()
|
||||
{
|
||||
$email = 'invalid';
|
||||
$name = 'long name';
|
||||
$age = '';
|
||||
$model = new DynamicModel(compact('name', 'email', 'age'));
|
||||
$model->addRule(['email', 'name', 'age'], 'required')
|
||||
->addRule('email', 'email')
|
||||
->addRule('name', 'string', ['max' => 3])
|
||||
->validate();
|
||||
$this->assertTrue($model->hasErrors());
|
||||
$this->assertTrue($model->hasErrors('email'));
|
||||
$this->assertTrue($model->hasErrors('name'));
|
||||
$this->assertTrue($model->hasErrors('age'));
|
||||
}
|
||||
|
||||
public function testDynamicProperty()
|
||||
{
|
||||
$email = 'invalid';
|
||||
|
||||
Reference in New Issue
Block a user