Skip to content

Commit e4fa74b

Browse files
Added test cases
1 parent 2bf8504 commit e4fa74b

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

tests/IdVNTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Tests;
10+
11+
use Illuminate\Support\Facades\Lang;
12+
use PHPViet\Laravel\Validation\Rules\IdVN;
13+
14+
/**
15+
* @author Vuong Minh <[email protected]>
16+
* @since 1.0.0
17+
*/
18+
class IdVNTest extends TestCase
19+
{
20+
21+
public function testValidId()
22+
{
23+
$rule = new IdVN();
24+
$id = '025479661';
25+
$this->assertTrue($rule->passes('attribute', $id));
26+
$this->assertTrue($rule('attribute', $id, null, null));
27+
28+
$validId = $this->app['validator']->validate([
29+
'id' => $id
30+
], [
31+
'id' => 'id_vn'
32+
]);
33+
$this->assertEquals($id, array_shift($validId));
34+
}
35+
36+
public function testInvalidIp()
37+
{
38+
$rule = new IdVN();
39+
$id = '025479661123123123123';
40+
$this->assertFalse($rule->passes('attribute', $id));
41+
$this->assertFalse($rule('attribute', $id, null, null));
42+
$this->expectException('Illuminate\Validation\ValidationException');
43+
$this->app['validator']->validate([
44+
'id' => $id
45+
], [
46+
'id' => 'id_vn'
47+
]);
48+
}
49+
50+
public function testCanTranslateErrorMessage()
51+
{
52+
Lang::addLines([
53+
'validation.phpviet.id' => 'id',
54+
], Lang::getLocale());
55+
$rule = new IdVN();
56+
$rule->passes('attribute', '025479661123123123123!!!');
57+
$this->assertEquals('id', $rule->message());
58+
}
59+
60+
}

tests/IpVNTest.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Tests;
10+
11+
use Illuminate\Support\Facades\Lang;
12+
use PHPViet\Laravel\Validation\Rules\IpVN;
13+
14+
/**
15+
* @author Vuong Minh <[email protected]>
16+
* @since 1.0.0
17+
*/
18+
class IpVNTest extends TestCase
19+
{
20+
21+
public function testValidCases()
22+
{
23+
$rule = new IpVN();
24+
$ruleV4 = new IpVN(IpVN::IPV4);
25+
$ruleV6 = new IpVN(IpVN::IPV6);
26+
$ipv4 = '113.173.134.203';
27+
$ipv6 = '2405:4800:102:1::3';
28+
$this->assertTrue($rule->passes('attribute', $ipv4));
29+
$this->assertTrue($rule('attribute', $ipv4, null, null));
30+
$this->assertTrue($ruleV4->passes('attribute', $ipv4));
31+
$this->assertTrue($ruleV4('attribute', $ipv4, null, null));
32+
$this->assertTrue($ruleV6->passes('attribute', $ipv6));
33+
$this->assertTrue($ruleV6('attribute', $ipv6, null, null));
34+
$this->assertFalse($ruleV4->passes('attribute', $ipv6));
35+
$this->assertFalse($ruleV4('attribute', $ipv6, null, null));
36+
$this->assertFalse($ruleV6->passes('attribute', $ipv4));
37+
$this->assertFalse($ruleV6('attribute', $ipv4, null, null));
38+
$validIps = $this->app['validator']->validate([
39+
'ipv4' => $ipv4,
40+
'ipv6' => $ipv6
41+
], [
42+
'ipv4' => 'ip_vn|ipv4_vn',
43+
'ipv6' => 'ip_vn|ipv6_vn'
44+
]);
45+
$this->assertEquals($ipv4, $validIps['ipv4']);
46+
$this->assertEquals($ipv6, $validIps['ipv6']);
47+
}
48+
49+
50+
public function testInvalidCases()
51+
{
52+
$rule = new IpVN();
53+
$ip = '113.173.134.203@';
54+
$this->assertFalse($rule->passes('attribute', $ip));
55+
$this->assertFalse($rule('attribute', $ip, null, null));
56+
$this->expectException('Illuminate\Validation\ValidationException');
57+
$this->app['validator']->validate([
58+
'ip' => $ip
59+
], [
60+
'ip' => 'ip_vn|ipv4_vn|ipv6_vn'
61+
]);
62+
}
63+
64+
public function testCanTranslateErrorMessage()
65+
{
66+
Lang::addLines([
67+
'validation.phpviet.ip' => 'ip',
68+
], Lang::getLocale());
69+
$rule = new IpVN();
70+
$rule->passes('attribute', '113.173.134.203@');
71+
$this->assertEquals('ip', $rule->message());
72+
}
73+
74+
}

tests/LandLineVNTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Tests;
10+
11+
use Illuminate\Support\Facades\Lang;
12+
use PHPViet\Laravel\Validation\Rules\LandLineVN;
13+
14+
/**
15+
* @author Vuong Minh <[email protected]>
16+
* @since 1.0.0
17+
*/
18+
class LandLineVNTest extends TestCase
19+
{
20+
21+
public function testValidId()
22+
{
23+
$rule = new LandLineVN();
24+
$landLine = '02838574955';
25+
$this->assertTrue($rule->passes('attribute', $landLine));
26+
$this->assertTrue($rule('attribute', $landLine, null, null));
27+
28+
$validLandLine = $this->app['validator']->validate([
29+
'landLine' => $landLine
30+
], [
31+
'landLine' => 'land_line_vn'
32+
]);
33+
$this->assertEquals($landLine, array_shift($validLandLine));
34+
}
35+
36+
public function testInvalidIp()
37+
{
38+
$rule = new LandLineVN();
39+
$landLine = '02838574955!@#';
40+
$this->assertFalse($rule->passes('attribute', $landLine));
41+
$this->assertFalse($rule('attribute', $landLine, null, null));
42+
$this->expectException('Illuminate\Validation\ValidationException');
43+
$this->app['validator']->validate([
44+
'landLine' => $landLine
45+
], [
46+
'landLine' => 'land_line_vn'
47+
]);
48+
}
49+
50+
public function testCanTranslateErrorMessage()
51+
{
52+
Lang::addLines([
53+
'validation.phpviet.land_line' => 'land_line',
54+
], Lang::getLocale());
55+
$rule = new LandLineVN();
56+
$rule->passes('attribute', '02838574955!!!');
57+
$this->assertEquals('land_line', $rule->message());
58+
}
59+
60+
}

tests/MobileVNTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Tests;
10+
11+
use Illuminate\Support\Facades\Lang;
12+
use PHPViet\Laravel\Validation\Rules\MobileVN;
13+
14+
/**
15+
* @author Vuong Minh <[email protected]>
16+
* @since 1.0.0
17+
*/
18+
class MobileVNTest extends TestCase
19+
{
20+
21+
public function testValidId()
22+
{
23+
$rule = new MobileVN();
24+
$mobile = '0909113911';
25+
$this->assertTrue($rule->passes('attribute', $mobile));
26+
$this->assertTrue($rule('attribute', $mobile, null, null));
27+
28+
$validMobile = $this->app['validator']->validate([
29+
'mobile' => $mobile
30+
], [
31+
'mobile' => 'mobile_vn'
32+
]);
33+
$this->assertEquals($mobile, array_shift($validMobile));
34+
}
35+
36+
public function testInvalidIp()
37+
{
38+
$rule = new MobileVN();
39+
$mobile = '0909113911!@#';
40+
$this->assertFalse($rule->passes('attribute', $mobile));
41+
$this->assertFalse($rule('attribute', $mobile, null, null));
42+
$this->expectException('Illuminate\Validation\ValidationException');
43+
$this->app['validator']->validate([
44+
'mobile' => $mobile
45+
], [
46+
'mobile' => 'mobile_vn'
47+
]);
48+
}
49+
50+
public function testCanTranslateErrorMessage()
51+
{
52+
Lang::addLines([
53+
'validation.phpviet.mobile' => 'mobile',
54+
], Lang::getLocale());
55+
$rule = new MobileVN();
56+
$rule->passes('attribute', '0909113911!!!');
57+
$this->assertEquals('mobile', $rule->message());
58+
}
59+
60+
}

tests/TestCase.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Tests;
10+
11+
use Orchestra\Testbench\TestCase as BaseTestCase;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
class TestCase extends BaseTestCase
18+
{
19+
20+
public function getPackageProviders($app)
21+
{
22+
return ['PHPViet\Laravel\Validation\ServiceProvider'];
23+
}
24+
25+
}

0 commit comments

Comments
 (0)