Completed
Push — master ( 12b9f9...aeff6d )
by Владислав
02:07
created

DeCaptchaAbstractTest::testExecutionDelayed()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class DeCaptchaAbstractTest.
5
 */
6
class DeCaptchaAbstractTest extends PHPUnit_Framework_TestCase
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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';
0 ignored issues
show
Bug introduced by
The property domain does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
25
            return $this->getBaseUrl();
1 ignored issue
show
Bug introduced by
The method getBaseUrl() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetBaseUrl()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
26
        };
27
        $bound = $getBaseUrlCaller->bindTo($abstract, $abstract);
28
        $this->assertEquals('http://domain/', $bound());
29
    }
30
31
//    public function testGetActionUrl()
1 ignored issue
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
//    {
33
//        $abstract = $this->newInstance();
34
//        $getBaseUrlGetCodeCaller = function () {
35
//            $this->captchaId = 123;
36
//
37
//            return $this->getActionUrl('get_code');
38
//        };
39
//        $getBaseUrlGetBalanceCaller = function ($action, $key, $id) {
40
//            $this->domain = 'domain';
41
//            $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY, $key);
42
//            $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_CAPTCHA, $id);
43
//            return $this->getActionUrl('get_balance');
44
//        };
45
//        $abstract->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY '123456');
46
//        $bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract);
47
//        $this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound());
48
//        $bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract);
49
//        $this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound());
50
//    }
51
52
    public function testGetFilePath()
53
    {
54
        $abstract = $this->newInstance();
55
        $getFilePathCaller = function ($val) {
56
            return $this->getFilePath($val);
1 ignored issue
show
Bug introduced by
The method getFilePath() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetFilePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57
        };
58
        $bound = $getFilePathCaller->bindTo($abstract, $abstract);
59
        $this->assertEquals(__DIR__.'/data/Captcha.jpg', $bound(__DIR__.'/data/Captcha.jpg'));
60
        $filePathUpload = $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha.jpg');
61
        $file1 = file_get_contents(__DIR__.'/data/Captcha.jpg');
62
        $file2 = file_get_contents($filePathUpload);
63
        $this->assertEquals($file1, $file2);
64
    }
65
66
    /**
67
     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
68
     * @expectedExceptionCode 16
69
     */
70 View Code Duplication
    public function testGetFilePathErrorFileNotFound()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
71
    {
72
        $abstract = $this->newInstance();
73
        $getFilePathCaller = function ($val) {
74
            return $this->getFilePath($val);
1 ignored issue
show
Bug introduced by
The method getFilePath() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetFilePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
75
        };
76
        $bound = $getFilePathCaller->bindTo($abstract, $abstract);
77
        $bound(__DIR__.'/data/Captcha1.jpg');
78
    }
79
80
    /**
81
     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
82
     * @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123
83
     * @expectedExceptionCode 15
84
     */
85 View Code Duplication
    public function testGetFilePathErrorFileIsNotLoaded()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
86
    {
87
        $abstract = $this->newInstance();
88
        $getFilePathCaller = function ($val) {
89
            return $this->getFilePath($val);
1 ignored issue
show
Bug introduced by
The method getFilePath() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetFilePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
        };
91
        $bound = $getFilePathCaller->bindTo($abstract, $abstract);
92
        $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123');
93
    }
94
95
//    public function testGetResponse()
1 ignored issue
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
//    {
97
//        $abstract = $this->newInstance();
98
//        $getResponseCaller = function ($val) {
99
//            $this->domain = 'echo.jsontest.com/aaa/bbb';
100
//            return $this->getResponse($val);
101
//        };
102
//        $bound = $getResponseCaller->bindTo($abstract, $abstract);
103
//        $res = $bound('');
104
//        $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(' ', '', $res)));
105
//    }
106
107
    public function testExecutionDelayed()
108
    {
109
        $abstract = $this->newInstance();
110
        $executionDelayedCaller = function ($second, $call = null) {
111
            return $this->executionDelayed($second, $call);
1 ignored issue
show
Bug introduced by
The method executionDelayed() does not exist on DeCaptchaAbstractTest. Did you maybe mean testExecutionDelayed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
112
        };
113
        $bound = $executionDelayedCaller->bindTo($abstract, $abstract);
114
        $start = microtime(true);
115
        $bound(0);
116
        $bound(0.1);
117
        $timePassed = microtime(true) - $start;
118
        $this->assertTrue(abs($timePassed - 0.1) < 0.035);
119
120
        $start = microtime(true);
121
        $bound(0.15, function () {
122
            sleep(0.2);
123
        });
124
        $bound(0.1);
125
        $timePassed = microtime(true) - $start;
126
        $this->assertTrue(abs($timePassed - 0.25) < 0.035);
127
128
        $start = microtime(true);
129
        $bound(0.15, function () {
130
            sleep(0.2);
131
        });
132
        $bound(0.3);
133
        $timePassed = microtime(true) - $start;
134
        $this->assertTrue(abs($timePassed - 0.45) < 0.035);
135
136
        $this->assertEquals(2, $bound(0, function () {
137
            return 2;
138
        }));
139
        $this->assertEquals(null, $bound(0));
140
    }
141
142
//    public function testGetInUrl()
1 ignored issue
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
143
//    {
144
//        $abstract = $this->newInstance();
145
//        $getInUrlCaller = function () {
146
//            $this->domain = 'domain';
147
//            return $this->getInUrl();
148
//        };
149
//        $abstract->setApiKey('123456');
150
//        $bound = $getInUrlCaller->bindTo($abstract, $abstract);
151
//        $this->assertEquals('http://domain/in.php', $bound());
152
//    }
153
154
//    /**
1 ignored issue
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
//     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
156
//     * @expectedExceptionCode 4
157
//     */
158
//    public function testIsError()
159
//    {
160
//        $abstract = $this->newInstance();
161
//        $isErrorCaller = function ($val) {
162
//            return $this->isError($val);
163
//        };
164
//        $bound = $isErrorCaller->bindTo($abstract, $abstract);
165
//        $bound('ERROR_IP_NOT_ALLOWED');
166
//    }
167
168
//    public function testIsErrorNot()
1 ignored issue
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
169
//    {
170
//        $abstract = $this->newInstance();
171
//        $isErrorCaller = function ($val) {
172
//            return $this->isError($val);
173
//        };
174
//        $bound = $isErrorCaller->bindTo($abstract, $abstract);
175
//        $this->assertNull($bound('BALANCE:56'));
176
//    }
177
178
//    /**
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
179
//     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
180
//     * @expectedExceptionCode 17
181
//     * @expectedExceptionMessage Ошибка CURL: Could
182
//     */
183
//    public function testGetCurlResponseError()
184
//    {
185
//        $abstract = $this->newInstance();
186
//        $getCurlResponseCaller = function ($url, $val) {
187
//            return $this->getCurlResponse($url, $val);
188
//        };
189
//        $bound = $getCurlResponseCaller->bindTo($abstract, $abstract);
190
//        $bound('http://domain', ['protected' => 'value']);
191
//    }
192
//
193
//    public function testGetCurlResponse()
194
//    {
195
//        $abstract = $this->newInstance();
196
//        $getCurlResponseCaller = function ($url, $val) {
197
//            return $this->getCurlResponse($url, $val);
198
//        };
199
//        $bound = $getCurlResponseCaller->bindTo($abstract, $abstract);
200
//        $data = $bound('http://httpbin.org/post', ['protected' => 'value']);
201
//        $data = json_decode($data, true);
202
//        $this->assertEquals(['protected' => 'value'], $data['form']);
203
//    }
204
}
205