2016-09-03 04:24:22 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\validators;
|
2016-09-03 04:24:22 +05:30
|
|
|
|
|
|
|
use Codeception\Specify;
|
2019-02-23 04:41:57 +05:30
|
|
|
use common\tests\unit\TestCase;
|
2016-09-03 04:24:22 +05:30
|
|
|
use common\validators\UuidValidator;
|
|
|
|
use Faker\Provider\Uuid;
|
|
|
|
use yii\base\Model;
|
|
|
|
|
|
|
|
class UuidValidatorTest extends TestCase {
|
|
|
|
use Specify;
|
|
|
|
|
|
|
|
public function testValidateAttribute() {
|
|
|
|
$this->specify('expected error if passed empty value', function() {
|
2016-11-04 22:03:57 +05:30
|
|
|
$validator = new UuidValidator();
|
|
|
|
$model = $this->createModel();
|
|
|
|
$validator->validateAttribute($model, 'attribute');
|
|
|
|
$this->assertTrue($model->hasErrors());
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['Attribute must be valid uuid'], $model->getErrors('attribute'));
|
2016-09-03 04:24:22 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('expected error if passed invalid string', function() {
|
2016-11-04 22:03:57 +05:30
|
|
|
$validator = new UuidValidator();
|
|
|
|
$model = $this->createModel();
|
2016-09-03 04:24:22 +05:30
|
|
|
$model->attribute = '123456789';
|
2016-11-04 22:03:57 +05:30
|
|
|
$validator->validateAttribute($model, 'attribute');
|
|
|
|
$this->assertTrue($model->hasErrors());
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['Attribute must be valid uuid'], $model->getErrors('attribute'));
|
2016-11-04 22:03:57 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed nil uuid and allowNil is set to true', function() {
|
|
|
|
$validator = new UuidValidator();
|
|
|
|
$model = $this->createModel();
|
|
|
|
$model->attribute = '00000000-0000-0000-0000-000000000000';
|
|
|
|
$validator->validateAttribute($model, 'attribute');
|
|
|
|
$this->assertFalse($model->hasErrors());
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed nil uuid and allowNil is set to false', function() {
|
|
|
|
$validator = new UuidValidator();
|
|
|
|
$validator->allowNil = false;
|
|
|
|
$model = $this->createModel();
|
|
|
|
$model->attribute = '00000000-0000-0000-0000-000000000000';
|
|
|
|
$validator->validateAttribute($model, 'attribute');
|
|
|
|
$this->assertTrue($model->hasErrors());
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(['Attribute must be valid uuid'], $model->getErrors('attribute'));
|
2016-09-03 04:24:22 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed valid uuid', function() {
|
2016-11-04 22:03:57 +05:30
|
|
|
$validator = new UuidValidator();
|
|
|
|
$model = $this->createModel();
|
2016-09-03 04:24:22 +05:30
|
|
|
$model->attribute = Uuid::uuid();
|
2016-11-04 22:03:57 +05:30
|
|
|
$validator->validateAttribute($model, 'attribute');
|
|
|
|
$this->assertFalse($model->hasErrors());
|
2016-09-03 04:24:22 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed uuid string without dashes and converted to standart value', function() {
|
2016-11-04 22:03:57 +05:30
|
|
|
$validator = new UuidValidator();
|
|
|
|
$model = $this->createModel();
|
2016-09-03 04:24:22 +05:30
|
|
|
$originalUuid = Uuid::uuid();
|
|
|
|
$model->attribute = str_replace('-', '', $originalUuid);
|
2016-11-04 22:03:57 +05:30
|
|
|
$validator->validateAttribute($model, 'attribute');
|
|
|
|
$this->assertFalse($model->hasErrors());
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame($originalUuid, $model->attribute);
|
2016-09-03 04:24:22 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:03:57 +05:30
|
|
|
/**
|
|
|
|
* @return Model
|
|
|
|
*/
|
|
|
|
public function createModel() {
|
|
|
|
return new class extends Model {
|
|
|
|
public $attribute;
|
|
|
|
};
|
2016-09-03 04:24:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|