BooleanValidatorTest.php 1.7 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 22 23 24 25
	public function testValidateValue()
	{
		$val = new BooleanValidator;
		$this->assertTrue($val->validateValue(true));
		$this->assertTrue($val->validateValue(false));
		$this->assertTrue($val->validateValue('0'));
		$this->assertTrue($val->validateValue('1'));
Suralc committed
26
		$this->assertFalse($val->validateValue(null));
Alexander Makarov committed
27
		$this->assertFalse($val->validateValue([]));
Suralc committed
28 29 30 31 32 33 34 35
		$val->strict = true;
		$this->assertTrue($val->validateValue('0'));
		$this->assertTrue($val->validateValue('1'));
		$this->assertFalse($val->validateValue(true));
		$this->assertFalse($val->validateValue(false));
		$val->trueValue = true;
		$val->falseValue = false;
		$this->assertFalse($val->validateValue('0'));
Alexander Makarov committed
36
		$this->assertFalse($val->validateValue([]));
Suralc committed
37 38 39
		$this->assertTrue($val->validateValue(true));
		$this->assertTrue($val->validateValue(false));
	}
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
	}
}