NumberValidatorTest.php 6.09 KB
Newer Older
Suralc committed
1 2 3 4 5 6
<?php

namespace yiiunit\framework\validators;


use yii\validators\NumberValidator;
7
use yiiunit\data\validators\models\FakedValidationModel;
Suralc committed
8 9 10 11
use yiiunit\TestCase;

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

Suralc committed
18 19 20 21 22
	public function testEnsureMessageOnInit()
	{
		$val = new NumberValidator;
		$this->assertTrue(is_string($val->message));
		$this->assertTrue(is_null($val->max));
Alexander Makarov committed
23
		$val = new NumberValidator(['min' => -1, 'max' => 20, 'integerOnly' => true]);
Suralc committed
24 25 26 27 28 29 30 31
		$this->assertTrue(is_string($val->message));
		$this->assertTrue(is_string($val->tooSmall));
		$this->assertTrue(is_string($val->tooBig));
	}

	public function testValidateValueSimple()
	{
		$val = new NumberValidator();
Qiang Xue committed
32 33 34 35 36 37 38
		$this->assertTrue($val->validate(20));
		$this->assertTrue($val->validate(0));
		$this->assertTrue($val->validate(-20));
		$this->assertTrue($val->validate('20'));
		$this->assertTrue($val->validate(25.45));
		$this->assertFalse($val->validate('25,45'));
		$this->assertFalse($val->validate('12:45'));
Alexander Makarov committed
39
		$val = new NumberValidator(['integerOnly' => true]);
Qiang Xue committed
40 41 42 43 44 45 46 47
		$this->assertTrue($val->validate(20));
		$this->assertTrue($val->validate(0));
		$this->assertFalse($val->validate(25.45));
		$this->assertTrue($val->validate('20'));
		$this->assertFalse($val->validate('25,45'));
		$this->assertTrue($val->validate('020'));
		$this->assertTrue($val->validate(0x14));
		$this->assertFalse($val->validate('0x14')); // todo check this
Suralc committed
48 49 50 51 52
	}

	public function testValidateValueAdvanced()
	{
		$val = new NumberValidator();
Qiang Xue committed
53 54 55 56 57 58 59
		$this->assertTrue($val->validate('-1.23')); // signed float
		$this->assertTrue($val->validate('-4.423e-12')); // signed float + exponent
		$this->assertTrue($val->validate('12E3')); // integer + exponent
		$this->assertFalse($val->validate('e12')); // just exponent
		$this->assertFalse($val->validate('-e3'));
		$this->assertFalse($val->validate('-4.534-e-12')); // 'signed' exponent
		$this->assertFalse($val->validate('12.23^4')); // expression instead of value
Alexander Makarov committed
60
		$val = new NumberValidator(['integerOnly' => true]);
Qiang Xue committed
61 62 63 64 65 66 67
		$this->assertFalse($val->validate('-1.23'));
		$this->assertFalse($val->validate('-4.423e-12'));
		$this->assertFalse($val->validate('12E3'));
		$this->assertFalse($val->validate('e12'));
		$this->assertFalse($val->validate('-e3'));
		$this->assertFalse($val->validate('-4.534-e-12'));
		$this->assertFalse($val->validate('12.23^4'));
Suralc committed
68 69 70 71
	}

	public function testValidateValueMin()
	{
Alexander Makarov committed
72
		$val = new NumberValidator(['min' => 1]);
Qiang Xue committed
73 74 75 76
		$this->assertTrue($val->validate(1));
		$this->assertFalse($val->validate(-1));
		$this->assertFalse($val->validate('22e-12'));
		$this->assertTrue($val->validate(PHP_INT_MAX + 1));
Alexander Makarov committed
77
		$val = new NumberValidator(['min' => 1], ['integerOnly' => true]);
Qiang Xue committed
78 79 80 81
		$this->assertTrue($val->validate(1));
		$this->assertFalse($val->validate(-1));
		$this->assertFalse($val->validate('22e-12'));
		$this->assertTrue($val->validate(PHP_INT_MAX + 1));
Suralc committed
82 83 84 85
	}

	public function testValidateValueMax()
	{
Alexander Makarov committed
86
		$val = new NumberValidator(['max' => 1.25]);
Qiang Xue committed
87 88 89 90
		$this->assertTrue($val->validate(1));
		$this->assertFalse($val->validate(1.5));
		$this->assertTrue($val->validate('22e-12'));
		$this->assertTrue($val->validate('125e-2'));
Alexander Makarov committed
91
		$val = new NumberValidator(['max' => 1.25, 'integerOnly' => true]);
Qiang Xue committed
92 93 94 95
		$this->assertTrue($val->validate(1));
		$this->assertFalse($val->validate(1.5));
		$this->assertFalse($val->validate('22e-12'));
		$this->assertFalse($val->validate('125e-2'));
Suralc committed
96 97 98 99
	}

	public function testValidateValueRange()
	{
Alexander Makarov committed
100
		$val = new NumberValidator(['min' => -10, 'max' => 20]);
Qiang Xue committed
101 102 103 104
		$this->assertTrue($val->validate(0));
		$this->assertTrue($val->validate(-10));
		$this->assertFalse($val->validate(-11));
		$this->assertFalse($val->validate(21));
Alexander Makarov committed
105
		$val = new NumberValidator(['min' => -10, 'max' => 20, 'integerOnly' => true]);
Qiang Xue committed
106 107 108 109
		$this->assertTrue($val->validate(0));
		$this->assertFalse($val->validate(-11));
		$this->assertFalse($val->validate(22));
		$this->assertFalse($val->validate('20e-1'));
Suralc committed
110 111 112 113 114 115 116 117 118 119 120 121
	}

	public function testValidateAttribute()
	{
		$val = new NumberValidator();
		$model = new FakedValidationModel();
		$model->attr_number = '5.5e1';
		$val->validateAttribute($model, 'attr_number');
		$this->assertFalse($model->hasErrors('attr_number'));
		$model->attr_number = '43^32'; //expression
		$val->validateAttribute($model, 'attr_number');
		$this->assertTrue($model->hasErrors('attr_number'));
Alexander Makarov committed
122
		$val = new NumberValidator(['min' => 10]);
Suralc committed
123 124 125 126 127 128 129
		$model = new FakedValidationModel();
		$model->attr_number = 10;
		$val->validateAttribute($model, 'attr_number');
		$this->assertFalse($model->hasErrors('attr_number'));
		$model->attr_number = 5;
		$val->validateAttribute($model, 'attr_number');
		$this->assertTrue($model->hasErrors('attr_number'));
Alexander Makarov committed
130
		$val = new NumberValidator(['max' => 10]);
Suralc committed
131 132 133 134 135 136 137
		$model = new FakedValidationModel();
		$model->attr_number = 10;
		$val->validateAttribute($model, 'attr_number');
		$this->assertFalse($model->hasErrors('attr_number'));
		$model->attr_number = 15;
		$val->validateAttribute($model, 'attr_number');
		$this->assertTrue($model->hasErrors('attr_number'));
Alexander Makarov committed
138
		$val = new NumberValidator(['max' => 10, 'integerOnly' => true]);
Suralc committed
139 140 141 142 143 144 145
		$model = new FakedValidationModel();
		$model->attr_number = 10;
		$val->validateAttribute($model, 'attr_number');
		$this->assertFalse($model->hasErrors('attr_number'));
		$model->attr_number = 3.43;
		$val->validateAttribute($model, 'attr_number');
		$this->assertTrue($model->hasErrors('attr_number'));
Alexander Makarov committed
146 147
		$val = new NumberValidator(['min' => 1]);
		$model = FakedValidationModel::createWithAttributes(['attr_num' => [1,2,3]]);
148 149
		$val->validateAttribute($model, 'attr_num');
		$this->assertTrue($model->hasErrors('attr_num'));
Suralc committed
150 151 152 153
	}

	public function testEnsureCustomMessageIsSetOnValidateAttribute()
	{
Alexander Makarov committed
154
		$val = new NumberValidator([
Suralc committed
155
			'tooSmall' => '{attribute} is to small.',
Suralc committed
156
			'min' => 5
Alexander Makarov committed
157
		]);
Suralc committed
158 159 160 161 162 163 164 165
		$model = new FakedValidationModel();
		$model->attr_number = 0;
		$val->validateAttribute($model, 'attr_number');
		$this->assertTrue($model->hasErrors('attr_number'));
		$this->assertEquals(1, count($model->getErrors('attr_number')));
		$msgs = $model->getErrors('attr_number');
		$this->assertSame('attr_number is to small.', $msgs[0]);
	}
Qiang Xue committed
166
}