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