RegularExpressionValidatorTest.php 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace yiiunit\framework\validators;


use yii\validators\RegularExpressionValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;

class RegularExpressionValidatorTest extends TestCase
{
12 13 14 15 16 17
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

18 19
	public function testValidateValue()
	{
Alexander Makarov committed
20
		$val = new RegularExpressionValidator(['pattern' => '/^[a-zA-Z0-9](\.)?([^\/]*)$/m']);
Qiang Xue committed
21 22 23
		$this->assertTrue($val->validate('b.4'));
		$this->assertFalse($val->validate('b./'));
		$this->assertFalse($val->validate(['a', 'b']));
24
		$val->not = true;
Qiang Xue committed
25 26 27
		$this->assertFalse($val->validate('b.4'));
		$this->assertTrue($val->validate('b./'));
		$this->assertFalse($val->validate(['a', 'b']));
28 29 30 31
	}

	public function testValidateAttribute()
	{
Alexander Makarov committed
32 33
		$val = new RegularExpressionValidator(['pattern' => '/^[a-zA-Z0-9](\.)?([^\/]*)$/m']);
		$m = FakedValidationModel::createWithAttributes(['attr_reg1' => 'b.4']);
34 35 36 37 38 39 40 41 42
		$val->validateAttribute($m, 'attr_reg1');
		$this->assertFalse($m->hasErrors('attr_reg1'));
		$m->attr_reg1 = 'b./';
		$val->validateAttribute($m, 'attr_reg1');
		$this->assertTrue($m->hasErrors('attr_reg1'));
	}

	public function testMessageSetOnInit()
	{
Alexander Makarov committed
43
		$val = new RegularExpressionValidator(['pattern' => '/^[a-zA-Z0-9](\.)?([^\/]*)$/m']);
44 45 46 47 48 49 50
		$this->assertTrue(is_string($val->message));
	}

	public function testInitException()
	{
		$this->setExpectedException('yii\base\InvalidConfigException');
		$val = new RegularExpressionValidator();
Qiang Xue committed
51
		$val->validate('abc');
52 53
	}

Qiang Xue committed
54
}