Bitrix24   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 71
dl 0
loc 169
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructCorrectInstanceWithCustomUri() 0 11 1
A testGetAuthorizationMethod() 0 24 1
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 14 1
A testGetExtraOAuthHeaders() 0 19 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 16 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 16 1
A testGetAccessTokenEndpoint() 0 11 1
A testGetAuthorizationEndpoint() 0 11 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\Bitrix24;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OAuthTest\Unit\OAuth2\Service\Bitrix24. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use PHPUnit\Framework\Assert;
8
use PHPUnit\Framework\TestCase;
9
10
class Bitrix24 extends TestCase
11
{
12
    /**
13
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
14
     */
15
    public function testConstructCorrectInstanceWithCustomUri(): void
16
    {
17
        $service = new self(
18
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
19
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
20
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
21
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
22
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
23
        );
24
25
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
26
    }
27
28
    /**
29
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
30
     * @covers \OAuth\OAuth2\Service\Bitrix24::getAuthorizationEndpoint
31
     */
32
    public function testGetAuthorizationEndpoint(): void
33
    {
34
        $service = new self(
35
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
36
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
37
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
38
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
39
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
40
        );
41
42
        self::assertSame('https://bitrix24.com/oauth/authorize/', $service->getAuthorizationEndpoint()->getAbsoluteUri());
0 ignored issues
show
Bug introduced by
The method getAuthorizationEndpoint() does not exist on OAuthTest\Unit\OAuth2\Service\Bitrix24. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        self::assertSame('https://bitrix24.com/oauth/authorize/', $service->/** @scrutinizer ignore-call */ getAuthorizationEndpoint()->getAbsoluteUri());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
    }
44
45
    /**
46
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
47
     * @covers \OAuth\OAuth2\Service\Bitrix24::getAccessTokenEndpoint
48
     */
49
    public function testGetAccessTokenEndpoint(): void
50
    {
51
        $service = new self(
52
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
53
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
54
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
55
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
56
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
57
        );
58
59
        self::assertSame('https://bitrix24.com/oauth/token/', $service->getAccessTokenEndpoint()->getAbsoluteUri());
0 ignored issues
show
Bug introduced by
The method getAccessTokenEndpoint() does not exist on OAuthTest\Unit\OAuth2\Service\Bitrix24. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        self::assertSame('https://bitrix24.com/oauth/token/', $service->/** @scrutinizer ignore-call */ getAccessTokenEndpoint()->getAbsoluteUri());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
    }
61
62
    /**
63
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
64
     * @covers \OAuth\OAuth2\Service\Bitrix24::getAuthorizationMethod
65
     */
66
    public function testGetAuthorizationMethod(): void
67
    {
68
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
69
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0);
70
71
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
72
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
73
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
74
75
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
76
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
77
78
        $service = new self(
79
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
80
            $client,
81
            $storage,
82
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
84
        );
85
86
        $uri = $service->request('https://pieterhordijk.com/my/awesome/path');
0 ignored issues
show
Bug introduced by
The method request() does not exist on OAuthTest\Unit\OAuth2\Service\Bitrix24. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        /** @scrutinizer ignore-call */ 
87
        $uri = $service->request('https://pieterhordijk.com/my/awesome/path');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
        $absoluteUri = parse_url($uri->getAbsoluteUri());
88
89
        self::assertSame('access_token=foo', $absoluteUri['query']);
90
    }
91
92
    /**
93
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
94
     * @covers \OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
95
     */
96
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void
97
    {
98
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
99
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
100
101
        $service = new self(
102
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
103
            $client,
104
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
105
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
106
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
107
        );
108
109
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
110
111
        $service->requestAccessToken('foo');
0 ignored issues
show
Bug introduced by
The method requestAccessToken() does not exist on OAuthTest\Unit\OAuth2\Service\Bitrix24. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $service->/** @scrutinizer ignore-call */ 
112
                  requestAccessToken('foo');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
    }
113
114
    /**
115
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
116
     * @covers \OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
117
     */
118
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
119
    {
120
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
121
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"error":"some_error"}');
122
123
        $service = new self(
124
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
125
            $client,
126
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
127
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
128
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
129
        );
130
131
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
132
133
        $service->requestAccessToken('foo');
134
    }
135
136
    /**
137
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
138
     * @covers \OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
139
     */
140
    public function testParseAccessTokenResponseValidWithoutRefreshToken(): void
141
    {
142
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
143
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}');
144
145
        $service = new self(
146
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
147
            $client,
148
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
149
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
150
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
151
        );
152
153
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
154
    }
155
156
    /**
157
     * @covers \OAuth\OAuth2\Service\Bitrix24::__construct
158
     * @covers \OAuth\OAuth2\Service\Bitrix24::getExtraOAuthHeaders
159
     */
160
    public function testGetExtraOAuthHeaders(): void
161
    {
162
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
163
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($uri, $params, $extraHeaders) {
164
            Assert::assertTrue(array_key_exists('Accept', $extraHeaders));
165
            Assert::assertTrue(in_array('application/json', $extraHeaders, true));
166
167
            return '{"access_token":"foo","expires_in":"bar"}';
168
        });
169
170
        $service = new self(
171
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
172
            $client,
173
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
174
            [],
0 ignored issues
show
Unused Code introduced by
The call to OAuthTest\Unit\OAuth2\Se...Bitrix24::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
        $service = /** @scrutinizer ignore-call */ new self(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
175
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
176
        );
177
178
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
179
    }
180
}
181