CompareValidatorTest.php 4.95 KB
Newer Older
1 2 3 4 5
<?php
namespace yiiunit\framework\validators;

use yii\base\InvalidConfigException;
use yii\validators\CompareValidator;
6
use yiiunit\data\validators\models\FakedValidationModel;
7 8 9 10 11 12
use yiiunit\TestCase;



class CompareValidatorTest extends TestCase
{
13 14 15 16 17
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}
18 19 20 21 22

	public function testValidateValueException()
	{
		$this->setExpectedException('yii\base\InvalidConfigException');
		$val = new CompareValidator;
Qiang Xue committed
23
		$val->validate('val');
24 25 26 27 28 29
	}

	public function testValidateValue()
	{
		$value = 18449;
		// default config
Alexander Makarov committed
30
		$val = new CompareValidator(['compareValue' => $value]);
Qiang Xue committed
31 32 33
		$this->assertTrue($val->validate($value));
		$this->assertTrue($val->validate((string)$value));
		$this->assertFalse($val->validate($value + 1));
34
		foreach ($this->getOperationTestData($value) as $op => $tests) {
Alexander Makarov committed
35
			$val = new CompareValidator(['compareValue' => $value]);
36 37
			$val->operator = $op;
			foreach ($tests as $test) {
Qiang Xue committed
38
				$this->assertEquals($test[1], $val->validate($test[0]));
39 40 41 42 43 44
			}
		}
	}

	protected function getOperationTestData($value)
	{
Alexander Makarov committed
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		return [
			'===' => [
				[$value, true],
				[(string)$value, false],
				[(float)$value, false],
				[$value + 1, false],
			],
			'!=' => [
				[$value, false],
				[(string)$value, false],
				[(float)$value, false],
				[$value + 0.00001, true],
				[false, true],
			],
			'!==' => [
				[$value, false],
				[(string)$value, true],
				[(float)$value, true],
				[false, true],
			],
			'>' => [
				[$value, false],
				[$value + 1, true],
				[$value - 1, false],
			],
			'>=' => [
				[$value, true],
				[$value + 1, true],
				[$value - 1, false],
			],
			'<' => [
				[$value, false],
				[$value + 1, false],
				[$value - 1, true],
			],
			'<=' => [
				[$value, true],
				[$value + 1, false],
				[$value - 1, true],
			],
			//'non-op' => [
			//	[$value, false],
			//	[$value + 1, false],
			//	[$value - 1, false],
			//],
		];
91 92 93 94 95 96 97
	}

	public function testValidateAttribute()
	{
		// invalid-array
		$val = new CompareValidator;
		$model = new FakedValidationModel;
Alexander Makarov committed
98
		$model->attr = ['test_val'];
99 100
		$val->validateAttribute($model, 'attr');
		$this->assertTrue($model->hasErrors('attr'));
Alexander Makarov committed
101
		$val = new CompareValidator(['compareValue' => 'test-string']);
102 103 104 105
		$model = new FakedValidationModel;
		$model->attr_test = 'test-string';
		$val->validateAttribute($model, 'attr_test');
		$this->assertFalse($model->hasErrors('attr_test'));
Alexander Makarov committed
106
		$val = new CompareValidator(['compareAttribute' => 'attr_test_val']);
107 108 109 110 111 112
		$model = new FakedValidationModel;
		$model->attr_test = 'test-string';
		$model->attr_test_val = 'test-string';
		$val->validateAttribute($model, 'attr_test');
		$this->assertFalse($model->hasErrors('attr_test'));
		$this->assertFalse($model->hasErrors('attr_test_val'));
Alexander Makarov committed
113
		$val = new CompareValidator(['compareAttribute' => 'attr_test_val']);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		$model = new FakedValidationModel;
		$model->attr_test = 'test-string';
		$model->attr_test_val = 'test-string-false';
		$val->validateAttribute($model, 'attr_test');
		$this->assertTrue($model->hasErrors('attr_test'));
		$this->assertFalse($model->hasErrors('attr_test_val'));
		// assume: _repeat
		$val = new CompareValidator;
		$model = new FakedValidationModel;
		$model->attr_test = 'test-string';
		$model->attr_test_repeat = 'test-string';
		$val->validateAttribute($model, 'attr_test');
		$this->assertFalse($model->hasErrors('attr_test'));
		$this->assertFalse($model->hasErrors('attr_test_repeat'));
		$val = new CompareValidator;
		$model = new FakedValidationModel;
		$model->attr_test = 'test-string';
		$model->attr_test_repeat = 'test-string2';
		$val->validateAttribute($model, 'attr_test');
		$this->assertTrue($model->hasErrors('attr_test'));
		$this->assertFalse($model->hasErrors('attr_test_repeat'));
Suralc committed
135 136 137
		// not existing op
		$val = new CompareValidator();
		$val->operator = '<>';
Alexander Makarov committed
138
		$model = FakedValidationModel::createWithAttributes(['attr_o' => 5, 'attr_o_repeat' => 5]);
Suralc committed
139 140
		$val->validateAttribute($model, 'attr_o');
		$this->assertTrue($model->hasErrors('attr_o'));
141 142 143 144 145 146
	}

	public function testValidateAttributeOperators()
	{
		$value = 55;
		foreach ($this->getOperationTestData($value) as $operator => $tests) {
Alexander Makarov committed
147
			$val = new CompareValidator(['operator' => $operator, 'compareValue' => $value]);
148 149 150 151 152 153 154 155 156 157 158 159 160
			foreach ($tests as $test) {
				$model = new FakedValidationModel;
				$model->attr_test = $test[0];
				$val->validateAttribute($model, 'attr_test');
				$this->assertEquals($test[1], !$model->hasErrors('attr_test'));
			}

		}
	}

	public function testEnsureMessageSetOnInit()
	{
		foreach ($this->getOperationTestData(1337) as $operator => $tests) {
Alexander Makarov committed
161
			$val = new CompareValidator(['operator' => $operator]);
162 163 164
			$this->assertTrue(strlen($val->message) > 1);
		}
		try {
Alexander Makarov committed
165
			$val = new CompareValidator(['operator' => '<>']);
166 167 168 169 170 171 172 173 174
		} catch (InvalidConfigException $e) {
			return;
		}
		catch (\Exception $e) {
			$this->fail('InvalidConfigException expected' . get_class($e) . 'received');
			return;
		}
		$this->fail('InvalidConfigException expected none received');
	}
Qiang Xue committed
175
}