StringValidatorTest.php 4.25 KB
Newer Older
Suralc committed
1 2 3 4 5 6
<?php

namespace yiiunit\framework\validators;


use yii\validators\StringValidator;
7
use yiiunit\data\validators\models\FakedValidationModel;
Suralc committed
8 9 10 11 12 13 14 15 16 17 18 19 20
use yiiunit\TestCase;

class StringValidatorTest extends TestCase
{
	public function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

	public function testValidateValue()
	{
		$val = new StringValidator();
Alexander Makarov committed
21
		$this->assertFalse($val->validateValue(['not a string']));
Suralc committed
22 23 24 25 26
		$this->assertTrue($val->validateValue('Just some string'));
	}

	public function testValidateValueLength()
	{
Alexander Makarov committed
27
		$val = new StringValidator(['length' => 25]);
Suralc committed
28 29 30 31
		$this->assertTrue($val->validateValue(str_repeat('x', 25)));
		$this->assertTrue($val->validateValue(str_repeat('€', 25)));
		$this->assertFalse($val->validateValue(str_repeat('x', 125)));
		$this->assertFalse($val->validateValue(''));
Alexander Makarov committed
32
		$val = new StringValidator(['length' => [25]]);
Suralc committed
33 34 35 36
		$this->assertTrue($val->validateValue(str_repeat('x', 25)));
		$this->assertTrue($val->validateValue(str_repeat('x', 1250)));
		$this->assertFalse($val->validateValue(str_repeat('Ä', 24)));
		$this->assertFalse($val->validateValue(''));
Alexander Makarov committed
37
		$val = new StringValidator(['length' => [10, 20]]);
Suralc committed
38 39 40 41 42 43 44
		$this->assertTrue($val->validateValue(str_repeat('x', 15)));
		$this->assertTrue($val->validateValue(str_repeat('x', 10)));
		$this->assertTrue($val->validateValue(str_repeat('x', 20)));
		$this->assertFalse($val->validateValue(str_repeat('x', 5)));
		$this->assertFalse($val->validateValue(str_repeat('x', 25)));
		$this->assertFalse($val->validateValue(''));
		// make sure min/max are overridden
Alexander Makarov committed
45
		$val = new StringValidator(['length' => [10, 20], 'min' => 25, 'max' => 35]);
Suralc committed
46 47 48 49 50 51
		$this->assertTrue($val->validateValue(str_repeat('x', 15)));
		$this->assertFalse($val->validateValue(str_repeat('x', 30)));
	}

	public function testValidateValueMinMax()
	{
Alexander Makarov committed
52
		$val = new StringValidator(['min' => 10]);
Suralc committed
53 54
		$this->assertTrue($val->validateValue(str_repeat('x', 10)));
		$this->assertFalse($val->validateValue('xxxx'));
Alexander Makarov committed
55
		$val = new StringValidator(['max' => 10]);
Suralc committed
56 57
		$this->assertTrue($val->validateValue('xxxx'));
		$this->assertFalse($val->validateValue(str_repeat('y', 20)));
Alexander Makarov committed
58
		$val = new StringValidator(['min' => 10, 'max' => 20]);
Suralc committed
59 60 61 62 63 64 65 66 67 68 69 70
		$this->assertTrue($val->validateValue(str_repeat('y', 15)));
		$this->assertFalse($val->validateValue('abc'));
		$this->assertFalse($val->validateValue(str_repeat('b', 25)));
	}

	public function testValidateAttribute()
	{
		$val = new StringValidator();
		$model = new FakedValidationModel();
		$model->attr_string = 'a tet string';
		$val->validateAttribute($model, 'attr_string');
		$this->assertFalse($model->hasErrors());
Alexander Makarov committed
71
		$val = new StringValidator(['length' => 20]);
Suralc committed
72 73 74 75 76 77 78 79
		$model = new FakedValidationModel();
		$model->attr_string = str_repeat('x', 20);
		$val->validateAttribute($model, 'attr_string');
		$this->assertFalse($model->hasErrors());
		$model = new FakedValidationModel();
		$model->attr_string = 'abc';
		$val->validateAttribute($model, 'attr_string');
		$this->assertTrue($model->hasErrors('attr_string'));
Alexander Makarov committed
80
		$val = new StringValidator(['max' => 2]);
Suralc committed
81 82 83 84 85 86 87 88
		$model = new FakedValidationModel();
		$model->attr_string = 'a';
		$val->validateAttribute($model, 'attr_string');
		$this->assertFalse($model->hasErrors());
		$model = new FakedValidationModel();
		$model->attr_string = 'abc';
		$val->validateAttribute($model, 'attr_string');
		$this->assertTrue($model->hasErrors('attr_string'));
Alexander Makarov committed
89 90
		$val = new StringValidator(['max' => 1]);
		$model = FakedValidationModel::createWithAttributes(['attr_str' => ['abc']]);
91 92
		$val->validateAttribute($model, 'attr_str');
		$this->assertTrue($model->hasErrors('attr_str'));
Suralc committed
93 94 95 96
	}

	public function testEnsureMessagesOnInit()
	{
Alexander Makarov committed
97
		$val = new StringValidator(['min' => 1, 'max' => 2]);
Suralc committed
98 99 100 101 102 103 104
		$this->assertTrue(is_string($val->message));
		$this->assertTrue(is_string($val->tooLong));
		$this->assertTrue(is_string($val->tooShort));
	}

	public function testCustomErrorMessageInValidateAttribute()
	{
Alexander Makarov committed
105
		$val = new StringValidator([
Suralc committed
106 107
			'min' => 5,
			'tooShort' => '{attribute} to short. Min is {min}',
Alexander Makarov committed
108
		]);
Suralc committed
109 110 111 112
		$model = new FakedValidationModel();
		$model->attr_string = 'abc';
		$val->validateAttribute($model, 'attr_string');
		$this->assertTrue($model->hasErrors('attr_string'));
Suralc committed
113 114
		$errorMsg = $model->getErrors('attr_string');
		$this->assertEquals('attr_string to short. Min is 5', $errorMsg[0]);
Suralc committed
115 116
	}
}