Skip to content

Commit beeda04

Browse files
Added test cases
1 parent 664ae61 commit beeda04

File tree

13 files changed

+300
-30
lines changed

13 files changed

+300
-30
lines changed

src/Bootstrap.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ protected function loadTrans(I18N $i18n): void
4444
'class' => 'yii\i18n\PhpMessageSource',
4545
'basePath' => '@phpviet/yii/validation/messages',
4646
'fileMap' => [
47-
'phpviet/validation' => 'validation'
47+
'phpviet/validation' => 'validation.php'
4848
]
4949
], $i18n->translations['phpviet/validation'] ?? []);
50-
5150
}
5251

5352
/**

src/messages/en/validation.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/messages/vi/validation.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
*/
88

99
return [
10-
'id' => '{attribute} phải là số chứng minh thư hoặc thẻ căn cước tại Việt Nam.',
11-
'mobile' => '{attribute} phải là số di động tại Việt Nam.',
12-
'land_line' => '{attribute} phải là số điện thoại bàn tại Việt Nam.',
13-
'ip' => '{attribute} phải là ip Việt Nam.',
14-
'ipv4' => '{attribute} phải là ipv4 tại Việt Nam.',
15-
'ipv6' => '{attribute} phải là ipv6 tại Việt Nam.',
10+
'{attribute} must be an id number of Vietnam.' => '{attribute} phải là số chứng minh thư hoặc thẻ căn cước tại Việt Nam.',
11+
'{attribute} must be a mobile phone number of Vietnam.' => '{attribute} phải là số di động tại Việt Nam.',
12+
'{attribute} must be a land line phone number of Vietnam.' => '{attribute} phải là số điện thoại bàn tại Việt Nam.',
13+
'{attribute} must be Vietnam ip.' => '{attribute} phải là ip Việt Nam.',
14+
'{attribute} must be Vietnam ipv4.' => '{attribute} phải là ipv4 tại Việt Nam.',
15+
'{attribute} must be Vietnam ipv6.' => '{attribute} phải là ipv6 tại Việt Nam.',
1616
];

src/validators/IdVNValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class IdVNValidator extends RegularExpressionValidator
2626
*/
2727
public function init()
2828
{
29-
$this->message = $this->message ?? Yii::t('phpviet/validation', 'id');
29+
$this->message = $this->message ?? Yii::t('phpviet/validation', '{attribute} must be an id number of Vietnam.');
3030
$this->pattern = PatternProvider::pregFormat();
3131

3232
parent::init();

src/validators/IpVNValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ protected function getDefaultMessage(): string
8080
{
8181
switch ($this->version) {
8282
case self::IPV4:
83-
return Yii::t('phpviet/validation', 'ipv4');
83+
return Yii::t('phpviet/validation', '{attribute} must be Vietnam ipv4.');
8484
case self::IPV6:
85-
return Yii::t('phpviet/validation', 'ipv6');
85+
return Yii::t('phpviet/validation', '{attribute} must be Vietnam ipv6.');
8686
default:
87-
return Yii::t('phpviet/validation', 'ip');
87+
return Yii::t('phpviet/validation', '{attribute} must be Vietnam ip.');
8888
}
8989
}
9090

src/validators/LandLineVNValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LandLineVNValidator extends RegularExpressionValidator
2626
*/
2727
public function init()
2828
{
29-
$this->message = $this->message ?? Yii::t('phpviet/validation', 'land_line');
29+
$this->message = $this->message ?? Yii::t('phpviet/validation', '{attribute} must be a land line phone number of Vietnam.');
3030
$this->pattern = PatternProvider::pregFormat();
3131

3232
parent::init();

src/validators/MobileVNValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MobileVNValidator extends RegularExpressionValidator
2626
*/
2727
public function init()
2828
{
29-
$this->message = $this->message ?? Yii::t('phpviet/validation', 'mobile');
29+
$this->message = $this->message ?? Yii::t('phpviet/validation', '{attribute} must be a mobile phone number of Vietnam.');
3030
$this->pattern = PatternProvider::pregFormat();
3131

3232
parent::init();

tests/IdVNTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/yii-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace phpviet\yii\validation\tests;
10+
11+
use yii\base\DynamicModel;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class IdVNTest extends TestCase
18+
{
19+
public function testValid()
20+
{
21+
$model = DynamicModel::validateData([
22+
'id' => '025479661',
23+
], [
24+
[['id'], 'id_vn'],
25+
]);
26+
$this->assertFalse($model->hasErrors());
27+
}
28+
29+
public function testInvalid()
30+
{
31+
$model = DynamicModel::validateData([
32+
'id' => '02 5479661',
33+
], [
34+
[['id'], 'id_vn'],
35+
]);
36+
$this->assertTrue($model->hasErrors());
37+
}
38+
39+
public function testCanTranslateErrorMessage()
40+
{
41+
$model = DynamicModel::validateData([
42+
'id' => '02 5479661',
43+
], [
44+
[['id'], 'id_vn'],
45+
]);
46+
$this->assertContains('Việt Nam', current($model->getErrors('id')));
47+
}
48+
49+
}

tests/IpVNTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/yii-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace phpviet\yii\validation\tests;
10+
11+
use yii\base\DynamicModel;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class IpVNTest extends TestCase
18+
{
19+
20+
public function testValid()
21+
{
22+
$model = DynamicModel::validateData([
23+
'ipv4' => '113.173.134.203',
24+
'ipv6' => '2405:4800:102:1::3'
25+
], [
26+
[['ipv4', 'ipv6'], 'ip_vn'],
27+
[['ipv4'], 'ipv4_vn'],
28+
[['ipv6'], 'ipv6_vn']
29+
]);
30+
$this->assertFalse($model->hasErrors());
31+
}
32+
33+
public function testInvalid()
34+
{
35+
$model = DynamicModel::validateData([
36+
'ipv4' => '113.173.134.203@',
37+
'ipv6' => '2405:4800:102:1::3!'
38+
], [
39+
[['ipv4', 'ipv6'], 'ip_vn'],
40+
[['ipv4'], 'ipv4_vn'],
41+
[['ipv6'], 'ipv6_vn']
42+
]);
43+
$this->assertTrue($model->hasErrors());
44+
}
45+
46+
public function testCanTranslateErrorMessage()
47+
{
48+
$model = DynamicModel::validateData([
49+
'ipv4' => '113.173.134.203@',
50+
'ipv6' => '2405:4800:102:1::3!'
51+
], [
52+
[['ipv4', 'ipv6'], 'ip_vn'],
53+
[['ipv4'], 'ipv4_vn'],
54+
[['ipv6'], 'ipv6_vn']
55+
]);
56+
$this->assertContains('Việt Nam', current($model->getErrors('ipv4')));
57+
$this->assertContains('Việt Nam', current($model->getErrors('ipv6')));
58+
}
59+
60+
}

tests/LandLineVNTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/yii-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace phpviet\yii\validation\tests;
10+
11+
use yii\base\DynamicModel;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class LandLineVNTest extends TestCase
18+
{
19+
public function testValid()
20+
{
21+
$model = DynamicModel::validateData([
22+
'landLine' => '02838574955',
23+
], [
24+
[['landLine'], 'land_line_vn'],
25+
]);
26+
$this->assertFalse($model->hasErrors());
27+
}
28+
29+
public function testInvalid()
30+
{
31+
$model = DynamicModel::validateData([
32+
'landLine' => '02838574955!',
33+
], [
34+
[['landLine'], 'land_line_vn'],
35+
]);
36+
$this->assertTrue($model->hasErrors());
37+
}
38+
39+
public function testCanTranslateErrorMessage()
40+
{
41+
$model = DynamicModel::validateData([
42+
'landLine' => '02838574_955',
43+
], [
44+
[['landLine'], 'land_line_vn'],
45+
]);
46+
$this->assertContains('Việt Nam', current($model->getErrors('landLine')));
47+
}
48+
}

0 commit comments

Comments
 (0)