CompareValidatorTest.php 4.94 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
use yiiunit\TestCase;

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

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

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

	protected function getOperationTestData($value)
	{
Alexander Makarov committed
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		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],
			//],
		];
89 90 91 92 93 94 95
	}

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

	public function testValidateAttributeOperators()
	{
		$value = 55;
		foreach ($this->getOperationTestData($value) as $operator => $tests) {
Alexander Makarov committed
145
			$val = new CompareValidator(['operator' => $operator, 'compareValue' => $value]);
146 147 148 149 150 151 152 153 154 155 156 157 158
			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
159
			$val = new CompareValidator(['operator' => $operator]);
160 161 162
			$this->assertTrue(strlen($val->message) > 1);
		}
		try {
163
			new CompareValidator(['operator' => '<>']);
164 165 166 167 168 169 170 171 172
		} 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
173
}