|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: harry |
|
5
|
|
|
* Date: 3/8/18 |
|
6
|
|
|
* Time: 4:37 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace PluginSimpleValidate\Tests\unit; |
|
10
|
|
|
|
|
11
|
|
|
use PluginSimpleValidate\FormValidation; |
|
12
|
|
|
use Zend\Diactoros\ServerRequest; |
|
13
|
|
|
|
|
14
|
|
|
class FormValidationTest extends Base |
|
15
|
|
|
{ |
|
16
|
|
|
public function test_createFromPost() |
|
17
|
|
|
{ |
|
18
|
|
|
$validation = new FormValidation($this->language); |
|
19
|
|
|
$validation->createFromPost( |
|
20
|
|
|
$this->create_fake_request(), |
|
21
|
|
|
$this->create_rules() |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
$validation->run(); |
|
25
|
|
|
|
|
26
|
|
|
if (!$validation->getStatus()) { |
|
27
|
|
|
$this->assertEquals([ |
|
28
|
|
|
'email' => [ |
|
29
|
|
|
'field must be a valid email address', |
|
30
|
|
|
], |
|
31
|
|
|
'grant_type' => [ |
|
32
|
|
|
'field is required', |
|
33
|
|
|
], |
|
34
|
|
|
'client_id' => [ |
|
35
|
|
|
'field is required', |
|
36
|
|
|
'field must be a number', |
|
37
|
|
|
], |
|
38
|
|
|
'client_secret' => [ |
|
39
|
|
|
'field is required', |
|
40
|
|
|
'field may only letters and numbers', |
|
41
|
|
|
], |
|
42
|
|
|
'redirect_uri' => [ |
|
43
|
|
|
'field is required', |
|
44
|
|
|
], |
|
45
|
|
|
'username' => [ |
|
46
|
|
|
'field is required', |
|
47
|
|
|
'field length must be greater or equal than 5 or less or equal than 10', |
|
48
|
|
|
], |
|
49
|
|
|
'password' => [ |
|
50
|
|
|
'field is required', |
|
51
|
|
|
'field length must be greater than 5', |
|
52
|
|
|
], |
|
53
|
|
|
'scope' => [ |
|
54
|
|
|
'field is required', |
|
55
|
|
|
], |
|
56
|
|
|
], |
|
57
|
|
|
$validation->getErrors() |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return ServerRequest |
|
64
|
|
|
*/ |
|
65
|
|
|
private function create_fake_request() |
|
66
|
|
|
{ |
|
67
|
|
|
return new ServerRequest( |
|
68
|
|
|
[], |
|
69
|
|
|
[], |
|
70
|
|
|
null, |
|
71
|
|
|
'POST', |
|
72
|
|
|
'php://input', |
|
73
|
|
|
[], |
|
74
|
|
|
[], |
|
75
|
|
|
[], |
|
76
|
|
|
[ |
|
77
|
|
|
'email' => 'abc', |
|
78
|
|
|
'grant_type' => '', |
|
79
|
|
|
'client_id' => '', |
|
80
|
|
|
'client_secret' => '', |
|
81
|
|
|
'redirect_uri' => '', |
|
82
|
|
|
'username' => '', |
|
83
|
|
|
'password' => '', |
|
84
|
|
|
'scope' => '', |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
private function create_rules() |
|
94
|
|
|
{ |
|
95
|
|
|
return [ |
|
96
|
|
|
'email' => 'required,validEmail', |
|
97
|
|
|
'grant_type' => 'required', |
|
98
|
|
|
'client_id' => 'required,isNumber', |
|
99
|
|
|
'client_secret' => 'required,isAlphaOrNumeric', |
|
100
|
|
|
'redirect_uri' => 'required', |
|
101
|
|
|
'username' => 'required,lengthBetweenOrEqual:5:10', |
|
102
|
|
|
'password' => 'required,lengthGreaterThan:5', |
|
103
|
|
|
'scope' => 'required', |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
} |