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