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->setErrorLang(\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
|
|
|
$this->host = 'domain'; |
24
|
|
|
|
25
|
|
|
return $this->getBaseUrl(); |
26
|
|
|
}; |
27
|
|
|
$bound = $getBaseUrlCaller->bindTo($abstract, $abstract); |
28
|
|
|
$this->assertEquals('http://domain/', $bound()); |
29
|
|
|
|
30
|
|
|
$abstract = $this->newInstance(); |
31
|
|
|
$getBaseUrlCaller = function () { |
32
|
|
|
$this->host = 'domain'; |
33
|
|
|
$this->scheme = 'https'; |
34
|
|
|
|
35
|
|
|
return $this->getBaseUrl(); |
36
|
|
|
}; |
37
|
|
|
$bound = $getBaseUrlCaller->bindTo($abstract, $abstract); |
38
|
|
|
$this->assertEquals('https://domain/', $bound()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// public function testGetActionUrl() |
42
|
|
|
// { |
43
|
|
|
// $abstract = $this->newInstance(); |
44
|
|
|
// $getBaseUrlGetCodeCaller = function () { |
45
|
|
|
// $this->captchaId = 123; |
46
|
|
|
// |
47
|
|
|
// return $this->getActionUrl('get_code'); |
48
|
|
|
// }; |
49
|
|
|
// $getBaseUrlGetBalanceCaller = function ($action, $key, $id) { |
50
|
|
|
// $this->domain = 'domain'; |
51
|
|
|
// $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY, $key); |
52
|
|
|
// $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_CAPTCHA, $id); |
53
|
|
|
// return $this->getActionUrl('get_balance'); |
54
|
|
|
// }; |
55
|
|
|
// $abstract->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY '123456'); |
56
|
|
|
// $bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract); |
57
|
|
|
// $this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound()); |
58
|
|
|
// $bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract); |
59
|
|
|
// $this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound()); |
60
|
|
|
// } |
61
|
|
|
|
62
|
|
|
public function testGetFilePath() |
63
|
|
|
{ |
64
|
|
|
$abstract = $this->newInstance(); |
65
|
|
|
$getFilePathCaller = function ($val) { |
66
|
|
|
return $this->getFilePath($val); |
67
|
|
|
}; |
68
|
|
|
$bound = $getFilePathCaller->bindTo($abstract, $abstract); |
69
|
|
|
$this->assertEquals(__DIR__.'/data/Captcha.jpg', $bound(__DIR__.'/data/Captcha.jpg')); |
70
|
|
|
$filePathUpload = $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha.jpg'); |
71
|
|
|
$file1 = file_get_contents(__DIR__.'/data/Captcha.jpg'); |
72
|
|
|
$file2 = file_get_contents($filePathUpload); |
73
|
|
|
$this->assertEquals($file1, $file2); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
78
|
|
|
* @expectedExceptionCode 16 |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function testGetFilePathErrorFileNotFound() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$abstract = $this->newInstance(); |
83
|
|
|
$getFilePathCaller = function ($val) { |
84
|
|
|
return $this->getFilePath($val); |
85
|
|
|
}; |
86
|
|
|
$bound = $getFilePathCaller->bindTo($abstract, $abstract); |
87
|
|
|
$bound(__DIR__.'/data/Captcha1.jpg'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
92
|
|
|
* @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
93
|
|
|
* @expectedExceptionCode 15 |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function testGetFilePathErrorFileIsNotLoaded() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$abstract = $this->newInstance(); |
98
|
|
|
$getFilePathCaller = function ($val) { |
99
|
|
|
return $this->getFilePath($val); |
100
|
|
|
}; |
101
|
|
|
$bound = $getFilePathCaller->bindTo($abstract, $abstract); |
102
|
|
|
$bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// public function testGetResponse() |
106
|
|
|
// { |
107
|
|
|
// $abstract = $this->newInstance(); |
108
|
|
|
// $getResponseCaller = function ($val) { |
109
|
|
|
// $this->domain = 'echo.jsontest.com/aaa/bbb'; |
110
|
|
|
// return $this->getResponse($val); |
111
|
|
|
// }; |
112
|
|
|
// $bound = $getResponseCaller->bindTo($abstract, $abstract); |
113
|
|
|
// $res = $bound(''); |
114
|
|
|
// $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(' ', '', $res))); |
115
|
|
|
// } |
116
|
|
|
|
117
|
|
|
public function testExecutionDelayed() |
118
|
|
|
{ |
119
|
|
|
$abstract = $this->newInstance(); |
120
|
|
|
$executionDelayedCaller = function ($second, $call = null) { |
121
|
|
|
return $this->executionDelayed($second, $call); |
122
|
|
|
}; |
123
|
|
|
$bound = $executionDelayedCaller->bindTo($abstract, $abstract); |
124
|
|
|
$start = microtime(true); |
125
|
|
|
$bound(0); |
126
|
|
|
$bound(0.1); |
127
|
|
|
$timePassed = microtime(true) - $start; |
128
|
|
|
$this->assertTrue(abs($timePassed - 0.1) < 0.055); |
129
|
|
|
|
130
|
|
|
$start = microtime(true); |
131
|
|
|
$bound(0.15, function () { |
132
|
|
|
sleep(0.2); |
133
|
|
|
}); |
134
|
|
|
$bound(0.1); |
135
|
|
|
$timePassed = microtime(true) - $start; |
136
|
|
|
$this->assertTrue(abs($timePassed - 0.25) < 0.055); |
137
|
|
|
|
138
|
|
|
$start = microtime(true); |
139
|
|
|
$bound(0.15, function () { |
140
|
|
|
sleep(0.2); |
141
|
|
|
}); |
142
|
|
|
$bound(0.3); |
143
|
|
|
$timePassed = microtime(true) - $start; |
144
|
|
|
$this->assertTrue(abs($timePassed - 0.45) < 0.055); |
145
|
|
|
|
146
|
|
|
$this->assertEquals(2, $bound(0, function () { |
147
|
|
|
return 2; |
148
|
|
|
})); |
149
|
|
|
$this->assertEquals(null, $bound(0)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// public function testGetInUrl() |
153
|
|
|
// { |
154
|
|
|
// $abstract = $this->newInstance(); |
155
|
|
|
// $getInUrlCaller = function () { |
156
|
|
|
// $this->domain = 'domain'; |
157
|
|
|
// return $this->getInUrl(); |
158
|
|
|
// }; |
159
|
|
|
// $abstract->setApiKey('123456'); |
160
|
|
|
// $bound = $getInUrlCaller->bindTo($abstract, $abstract); |
161
|
|
|
// $this->assertEquals('http://domain/in.php', $bound()); |
162
|
|
|
// } |
163
|
|
|
|
164
|
|
|
// /** |
165
|
|
|
// * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
166
|
|
|
// * @expectedExceptionCode 4 |
167
|
|
|
// */ |
168
|
|
|
// public function testIsError() |
169
|
|
|
// { |
170
|
|
|
// $abstract = $this->newInstance(); |
171
|
|
|
// $isErrorCaller = function ($val) { |
172
|
|
|
// return $this->isError($val); |
173
|
|
|
// }; |
174
|
|
|
// $bound = $isErrorCaller->bindTo($abstract, $abstract); |
175
|
|
|
// $bound('ERROR_IP_NOT_ALLOWED'); |
176
|
|
|
// } |
177
|
|
|
|
178
|
|
|
// public function testIsErrorNot() |
179
|
|
|
// { |
180
|
|
|
// $abstract = $this->newInstance(); |
181
|
|
|
// $isErrorCaller = function ($val) { |
182
|
|
|
// return $this->isError($val); |
183
|
|
|
// }; |
184
|
|
|
// $bound = $isErrorCaller->bindTo($abstract, $abstract); |
185
|
|
|
// $this->assertNull($bound('BALANCE:56')); |
186
|
|
|
// } |
187
|
|
|
|
188
|
|
|
// /** |
189
|
|
|
// * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
190
|
|
|
// * @expectedExceptionCode 17 |
191
|
|
|
// * @expectedExceptionMessage Ошибка CURL: Could |
192
|
|
|
// */ |
193
|
|
|
// public function testGetCurlResponseError() |
194
|
|
|
// { |
195
|
|
|
// $abstract = $this->newInstance(); |
196
|
|
|
// $getCurlResponseCaller = function ($url, $val) { |
197
|
|
|
// return $this->getCurlResponse($url, $val); |
198
|
|
|
// }; |
199
|
|
|
// $bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
200
|
|
|
// $bound('http://domain', ['protected' => 'value']); |
201
|
|
|
// } |
202
|
|
|
// |
203
|
|
|
// public function testGetCurlResponse() |
204
|
|
|
// { |
205
|
|
|
// $abstract = $this->newInstance(); |
206
|
|
|
// $getCurlResponseCaller = function ($url, $val) { |
207
|
|
|
// return $this->getCurlResponse($url, $val); |
208
|
|
|
// }; |
209
|
|
|
// $bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
210
|
|
|
// $data = $bound('http://httpbin.org/post', ['protected' => 'value']); |
211
|
|
|
// $data = json_decode($data, true); |
212
|
|
|
// $this->assertEquals(['protected' => 'value'], $data['form']); |
213
|
|
|
// } |
214
|
|
|
} |
215
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.