Completed
Push — master ( b6da7b...a9dd77 )
by Владислав
09:27 queued 07:18
created

DeCaptchaAbstractTest::testGetResponse()   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
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 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...
6
    {
7
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
8
//        $foo->expects($this->any())
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% 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...
9
//            ->method("baz")
10
//            ->will($this->returnValue("You called baz!"));
11
        $getBaseUrlCaller = function () {
12
            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...
13
        };
14
        $abstract->domain = 'domain';
0 ignored issues
show
Bug introduced by
Accessing domain 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
        $bound = $getBaseUrlCaller->bindTo($abstract, $abstract);
16
        $this->assertEquals('http://domain/', $bound());
17
    }
18
19
    public function testSetApiKey()
20
    {
21
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
22
        $abstract->setApiKey('123456val');
23
        $apiKeyValCaller = function () {
24
            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...
25
        };
26
        $bound = $apiKeyValCaller->bindTo($abstract, $abstract);
27
        $this->assertEquals('123456val', $bound());
28
29
        $abstract->setApiKey(function () {
30
            return '123456' . 'fun';
31
        });
32
        $apiKeyFunCaller = function () {
33
            return $this->apiKey;
34
        };
35
        $bound = $apiKeyFunCaller->bindTo($abstract, $abstract);
36
        $this->assertEquals('123456fun', $bound());
37
    }
38
39
    public function testGetActionUrl()
40
    {
41
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
42
        $getBaseUrlGetCodeCaller = function () {
43
            $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...
44
            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...
45
        };
46
        $getBaseUrlGetBalanceCaller = function () {
47
            $this->captchaId = 234;
48
            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...
49
        };
50
        $abstract->domain = 'domain';
0 ignored issues
show
Bug introduced by
Accessing domain 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...
51
        $abstract->setApiKey('123456');
52
        $bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract);
53
        $this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound());
54
        $bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract);
55
        $this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound());
56
    }
57
58
    public function testGetFilePath()
59
    {
60
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
61
        $getFilePathCaller = function ($val) {
62
            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...
63
        };
64
        $bound = $getFilePathCaller->bindTo($abstract, $abstract);
65
        $this->assertEquals(__DIR__ . '/data/Captcha.jpg', $bound(__DIR__ . '/data/Captcha.jpg'));
66
        $filePathUpload = $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha.jpg');
67
        $file1 = file_get_contents(__DIR__ . '/data/Captcha.jpg');
68
        $file2 = file_get_contents($filePathUpload);
69
        $this->assertEquals($file1, $file2);
70
    }
71
72
    /**
73
     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
74
     * @expectedExceptionCode 16
75
     */
76 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...
77
    {
78
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
79
        $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...
80
        $getFilePathCaller = function ($val) {
81
            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...
82
        };
83
        $bound = $getFilePathCaller->bindTo($abstract, $abstract);
84
        $bound(__DIR__ . '/data/Captcha1.jpg');
85
    }
86
87
    /**
88
     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
89
     * @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123
90
     * @expectedExceptionCode 15
91
     */
92 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...
93
    {
94
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
95
        $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...
96
        $getFilePathCaller = function ($val) {
97
            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...
98
        };
99
        $bound = $getFilePathCaller->bindTo($abstract, $abstract);
100
        $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123');
101
    }
102
103
    public function testGetResponse()
104
    {
105
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
106
        $abstract->domain = 'echo.jsontest.com/aaa/bbb';
0 ignored issues
show
Bug introduced by
Accessing domain 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...
107
        $getResponseCaller = function ($val) {
108
            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...
109
        };
110
        $bound = $getResponseCaller->bindTo($abstract, $abstract);
111
        $res = $bound('');
112
        $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(" ", '', $res)));
113
    }
114
115
    public function testExecutionDelayed()
116
    {
117
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
118
        $executionDelayedCaller = function ($second, $call = null) {
119
            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...
120
        };
121
        $bound = $executionDelayedCaller->bindTo($abstract, $abstract);
122
        $start = microtime(true);
123
        $bound(0);
124
        $bound(0.1);
125
        $timePassed = microtime(true) - $start;
126
        $this->assertTrue(abs($timePassed - 0.1) < 0.025);
127
128
        $start = microtime(true);
129
        $bound(0.15, function () {
130
            sleep(0.2);
131
        });
132
        $bound(0.1);
133
        $timePassed = microtime(true) - $start;
134
        $this->assertTrue(abs($timePassed - 0.25) < 0.025);
135
136
        $start = microtime(true);
137
        $bound(0.15, function () {
138
            sleep(0.2);
139
        });
140
        $bound(0.3);
141
        $timePassed = microtime(true) - $start;
142
        $this->assertTrue(abs($timePassed - 0.45) < 0.025);
143
144
        $this->assertEquals(2, $bound(0, function () {
145
            return 2;
146
        }));
147
        $this->assertEquals(null, $bound(0));
148
    }
149
150 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...
151
    {
152
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
153
        $getInUrlCaller = function () {
154
            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...
155
        };
156
        $abstract->domain = 'domain';
0 ignored issues
show
Bug introduced by
Accessing domain 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...
157
        $abstract->setApiKey('123456');
158
        $bound = $getInUrlCaller->bindTo($abstract, $abstract);
159
        $this->assertEquals('http://domain/in.php', $bound());
160
    }
161
162
    /**
163
     * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors
164
     * @expectedExceptionCode 4
165
     */
166 View Code Duplication
    public function testIsError()
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...
167
    {
168
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
169
        $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...
170
        $isErrorCaller = function ($val) {
171
            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...
172
        };
173
        $bound = $isErrorCaller->bindTo($abstract, $abstract);
174
        $bound('ERROR_IP_NOT_ALLOWED');
175
    }
176
177 View Code Duplication
    public function testIsErrorNot()
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...
178
    {
179
        $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class);
180
        $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...
181
        $isErrorCaller = function ($val) {
182
            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...
183
        };
184
        $bound = $isErrorCaller->bindTo($abstract, $abstract);
185
        $this->assertNull($bound('BALANCE:56'));
186
    }
187
}