RangeValidatorTest.php 3.3 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

Alexander Kochetov committed
44 45 46 47 48 49 50 51 52 53 54
    public function testValidateArrayValue()
    {
        $val = new RangeValidator(['range' => range(1, 10, 1)]);
        $val->allowArray = true;
        $this->assertTrue($val->validate([1, 2, 3, 4, 5]));
        $this->assertTrue($val->validate([6, 7, 8, 9, 10]));
        $this->assertFalse($val->validate([0, 1, 2]));
        $this->assertFalse($val->validate([10, 11, 12]));
        $this->assertTrue($val->validate(["1", "2", "3", 4, 5, 6]));
    }

55 56 57 58 59 60 61 62 63 64
    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
65

Alexander Kochetov committed
66 67 68 69 70 71 72 73
    public function testValidateArrayValueStrict()
    {
        $val = new RangeValidator(['range' => range(1, 10, 1), 'strict' => true]);
        $val->allowArray = true;
        $this->assertFalse($val->validate(["1", "2", "3", "4", "5", "6"]));
        $this->assertFalse($val->validate(["1", "2", "3", 4, 5, 6]));
    }

74 75 76 77 78 79 80 81 82 83 84
    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
85

86 87 88 89 90 91 92 93 94 95 96
    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
97
}