Completed
Pull Request — master (#479)
by Andrey
03:26
created

Bitrix24Test   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 171
rs 10
c 1
b 0
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructCorrectInstanceWithCustomUri() 0 12 1
A testGetAuthorizationEndpoint() 0 12 1
A testGetAccessTokenEndpoint() 0 12 1
B testGetAuthorizationMethod() 0 25 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 17 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 17 1
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 15 1
A testGetExtraOAuthHeaders() 0 20 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\OAuth2\Service\Bitrix24;
6
use OAuth\Common\Token\TokenInterface;
7
8
class Bitrix24Test extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
12
     */
13
    public function testConstructCorrectInstanceWithCustomUri()
14
    {
15
        $service = new Bitrix24(
16
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
17
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
18
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
19
            array(),
20
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
21
        );
22
23
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
24
    }
25
26
    /**
27
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
28
     * @covers OAuth\OAuth2\Service\Bitrix24::getAuthorizationEndpoint
29
     */
30
    public function testGetAuthorizationEndpoint()
31
    {
32
        $service = new Bitrix24(
33
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
34
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
35
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
36
            array(),
37
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
38
        );
39
40
        $this->assertSame('https://bitrix24.com/oauth/authorize/', $service->getAuthorizationEndpoint()->getAbsoluteUri());
41
    }
42
43
    /**
44
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
45
     * @covers OAuth\OAuth2\Service\Bitrix24::getAccessTokenEndpoint
46
     */
47
    public function testGetAccessTokenEndpoint()
48
    {
49
        $service = new Bitrix24(
50
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
51
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
52
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
53
            array(),
54
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
55
        );
56
57
        $this->assertSame('https://bitrix24.com/oauth/token/', $service->getAccessTokenEndpoint()->getAbsoluteUri());
58
    }
59
60
    /**
61
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
62
     * @covers OAuth\OAuth2\Service\Bitrix24::getAuthorizationMethod
63
     */
64
    public function testGetAuthorizationMethod()
65
    {
66
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
67
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
68
69
        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
70
        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
71
        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
72
73
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
74
        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
75
76
        $service = new Bitrix24(
77
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
78
            $client,
79
            $storage,
80
            array(),
81
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
82
        );
83
84
        $uri         = $service->request('https://pieterhordijk.com/my/awesome/path');
85
        $absoluteUri = parse_url($uri->getAbsoluteUri());
0 ignored issues
show
Bug introduced by
The method getAbsoluteUri cannot be called on $uri (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
86
87
        $this->assertSame('access_token=foo', $absoluteUri['query']);
88
    }
89
90
    /**
91
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
92
     * @covers OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
93
     */
94
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
95
    {
96
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
97
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
98
99
        $service = new Bitrix24(
100
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
101
            $client,
102
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
103
            array(),
104
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
105
        );
106
107
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
108
109
        $service->requestAccessToken('foo');
110
    }
111
112
    /**
113
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
114
     * @covers OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
115
     */
116
    public function testParseAccessTokenResponseThrowsExceptionOnError()
117
    {
118
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
119
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"error":"some_error"}'));
120
121
        $service = new Bitrix24(
122
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
123
            $client,
124
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
125
            array(),
126
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
127
        );
128
129
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
130
131
        $service->requestAccessToken('foo');
132
    }
133
134
    /**
135
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
136
     * @covers OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
137
     */
138
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
139
    {
140
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
141
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
142
143
        $service = new Bitrix24(
144
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
145
            $client,
146
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
147
            array(),
148
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
149
        );
150
151
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
152
    }
153
154
    /**
155
     * @covers OAuth\OAuth2\Service\Bitrix24::__construct
156
     * @covers OAuth\OAuth2\Service\Bitrix24::getExtraOAuthHeaders
157
     */
158
    public function testGetExtraOAuthHeaders()
159
    {
160
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
161
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($uri, $params, $extraHeaders) {
162
            \PHPUnit_Framework_Assert::assertTrue(array_key_exists('Accept', $extraHeaders));
163
            \PHPUnit_Framework_Assert::assertTrue(in_array('application/json', $extraHeaders, true));
164
165
            return '{"access_token":"foo","expires_in":"bar"}';
166
        }));
167
168
        $service = new Bitrix24(
169
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
170
            $client,
171
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
172
            array(),
173
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
174
        );
175
176
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
177
    }
178
}
179