ExistValidatorTest.php 3.36 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
		$this->mockApplication();
22
		ActiveRecord::$db = $this->getConnection();
23 24 25 26 27 28 29 30 31 32 33 34 35 36
	}

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

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