You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.2 KiB
62 lines
2.2 KiB
11 years ago
|
<?php
|
||
|
namespace yiiunit\framework\validators;
|
||
|
|
||
|
use yii\validators\RequiredValidator;
|
||
11 years ago
|
use yiiunit\data\validators\models\FakedValidationModel;
|
||
11 years ago
|
use yiiunit\TestCase;
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @group validators
|
||
|
*/
|
||
11 years ago
|
class RequiredValidatorTest extends TestCase
|
||
|
{
|
||
11 years ago
|
protected function setUp()
|
||
|
{
|
||
|
parent::setUp();
|
||
|
$this->mockApplication();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function testValidateValueWithDefaults()
|
||
|
{
|
||
|
$val = new RequiredValidator();
|
||
|
$this->assertFalse($val->validate(null));
|
||
|
$this->assertFalse($val->validate([]));
|
||
|
$this->assertTrue($val->validate('not empty'));
|
||
|
$this->assertTrue($val->validate(['with', 'elements']));
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function testValidateValueWithValue()
|
||
|
{
|
||
|
$val = new RequiredValidator(['requiredValue' => 55]);
|
||
|
$this->assertTrue($val->validate(55));
|
||
|
$this->assertTrue($val->validate("55"));
|
||
|
$this->assertFalse($val->validate("should fail"));
|
||
|
$this->assertTrue($val->validate(true));
|
||
|
$val->strict = true;
|
||
|
$this->assertTrue($val->validate(55));
|
||
|
$this->assertFalse($val->validate("55"));
|
||
|
$this->assertFalse($val->validate("0x37"));
|
||
|
$this->assertFalse($val->validate("should fail"));
|
||
|
$this->assertFalse($val->validate(true));
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function testValidateAttribute()
|
||
|
{
|
||
|
// empty req-value
|
||
|
$val = new RequiredValidator();
|
||
|
$m = FakedValidationModel::createWithAttributes(['attr_val' => null]);
|
||
|
$val->validateAttribute($m, 'attr_val');
|
||
|
$this->assertTrue($m->hasErrors('attr_val'));
|
||
|
$this->assertTrue(stripos(current($m->getErrors('attr_val')), 'blank') !== false);
|
||
|
$val = new RequiredValidator(['requiredValue' => 55]);
|
||
|
$m = FakedValidationModel::createWithAttributes(['attr_val' => 56]);
|
||
|
$val->validateAttribute($m, 'attr_val');
|
||
|
$this->assertTrue($m->hasErrors('attr_val'));
|
||
|
$this->assertTrue(stripos(current($m->getErrors('attr_val')), 'must be') !== false);
|
||
|
$val = new RequiredValidator(['requiredValue' => 55]);
|
||
|
$m = FakedValidationModel::createWithAttributes(['attr_val' => 55]);
|
||
|
$val->validateAttribute($m, 'attr_val');
|
||
|
$this->assertFalse($m->hasErrors('attr_val'));
|
||
|
}
|
||
11 years ago
|
}
|