BooleanValidatorTest.php 1.63 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 18
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

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

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