From c829dd6b470e68b22268b68bd32cbc4b9e255bd3 Mon Sep 17 00:00:00 2001 From: Qiang Xue <qiang.xue@gmail.com> Date: Mon, 27 Jan 2014 17:24:48 -0500 Subject: [PATCH] added more tests. --- framework/base/DynamicModel.php | 2 +- tests/unit/framework/base/DynamicModelTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/framework/base/DynamicModel.php b/framework/base/DynamicModel.php index e4c409e..b93648d 100644 --- a/framework/base/DynamicModel.php +++ b/framework/base/DynamicModel.php @@ -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)) { diff --git a/tests/unit/framework/base/DynamicModelTest.php b/tests/unit/framework/base/DynamicModelTest.php index f070e29..5468421 100644 --- a/tests/unit/framework/base/DynamicModelTest.php +++ b/tests/unit/framework/base/DynamicModelTest.php @@ -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'; -- libgit2 0.27.1