BooleanValidatorTest.php 1.86 KB
Newer Older
Suralc committed
1 2
<?php
namespace yiiunit\framework\validators;
3

4
use yiiunit\data\validators\models\FakedValidationModel;
Suralc committed
5 6 7 8 9 10 11 12
use yii\validators\BooleanValidator;
use yiiunit\TestCase;

/**
 * BooleanValidatorTest
 */
class BooleanValidatorTest extends TestCase
{
13 14 15 16 17
    protected function setUp()
    {
        parent::setUp();
        $this->mockApplication();
    }
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    public function testValidateValue()
    {
        $val = new BooleanValidator;
        $this->assertTrue($val->validate(true));
        $this->assertTrue($val->validate(false));
        $this->assertTrue($val->validate('0'));
        $this->assertTrue($val->validate('1'));
        $this->assertFalse($val->validate(null));
        $this->assertFalse($val->validate([]));
        $val->strict = true;
        $this->assertTrue($val->validate('0'));
        $this->assertTrue($val->validate('1'));
        $this->assertFalse($val->validate(true));
        $this->assertFalse($val->validate(false));
        $val->trueValue = true;
        $val->falseValue = false;
        $this->assertFalse($val->validate('0'));
        $this->assertFalse($val->validate([]));
        $this->assertTrue($val->validate(true));
        $this->assertTrue($val->validate(false));
    }
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    public function testValidateAttributeAndError()
    {
        $obj = new FakedValidationModel;
        $obj->attrA = true;
        $obj->attrB = '1';
        $obj->attrC = '0';
        $obj->attrD = [];
        $val = new BooleanValidator;
        $val->validateAttribute($obj, 'attrA');
        $this->assertFalse($obj->hasErrors('attrA'));
        $val->validateAttribute($obj, 'attrC');
        $this->assertFalse($obj->hasErrors('attrC'));
        $val->strict = true;
        $val->validateAttribute($obj, 'attrB');
        $this->assertFalse($obj->hasErrors('attrB'));
        $val->validateAttribute($obj, 'attrD');
        $this->assertTrue($obj->hasErrors('attrD'));
    }
Suralc committed
59
}