1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class DeCaptchaAbstractTest. |
5
|
|
|
*/ |
6
|
|
|
class DeCaptchaAbstractTest extends PHPUnit_Framework_TestCase |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject|\jumper423\decaptcha\core\DeCaptchaAbstract |
10
|
|
|
*/ |
11
|
|
|
public function newInstance() |
12
|
|
|
{ |
13
|
|
|
$abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
14
|
|
|
$abstract->errorLang = \jumper423\decaptcha\core\DeCaptchaErrors::LANG_RU; |
|
|
|
|
15
|
|
|
|
16
|
|
|
return $abstract; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testGetBaseUrl() |
20
|
|
|
{ |
21
|
|
|
$abstract = $this->newInstance(); |
22
|
|
|
$getBaseUrlCaller = function () { |
23
|
|
|
return $this->getBaseUrl(); |
|
|
|
|
24
|
|
|
}; |
25
|
|
|
$abstract->domain = 'domain'; |
26
|
|
|
$bound = $getBaseUrlCaller->bindTo($abstract, $abstract); |
27
|
|
|
$this->assertEquals('http://domain/', $bound()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testSetApiKey() |
31
|
|
|
{ |
32
|
|
|
$abstract = $this->newInstance(); |
33
|
|
|
$abstract->setApiKey('123456val'); |
|
|
|
|
34
|
|
|
$apiKeyValCaller = function () { |
35
|
|
|
return $this->apiKey; |
|
|
|
|
36
|
|
|
}; |
37
|
|
|
$bound = $apiKeyValCaller->bindTo($abstract, $abstract); |
38
|
|
|
$this->assertEquals('123456val', $bound()); |
39
|
|
|
|
40
|
|
|
$abstract->setApiKey(function () { |
|
|
|
|
41
|
|
|
return '123456'.'fun'; |
42
|
|
|
}); |
43
|
|
|
$apiKeyFunCaller = function () { |
44
|
|
|
return $this->apiKey; |
45
|
|
|
}; |
46
|
|
|
$bound = $apiKeyFunCaller->bindTo($abstract, $abstract); |
47
|
|
|
$this->assertEquals('123456fun', $bound()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetActionUrl() |
51
|
|
|
{ |
52
|
|
|
$abstract = $this->newInstance(); |
53
|
|
|
$getBaseUrlGetCodeCaller = function () { |
54
|
|
|
$this->captchaId = 123; |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $this->getActionUrl('get_code'); |
|
|
|
|
57
|
|
|
}; |
58
|
|
|
$getBaseUrlGetBalanceCaller = function () { |
59
|
|
|
$this->captchaId = 234; |
60
|
|
|
|
61
|
|
|
return $this->getActionUrl('get_balance'); |
|
|
|
|
62
|
|
|
}; |
63
|
|
|
$abstract->domain = 'domain'; |
64
|
|
|
$abstract->setApiKey('123456'); |
|
|
|
|
65
|
|
|
$bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract); |
66
|
|
|
$this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound()); |
67
|
|
|
$bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract); |
68
|
|
|
$this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetFilePath() |
72
|
|
|
{ |
73
|
|
|
$abstract = $this->newInstance(); |
74
|
|
|
$getFilePathCaller = function ($val) { |
75
|
|
|
return $this->getFilePath($val); |
|
|
|
|
76
|
|
|
}; |
77
|
|
|
$bound = $getFilePathCaller->bindTo($abstract, $abstract); |
78
|
|
|
$this->assertEquals(__DIR__.'/data/Captcha.jpg', $bound(__DIR__.'/data/Captcha.jpg')); |
79
|
|
|
$filePathUpload = $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha.jpg'); |
80
|
|
|
$file1 = file_get_contents(__DIR__.'/data/Captcha.jpg'); |
81
|
|
|
$file2 = file_get_contents($filePathUpload); |
82
|
|
|
$this->assertEquals($file1, $file2); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
87
|
|
|
* @expectedExceptionCode 16 |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function testGetFilePathErrorFileNotFound() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$abstract = $this->newInstance(); |
92
|
|
|
$getFilePathCaller = function ($val) { |
93
|
|
|
return $this->getFilePath($val); |
|
|
|
|
94
|
|
|
}; |
95
|
|
|
$bound = $getFilePathCaller->bindTo($abstract, $abstract); |
96
|
|
|
$bound(__DIR__.'/data/Captcha1.jpg'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
101
|
|
|
* @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
102
|
|
|
* @expectedExceptionCode 15 |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function testGetFilePathErrorFileIsNotLoaded() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$abstract = $this->newInstance(); |
107
|
|
|
$getFilePathCaller = function ($val) { |
108
|
|
|
return $this->getFilePath($val); |
|
|
|
|
109
|
|
|
}; |
110
|
|
|
$bound = $getFilePathCaller->bindTo($abstract, $abstract); |
111
|
|
|
$bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
116
|
|
|
* @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
117
|
|
|
* @expectedExceptionCode 14 |
118
|
|
|
*/ |
119
|
|
|
public function testGetFilePathErrorFileIsNotWriteAccessFile() |
120
|
|
|
{ |
121
|
|
|
$abstract = $this->newInstance(); |
122
|
|
|
$getFilePathCaller = function ($val) { |
123
|
|
|
return $this->getFilePath($val); |
|
|
|
|
124
|
|
|
}; |
125
|
|
|
$bound = $getFilePathCaller->bindTo($abstract, $abstract); |
126
|
|
|
$path = tempnam(sys_get_temp_dir(), 'WRITE_ACCESS_FILE'); |
127
|
|
|
chmod($path, 0000); |
128
|
|
|
$bound($path); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testGetResponse() |
132
|
|
|
{ |
133
|
|
|
$abstract = $this->newInstance(); |
134
|
|
|
$abstract->domain = 'echo.jsontest.com/aaa/bbb'; |
135
|
|
|
$getResponseCaller = function ($val) { |
136
|
|
|
return $this->getResponse($val); |
|
|
|
|
137
|
|
|
}; |
138
|
|
|
$bound = $getResponseCaller->bindTo($abstract, $abstract); |
139
|
|
|
$res = $bound(''); |
140
|
|
|
$this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(' ', '', $res))); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testExecutionDelayed() |
144
|
|
|
{ |
145
|
|
|
$abstract = $this->newInstance(); |
146
|
|
|
$executionDelayedCaller = function ($second, $call = null) { |
147
|
|
|
return $this->executionDelayed($second, $call); |
|
|
|
|
148
|
|
|
}; |
149
|
|
|
$bound = $executionDelayedCaller->bindTo($abstract, $abstract); |
150
|
|
|
$start = microtime(true); |
151
|
|
|
$bound(0); |
152
|
|
|
$bound(0.1); |
153
|
|
|
$timePassed = microtime(true) - $start; |
154
|
|
|
$this->assertTrue(abs($timePassed - 0.1) < 0.035); |
155
|
|
|
|
156
|
|
|
$start = microtime(true); |
157
|
|
|
$bound(0.15, function () { |
158
|
|
|
sleep(0.2); |
159
|
|
|
}); |
160
|
|
|
$bound(0.1); |
161
|
|
|
$timePassed = microtime(true) - $start; |
162
|
|
|
$this->assertTrue(abs($timePassed - 0.25) < 0.035); |
163
|
|
|
|
164
|
|
|
$start = microtime(true); |
165
|
|
|
$bound(0.15, function () { |
166
|
|
|
sleep(0.2); |
167
|
|
|
}); |
168
|
|
|
$bound(0.3); |
169
|
|
|
$timePassed = microtime(true) - $start; |
170
|
|
|
$this->assertTrue(abs($timePassed - 0.45) < 0.035); |
171
|
|
|
|
172
|
|
|
$this->assertEquals(2, $bound(0, function () { |
173
|
|
|
return 2; |
174
|
|
|
})); |
175
|
|
|
$this->assertEquals(null, $bound(0)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testGetInUrl() |
179
|
|
|
{ |
180
|
|
|
$abstract = $this->newInstance(); |
181
|
|
|
$getInUrlCaller = function () { |
182
|
|
|
return $this->getInUrl(); |
|
|
|
|
183
|
|
|
}; |
184
|
|
|
$abstract->domain = 'domain'; |
185
|
|
|
$abstract->setApiKey('123456'); |
|
|
|
|
186
|
|
|
$bound = $getInUrlCaller->bindTo($abstract, $abstract); |
187
|
|
|
$this->assertEquals('http://domain/in.php', $bound()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
192
|
|
|
* @expectedExceptionCode 4 |
193
|
|
|
*/ |
194
|
|
|
public function testIsError() |
195
|
|
|
{ |
196
|
|
|
$abstract = $this->newInstance(); |
197
|
|
|
$isErrorCaller = function ($val) { |
198
|
|
|
return $this->isError($val); |
|
|
|
|
199
|
|
|
}; |
200
|
|
|
$bound = $isErrorCaller->bindTo($abstract, $abstract); |
201
|
|
|
$bound('ERROR_IP_NOT_ALLOWED'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testIsErrorNot() |
205
|
|
|
{ |
206
|
|
|
$abstract = $this->newInstance(); |
207
|
|
|
$isErrorCaller = function ($val) { |
208
|
|
|
return $this->isError($val); |
|
|
|
|
209
|
|
|
}; |
210
|
|
|
$bound = $isErrorCaller->bindTo($abstract, $abstract); |
211
|
|
|
$this->assertNull($bound('BALANCE:56')); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
216
|
|
|
* @expectedExceptionCode 17 |
217
|
|
|
* @expectedExceptionMessage Ошибка CURL: Could |
218
|
|
|
*/ |
219
|
|
|
public function testGetCurlResponseError() |
220
|
|
|
{ |
221
|
|
|
$abstract = $this->newInstance(); |
222
|
|
|
$abstract->domain = 'domain'; |
223
|
|
|
$getCurlResponseCaller = function ($val) { |
224
|
|
|
return $this->getCurlResponse($val); |
|
|
|
|
225
|
|
|
}; |
226
|
|
|
$bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
227
|
|
|
$bound(['protected' => 'value']); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testGetCurlResponse() |
231
|
|
|
{ |
232
|
|
|
$abstract = $this->newInstance(); |
233
|
|
|
$abstract->domain = 'httpbin.org'; |
234
|
|
|
$getCurlResponseCaller = function ($val) { |
235
|
|
|
$this->inUrl = 'post'; |
|
|
|
|
236
|
|
|
|
237
|
|
|
return $this->getCurlResponse($val); |
|
|
|
|
238
|
|
|
}; |
239
|
|
|
$bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
240
|
|
|
$data = $bound(['protected' => 'value']); |
241
|
|
|
$data = json_decode($data, true); |
242
|
|
|
$this->assertEquals(['protected' => 'value'], $data['form']); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.