Completed
Push — master ( 45b824...13f4b7 )
by Владислав
02:10
created

DeCaptchaAbstractTest::testGetCurlResponseError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
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...
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;
0 ignored issues
show
Bug introduced by
Accessing errorLang on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
11
        return $abstract;
12
    }
13
14 View Code Duplication
    public function testGetBaseUrl()
0 ignored issues
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...
15
    {
16
        $abstract = $this->newInstance();
17
        $getBaseUrlCaller = function() {
18
            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...
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');
0 ignored issues
show
Bug introduced by
The method setApiKey does only exist in jumper423\decaptcha\core\DeCaptchaAbstract, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
29
        $apiKeyValCaller = function() {
30
            return $this->apiKey;
0 ignored issues
show
Bug introduced by
The property apiKey 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...
31
        };
32
        $bound = $apiKeyValCaller->bindTo($abstract, $abstract);
33
        $this->assertEquals('123456val', $bound());
34
35
        $abstract->setApiKey(function() {
0 ignored issues
show
Bug introduced by
The method setApiKey does only exist in jumper423\decaptcha\core\DeCaptchaAbstract, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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;
0 ignored issues
show
Bug introduced by
The property captchaId 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...
50
            return $this->getActionUrl('get_code');
0 ignored issues
show
Bug introduced by
The method getActionUrl() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetActionUrl()?

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...
51
        };
52
        $getBaseUrlGetBalanceCaller = function() {
53
            $this->captchaId = 234;
54
            return $this->getActionUrl('get_balance');
0 ignored issues
show
Bug introduced by
The method getActionUrl() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetActionUrl()?

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...
55
        };
56
        $abstract->domain = 'domain';
57
        $abstract->setApiKey('123456');
0 ignored issues
show
Bug introduced by
The method setApiKey does only exist in jumper423\decaptcha\core\DeCaptchaAbstract, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
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...
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()
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...
83
    {
84
        $abstract = $this->newInstance();
85
        $getFilePathCaller = function($val) {
86
            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...
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()
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...
98
    {
99
        $abstract = $this->newInstance();
100
        $getFilePathCaller = function($val) {
101
            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...
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);
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetResponse()?

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...
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);
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...
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()
0 ignored issues
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...
155
    {
156
        $abstract = $this->newInstance();
157
        $getInUrlCaller = function() {
158
            return $this->getInUrl();
0 ignored issues
show
Bug introduced by
The method getInUrl() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetInUrl()?

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...
159
        };
160
        $abstract->domain = 'domain';
161
        $abstract->setApiKey('123456');
0 ignored issues
show
Bug introduced by
The method setApiKey does only exist in jumper423\decaptcha\core\DeCaptchaAbstract, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method isError() does not exist on DeCaptchaAbstractTest. Did you maybe mean testIsError()?

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...
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);
0 ignored issues
show
Bug introduced by
The method isError() does not exist on DeCaptchaAbstractTest. Did you maybe mean testIsError()?

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...
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);
0 ignored issues
show
Bug introduced by
The method getCurlResponse() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetCurlResponseError()?

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...
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';
0 ignored issues
show
Bug introduced by
The property inUrl 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...
212
            return $this->getCurlResponse($val);
0 ignored issues
show
Bug introduced by
The method getCurlResponse() does not exist on DeCaptchaAbstractTest. Did you maybe mean testGetCurlResponseError()?

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...
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
}