|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ntb\RestAPI; |
|
4
|
|
|
|
|
5
|
|
|
use SapphireTest; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Tests for the rest validation helper. |
|
9
|
|
|
* @todo: add more tests |
|
10
|
|
|
* @author Christian Blank <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class RestValidatorHelperTest extends SapphireTest { |
|
13
|
|
|
|
|
14
|
|
|
public function testIsUrl() { |
|
15
|
|
|
$correctUrls = [ |
|
16
|
|
|
'https://example.com', |
|
17
|
|
|
'http://example.com', |
|
18
|
|
|
'http://foo.com/blah_blah', |
|
19
|
|
|
'http://foo.com/blah_blah/', |
|
20
|
|
|
'http://foo.com/blah_blah_(wikipedia)', |
|
21
|
|
|
'http://foo.com/blah_blah_(wikipedia)_(again)', |
|
22
|
|
|
'http://www.example.com/wpstyle/?p=364', |
|
23
|
|
|
'https://www.example.com/foo/?bar=baz&inga=42&quux', |
|
24
|
|
|
'http://df.ws/123', |
|
25
|
|
|
'http://userid:[email protected]:8080', |
|
26
|
|
|
'http://userid:[email protected]:8080/', |
|
27
|
|
|
'http://[email protected]', |
|
28
|
|
|
'http://[email protected]/', |
|
29
|
|
|
'http://[email protected]:8080', |
|
30
|
|
|
'http://[email protected]:8080/', |
|
31
|
|
|
'http://userid:[email protected]', |
|
32
|
|
|
'http://userid:[email protected]/', |
|
33
|
|
|
'http://142.42.1.1/', |
|
34
|
|
|
'http://142.42.1.1:8080/', |
|
35
|
|
|
'http://fg.ws/', |
|
36
|
|
|
'http://fg.ws', |
|
37
|
|
|
'http://foo.com/blah_(wikipedia)#cite-1', |
|
38
|
|
|
'http://foo.com/blah_(wikipedia)_blah#cite-1', |
|
39
|
|
|
'http://foo.com/unicode_(✪)_in_parens', |
|
40
|
|
|
'http://foo.com/(something)?after=parens', |
|
41
|
|
|
'http://☺.damowmow.com/', |
|
42
|
|
|
'http://code.google.com/events/#&product=browser', |
|
43
|
|
|
'http://j.mp', |
|
44
|
|
|
'ftp://foo.bar/baz', |
|
45
|
|
|
'http://foo.bar/?q=Test%20URL-encoded%20stuff', |
|
46
|
|
|
'http://مثال.إختبار', |
|
47
|
|
|
'http://例子.测试', |
|
48
|
|
|
'http://उदाहरण.परीक्', |
|
49
|
|
|
"http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com", |
|
50
|
|
|
'http://1337.net', |
|
51
|
|
|
'http://a.b-c.de', |
|
52
|
|
|
'http://223.255.255.254' |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
$incorrectUrls = [ |
|
56
|
|
|
'http://', |
|
57
|
|
|
'http://.', |
|
58
|
|
|
'http://..', |
|
59
|
|
|
'http://../', |
|
60
|
|
|
'http://?', |
|
61
|
|
|
'http://??', |
|
62
|
|
|
'http://??/', |
|
63
|
|
|
'http://#', |
|
64
|
|
|
'http://##', |
|
65
|
|
|
'http://##/', |
|
66
|
|
|
'http://foo.bar?q=Spaces should be encoded', |
|
67
|
|
|
'//', |
|
68
|
|
|
'//a', |
|
69
|
|
|
'///a', |
|
70
|
|
|
'///', |
|
71
|
|
|
'http:///a', |
|
72
|
|
|
'foo.com', |
|
73
|
|
|
'rdar://1234', |
|
74
|
|
|
'h://test', |
|
75
|
|
|
'http:// shouldfail.com', |
|
76
|
|
|
':// should fail', |
|
77
|
|
|
'http://foo.bar/foo(bar)baz quux', |
|
78
|
|
|
'ftps://foo.bar/', |
|
79
|
|
|
'http://-error-.invalid/', |
|
80
|
|
|
'http://-a.b.co', |
|
81
|
|
|
'http://a.b-.co', |
|
82
|
|
|
'http://0.0.0.0', |
|
83
|
|
|
'http://10.1.1.0', |
|
84
|
|
|
'http://10.1.1.255', |
|
85
|
|
|
'http://224.1.1.1', |
|
86
|
|
|
'http://1.1.1.1.1', |
|
87
|
|
|
'http://123.123.123', |
|
88
|
|
|
'http://3628126748', |
|
89
|
|
|
'http://.www.foo.bar/', |
|
90
|
|
|
'http://.www.foo.bar./', |
|
91
|
|
|
'http://10.1.1.1', |
|
92
|
|
|
'http://10.1.1.254' |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
foreach($correctUrls as $url) { |
|
96
|
|
|
$t = $url; |
|
97
|
|
|
$this->assertTrue(RestValidatorHelper::is_url($url), "$t should be valid"); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
foreach($incorrectUrls as $url) { |
|
101
|
|
|
$t = $url; |
|
102
|
|
|
$this->assertFalse(RestValidatorHelper::is_url($url), "$t should be invalid"); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
View Code Duplication |
public function testValidateCountryCode() { |
|
|
|
|
|
|
107
|
|
|
$this->assertEquals('US', RestValidatorHelper::validate_country_code(['cc' => 'US'], 'cc')); |
|
|
|
|
|
|
108
|
|
|
$this->assertEquals(null, RestValidatorHelper::validate_country_code([], 'cc', ['required' => false])); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
TestHelper::assertException(function() { |
|
111
|
|
|
RestValidatorHelper::validate_country_code(['cc' => 'FOO'], 'cc'); |
|
112
|
|
|
}, 'ValidationException'); |
|
113
|
|
|
TestHelper::assertException(function() { |
|
114
|
|
|
RestValidatorHelper::validate_country_code([], 'cc'); |
|
115
|
|
|
}, 'ValidationException'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
View Code Duplication |
public function testValidateEmail() { |
|
|
|
|
|
|
119
|
|
|
$this->assertEquals('[email protected]', RestValidatorHelper::validate_email(['mail' => '[email protected]'], 'mail')); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
TestHelper::assertException(function() { |
|
122
|
|
|
RestValidatorHelper::validate_email(['mail' => 'FOO'], 'mail'); |
|
123
|
|
|
}, 'ValidationException'); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals(null, RestValidatorHelper::validate_email([], 'mail', ['required' => false])); |
|
|
|
|
|
|
126
|
|
|
TestHelper::assertException(function() { |
|
127
|
|
|
RestValidatorHelper::validate_email([], 'mail'); |
|
128
|
|
|
}, 'ValidationException'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
View Code Duplication |
public function testValidateUrl() { |
|
|
|
|
|
|
132
|
|
|
$this->assertEquals('http://test.com', RestValidatorHelper::validate_url(['url' => 'http://test.com'], 'url')); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
TestHelper::assertException(function() { |
|
135
|
|
|
RestValidatorHelper::validate_url(['url' => 'FOO'], 'url'); |
|
136
|
|
|
}, 'ValidationException'); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertEquals(null, RestValidatorHelper::validate_url([], 'url', ['required' => false])); |
|
|
|
|
|
|
139
|
|
|
TestHelper::assertException(function() { |
|
140
|
|
|
RestValidatorHelper::validate_url([], 'url'); |
|
141
|
|
|
}, 'ValidationException'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
public function testValidateString() { |
|
146
|
|
|
$this->assertEquals('foo bar', RestValidatorHelper::validate_string(['str' => 'foo bar'], 'str')); |
|
|
|
|
|
|
147
|
|
|
$this->assertEquals(null, RestValidatorHelper::validate_string([], 'str', ['required' => false])); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
TestHelper::assertException(function() { |
|
150
|
|
|
RestValidatorHelper::validate_string([], 'str'); |
|
151
|
|
|
}, 'ValidationException'); |
|
152
|
|
|
|
|
153
|
|
|
TestHelper::assertException(function() { |
|
154
|
|
|
RestValidatorHelper::validate_string(['str' => 15], 'str'); |
|
155
|
|
|
}, 'ValidationException'); |
|
156
|
|
|
|
|
157
|
|
|
TestHelper::assertException(function() { |
|
158
|
|
|
RestValidatorHelper::validate_string(['str' => 'foo bar'], 'str', ['max' => 4]); |
|
159
|
|
|
}, 'ValidationException'); |
|
160
|
|
|
$this->assertEquals('foo', RestValidatorHelper::validate_string(['str' => 'foo'], 'str'), ['max' => 4]); |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
TestHelper::assertException(function() { |
|
164
|
|
|
RestValidatorHelper::validate_string(['str' => 'fo'], 'str', ['min' => 3, 'max' => 4]); |
|
165
|
|
|
}, 'ValidationException'); |
|
166
|
|
|
$this->assertEquals('foo', RestValidatorHelper::validate_string(['str' => 'foo'], 'str', ['min' => 2, 'max' => 4])); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
public function testValidateInt() { |
|
171
|
|
|
$this->assertEquals(5, RestValidatorHelper::validate_int(['int' => 5], 'int')); |
|
|
|
|
|
|
172
|
|
|
$this->assertEquals(null, RestValidatorHelper::validate_int([], 'int', ['required' => false])); |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
TestHelper::assertException(function() { |
|
175
|
|
|
RestValidatorHelper::validate_int([], 'int'); |
|
176
|
|
|
}, 'ValidationException'); |
|
177
|
|
|
|
|
178
|
|
|
TestHelper::assertException(function() { |
|
179
|
|
|
RestValidatorHelper::validate_int(['int' => 'foo bar'], 'int'); |
|
180
|
|
|
}, 'ValidationException'); |
|
181
|
|
|
|
|
182
|
|
|
TestHelper::assertException(function() { |
|
183
|
|
|
RestValidatorHelper::validate_int(['int' => 5], 'int', ['max' => 4]); |
|
184
|
|
|
}, 'ValidationException'); |
|
185
|
|
|
$this->assertEquals(-5, RestValidatorHelper::validate_int(['int' => -5], 'int', ['max' => 0])); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
TestHelper::assertException(function() { |
|
189
|
|
|
RestValidatorHelper::validate_int(['int' => 1], 'int', ['min' => 3, 'max' => 4]); |
|
190
|
|
|
}, 'ValidationException'); |
|
191
|
|
|
$this->assertEquals(2, RestValidatorHelper::validate_int(['int' => 2], 'int', ['min' => 2, 'max' => 4])); |
|
|
|
|
|
|
192
|
|
|
$this->assertEquals(4, RestValidatorHelper::validate_int(['int' => 4], 'int', ['min' => 2, 'max' => 4])); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
View Code Duplication |
public function testValidateDatetime() { |
|
|
|
|
|
|
197
|
|
|
$this->assertEquals('2015-08-07 12:13:14', RestValidatorHelper::validate_datetime(['date' => '2015-08-07 12:13:14'], 'date')); |
|
|
|
|
|
|
198
|
|
|
$this->assertEquals(null, RestValidatorHelper::validate_datetime([], 'date', ['required' => false])); |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
TestHelper::assertException(function() { |
|
201
|
|
|
RestValidatorHelper::validate_datetime([], 'date'); |
|
202
|
|
|
}, 'ValidationException'); |
|
203
|
|
|
|
|
204
|
|
|
TestHelper::assertException(function() { |
|
205
|
|
|
RestValidatorHelper::validate_datetime(['date' => 'error'], 'date'); |
|
206
|
|
|
}, 'ValidationException'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.