ExistValidatorTest.php 3.33 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
use yiiunit\framework\db\DatabaseTestCase;

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

	public function setUp()
	{
		parent::setUp();
21
		ActiveRecord::$db = $this->getConnection();
22 23 24 25 26 27 28 29 30 31 32 33 34 35
	}

	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 {
Alexander Makarov committed
36
			$val = new ExistValidator(['className' => ValidatorTestMainModel::className()]);
37 38 39 40 41 42 43 44 45 46
			$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()
	{
Alexander Makarov committed
47
		$val = new ExistValidator(['className' => ValidatorTestRefModel::className(), 'attributeName' => 'id']);
48 49 50
		$this->assertTrue($val->validateValue(2));
		$this->assertTrue($val->validateValue(5));
		$this->assertFalse($val->validateValue(99));
Alexander Makarov committed
51
		$this->assertFalse($val->validateValue(['1']));
52 53 54 55 56
	}

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