Completed
Push — master ( 2a2a91...9e8812 )
by Владислав
02:55 queued 42s
created

testGetFilePathErrorFileIsNotWriteAccessFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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
    public function testGetResponse()
115
    {
116
        $abstract = $this->newInstance();
117
        $abstract->domain = 'echo.jsontest.com/aaa/bbb';
118
        $getResponseCaller = function ($val) {
119
            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...
120
        };
121
        $bound = $getResponseCaller->bindTo($abstract, $abstract);
122
        $res = $bound('');
123
        $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(' ', '', $res)));
124
    }
125
126
    public function testExecutionDelayed()
127
    {
128
        $abstract = $this->newInstance();
129
        $executionDelayedCaller = function ($second, $call = null) {
130
            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...
131
        };
132
        $bound = $executionDelayedCaller->bindTo($abstract, $abstract);
133
        $start = microtime(true);
134
        $bound(0);
135
        $bound(0.1);
136
        $timePassed = microtime(true) - $start;
137
        $this->assertTrue(abs($timePassed - 0.1) < 0.035);
138
139
        $start = microtime(true);
140
        $bound(0.15, function () {
141
            sleep(0.2);
142
        });
143
        $bound(0.1);
144
        $timePassed = microtime(true) - $start;
145
        $this->assertTrue(abs($timePassed - 0.25) < 0.035);
146
147
        $start = microtime(true);
148
        $bound(0.15, function () {
149
            sleep(0.2);
150
        });
151
        $bound(0.3);
152
        $timePassed = microtime(true) - $start;
153
        $this->assertTrue(abs($timePassed - 0.45) < 0.035);
154
155
        $this->assertEquals(2, $bound(0, function () {
156
            return 2;
157
        }));
158
        $this->assertEquals(null, $bound(0));
159
    }
160
161
    public function testGetInUrl()
162
    {
163
        $abstract = $this->newInstance();
164
        $getInUrlCaller = function () {
165
            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...
166
        };
167
        $abstract->domain = 'domain';
168
        $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...
169
        $bound = $getInUrlCaller->bindTo($abstract, $abstract);
170
        $this->assertEquals('http://domain/in.php', $bound());
171
    }
172
173
    /**
174
     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
175
     * @expectedExceptionCode 4
176
     */
177
    public function testIsError()
178
    {
179
        $abstract = $this->newInstance();
180
        $isErrorCaller = function ($val) {
181
            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...
182
        };
183
        $bound = $isErrorCaller->bindTo($abstract, $abstract);
184
        $bound('ERROR_IP_NOT_ALLOWED');
185
    }
186
187
    public function testIsErrorNot()
188
    {
189
        $abstract = $this->newInstance();
190
        $isErrorCaller = function ($val) {
191
            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...
192
        };
193
        $bound = $isErrorCaller->bindTo($abstract, $abstract);
194
        $this->assertNull($bound('BALANCE:56'));
195
    }
196
197
    /**
198
     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
199
     * @expectedExceptionCode 17
200
     * @expectedExceptionMessage Ошибка CURL: Could
201
     */
202
    public function testGetCurlResponseError()
203
    {
204
        $abstract = $this->newInstance();
205
        $abstract->domain = 'domain';
206
        $getCurlResponseCaller = function ($val) {
207
            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...
208
        };
209
        $bound = $getCurlResponseCaller->bindTo($abstract, $abstract);
210
        $bound(['protected' => 'value']);
211
    }
212
213
    public function testGetCurlResponse()
214
    {
215
        $abstract = $this->newInstance();
216
        $abstract->domain = 'httpbin.org';
217
        $getCurlResponseCaller = function ($val) {
218
            $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...
219
220
            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...
221
        };
222
        $bound = $getCurlResponseCaller->bindTo($abstract, $abstract);
223
        $data = $bound(['protected' => 'value']);
224
        $data = json_decode($data, true);
225
        $this->assertEquals(['protected' => 'value'], $data['form']);
226
    }
227
}
228