ExistValidatorTest.php 3.51 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace yiiunit\framework\validators;


use Yii;
use yii\base\Exception;
use yii\validators\ExistValidator;
use yiiunit\data\ar\ActiveRecord;
10 11
use yiiunit\data\validators\models\ValidatorTestMainModel;
use yiiunit\data\validators\models\ValidatorTestRefModel;
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
use yiiunit\framework\db\DatabaseTestCase;

class ExistValidatorTest extends DatabaseTestCase
{
	protected $initializeAppWithDb = true;
	protected $driverName = 'mysql';

	public function setUp()
	{
		parent::setUp();
		ActiveRecord::$db = Yii::$app->getComponent('db');
	}

	public function tearDown()
	{
		parent::tearDown();
	}

	public function testValidateValueExpectedException()
	{
		try {
			$val = new ExistValidator();
			$result = $val->validateValue('ref');
			$this->fail('Exception should have been thrown at this time');
		} catch (Exception $e) {
			$this->assertInstanceOf('yii\base\InvalidConfigException', $e);
			$this->assertEquals('The "className" property must be set.', $e->getMessage());
		}
		// combine to save the time creating a new db-fixture set (likely ~5 sec)
		try {
42
			$val = new ExistValidator(array('className' => ValidatorTestMainModel::className()));
43 44 45 46 47 48 49 50 51 52
			$val->validateValue('ref');
			$this->fail('Exception should have been thrown at this time');
		} catch (Exception $e) {
			$this->assertInstanceOf('yii\base\InvalidConfigException', $e);
			$this->assertEquals('The "attributeName" property must be set.', $e->getMessage());
		}
	}

	public function testValidateValue()
	{
53
		$val = new ExistValidator(array('className' => ValidatorTestRefModel::className(), 'attributeName' => 'id'));
54 55 56 57 58 59 60 61 62
		$this->assertTrue($val->validateValue(2));
		$this->assertTrue($val->validateValue(5));
		$this->assertFalse($val->validateValue(99));
		$this->assertFalse($val->validateValue(array('1')));
	}

	public function testValidateAttribute()
	{
		// existing value on different table
63 64
		$val = new ExistValidator(array('className' => ValidatorTestMainModel::className(), 'attributeName' => 'id'));
		$m = ValidatorTestRefModel::find(array('id' => 1));
65 66 67
		$val->validateAttribute($m, 'ref');
		$this->assertFalse($m->hasErrors());
		// non-existing value on different table
68 69
		$val = new ExistValidator(array('className' => ValidatorTestMainModel::className(), 'attributeName' => 'id'));
		$m = ValidatorTestRefModel::find(array('id' => 6));
70 71 72 73
		$val->validateAttribute($m, 'ref');
		$this->assertTrue($m->hasErrors('ref'));
		// existing value on same table
		$val = new ExistValidator(array('attributeName' => 'ref'));
74
		$m = ValidatorTestRefModel::find(array('id' => 2));
75 76 77 78
		$val->validateAttribute($m, 'test_val');
		$this->assertFalse($m->hasErrors());
		// non-existing value on same table
		$val = new ExistValidator(array('attributeName' => 'ref'));
79
		$m = ValidatorTestRefModel::find(array('id' => 5));
80 81 82 83
		$val->validateAttribute($m, 'test_val_fail');
		$this->assertTrue($m->hasErrors('test_val_fail'));
		// check for given value (true)
		$val = new ExistValidator();
84
		$m = ValidatorTestRefModel::find(array('id' => 3));
85 86 87 88
		$val->validateAttribute($m, 'ref');
		$this->assertFalse($m->hasErrors());
		// check for given defaults (false)
		$val = new ExistValidator();
89
		$m = ValidatorTestRefModel::find(array('id' => 4));
90 91 92
		$m->a_field = 'some new value';
		$val->validateAttribute($m, 'a_field');
		$this->assertTrue($m->hasErrors('a_field'));
93 94
		// check array
		$val = new ExistValidator(array('attributeName' => 'ref'));
95
		$m = ValidatorTestRefModel::find(array('id' => 2));
96 97 98
		$m->test_val = array(1,2,3);
		$val->validateAttribute($m, 'test_val');
		$this->assertTrue($m->hasErrors('test_val'));
99 100
	}
}