DynamicModelTest.php 2.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\base;

use yii\base\DynamicModel;
use yiiunit\TestCase;

/**
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DynamicModelTest extends TestCase
{
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

	public function testValidateData()
	{
		$email = 'invalid';
		$name = 'long name';
		$age = '';
		$model = DynamicModel::validateData(compact('name', 'email', 'age'), [
			[['email', 'name', 'age'], 'required'],
			['email', 'email'],
			['name', 'string', 'max' => 3],
		]);
		$this->assertTrue($model->hasErrors());
		$this->assertTrue($model->hasErrors('email'));
		$this->assertTrue($model->hasErrors('name'));
		$this->assertTrue($model->hasErrors('age'));
	}

Qiang Xue committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	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'));
	}

70 71 72 73 74 75 76 77 78 79 80
	public function testDynamicProperty()
	{
		$email = 'invalid';
		$name = 'long name';
		$model = new DynamicModel(compact('name', 'email'));
		$this->assertEquals($email, $model->email);
		$this->assertEquals($name, $model->name);
		$this->setExpectedException('yii\base\UnknownPropertyException');
		$age = $model->age;
	}
}