yii2-tempmail-validator/tests/Ely/Yii2/TempmailValidatorTest.php

65 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2016-05-01 22:01:39 +05:30
<?php
namespace Ely\Yii2;
include __DIR__ . '/../../../vendor/yiisoft/yii2/Yii.php';
2016-05-01 22:01:39 +05:30
use PHPUnit\Framework\TestCase;
2016-05-01 22:01:39 +05:30
use yii\base\Model;
use yii\console\Application;
class TempmailValidatorTest extends TestCase
2016-05-01 22:01:39 +05:30
{
protected function setUp(): void
2016-05-01 22:01:39 +05:30
{
parent::setUp();
new Application([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => __DIR__ . '/../../../vendor',
2016-05-01 22:01:39 +05:30
]);
}
public function testValidateValue()
{
$validator = new TempmailValidator();
$this->assertTrue($validator->validate('team@ely.by'));
$this->assertFalse($validator->validate('h4l29@spam4.me', $error));
$this->assertSame('the input value is not allowed email address.', $error);
2016-05-01 22:01:39 +05:30
}
public function testValidatorOnModel()
{
$model = $this->createDummyModel();
2016-05-01 22:01:39 +05:30
$model->email = 'team@ely.by';
$this->assertTrue($model->validate());
$model = $this->createDummyModel();
2016-05-01 22:01:39 +05:30
$model->email = 'spam@spam4.me';
$this->assertFalse($model->validate());
$this->assertSame('Email is not allowed email address.', $model->getFirstError('email'));
2016-05-01 22:01:39 +05:30
$model = $this->createDummyModel('{attribute} with custom message.');
$model->email = 'spam@spam4.me';
$this->assertFalse($model->validate());
$this->assertSame('Email with custom message.', $model->getFirstError('email'));
2016-05-01 22:01:39 +05:30
}
private function createDummyModel(string $customMessage = null): Model
{
return new class(['customMessage' => $customMessage]) extends Model
{
public $email;
2016-05-01 22:01:39 +05:30
public $customMessage;
2016-05-01 22:01:39 +05:30
public function rules(): array
{
return [
[['email'], TempmailValidator::class, 'message' => $this->customMessage],
];
}
};
2016-05-01 22:01:39 +05:30
}
}