Completed
Push — master ( 90d111...2a2a91 )
by Владислав
02:09
created

DeCaptchaAbstractTest::testIsErrorNot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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