RangeValidatorTest.php 2.54 KB
Newer Older
Suralc committed
1 2 3 4 5 6 7 8
<?php

namespace yiiunit\framework\validators;

use yii\validators\RangeValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;

9 10 11
/**
 * @group validators
 */
Suralc committed
12 13
class RangeValidatorTest extends TestCase
{
14 15 16 17 18
    protected function setUp()
    {
        parent::setUp();
        $this->mockApplication();
    }
19

20 21 22 23 24
    public function testInitException()
    {
        $this->setExpectedException('yii\base\InvalidConfigException', 'The "range" property must be set.');
        new RangeValidator(['range' => 'not an array']);
    }
Suralc committed
25

26 27 28 29 30
    public function testAssureMessageSetOnInit()
    {
        $val = new RangeValidator(['range' => []]);
        $this->assertTrue(is_string($val->message));
    }
Suralc committed
31

32 33 34 35 36 37 38 39 40 41 42
    public function testValidateValue()
    {
        $val = new RangeValidator(['range' => range(1, 10, 1)]);
        $this->assertTrue($val->validate(1));
        $this->assertFalse($val->validate(0));
        $this->assertFalse($val->validate(11));
        $this->assertFalse($val->validate(5.5));
        $this->assertTrue($val->validate(10));
        $this->assertTrue($val->validate("10"));
        $this->assertTrue($val->validate("5"));
    }
Suralc committed
43

44 45 46 47 48 49 50 51 52 53
    public function testValidateValueStrict()
    {
        $val = new RangeValidator(['range' => range(1, 10, 1), 'strict' => true]);
        $this->assertTrue($val->validate(1));
        $this->assertTrue($val->validate(5));
        $this->assertTrue($val->validate(10));
        $this->assertFalse($val->validate("1"));
        $this->assertFalse($val->validate("10"));
        $this->assertFalse($val->validate("5.5"));
    }
Suralc committed
54

55 56 57 58 59 60 61 62 63 64 65
    public function testValidateValueNot()
    {
        $val = new RangeValidator(['range' => range(1, 10, 1), 'not' => true]);
        $this->assertFalse($val->validate(1));
        $this->assertTrue($val->validate(0));
        $this->assertTrue($val->validate(11));
        $this->assertTrue($val->validate(5.5));
        $this->assertFalse($val->validate(10));
        $this->assertFalse($val->validate("10"));
        $this->assertFalse($val->validate("5"));
    }
Suralc committed
66

67 68 69 70 71 72 73 74 75 76 77
    public function testValidateAttribute()
    {
        $val = new RangeValidator(['range' => range(1, 10, 1)]);
        $m = FakedValidationModel::createWithAttributes(['attr_r1' => 5, 'attr_r2' => 999]);
        $val->validateAttribute($m, 'attr_r1');
        $this->assertFalse($m->hasErrors());
        $val->validateAttribute($m, 'attr_r2');
        $this->assertTrue($m->hasErrors('attr_r2'));
        $err = $m->getErrors('attr_r2');
        $this->assertTrue(stripos($err[0], 'attr_r2') !== false);
    }
Qiang Xue committed
78
}