|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OAuthTest\Unit\OAuth2\Service; |
|
4
|
|
|
|
|
5
|
|
|
use OAuth\Common\Token\TokenInterface; |
|
6
|
|
|
use OAuth\OAuth2\Service\Bitrix24; |
|
|
|
|
|
|
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 Bitrix24( |
|
18
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
19
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
|
|
20
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
|
|
21
|
|
|
[], |
|
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 Bitrix24( |
|
35
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
36
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
|
|
37
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
|
|
38
|
|
|
[], |
|
39
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
self::assertSame('https://bitrix24.com/oauth/authorize/', $service->getAuthorizationEndpoint()->getAbsoluteUri()); |
|
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 Bitrix24( |
|
52
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
53
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
|
|
54
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
|
|
55
|
|
|
[], |
|
56
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
self::assertSame('https://bitrix24.com/oauth/token/', $service->getAccessTokenEndpoint()->getAbsoluteUri()); |
|
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 Bitrix24( |
|
79
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
80
|
|
|
$client, |
|
|
|
|
|
|
81
|
|
|
$storage, |
|
|
|
|
|
|
82
|
|
|
[], |
|
83
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$uri = $service->request('https://pieterhordijk.com/my/awesome/path'); |
|
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 Bitrix24( |
|
102
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
103
|
|
|
$client, |
|
|
|
|
|
|
104
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
|
|
105
|
|
|
[], |
|
106
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
|
110
|
|
|
|
|
111
|
|
|
$service->requestAccessToken('foo'); |
|
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 Bitrix24( |
|
124
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
125
|
|
|
$client, |
|
|
|
|
|
|
126
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
|
|
127
|
|
|
[], |
|
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 Bitrix24( |
|
146
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
147
|
|
|
$client, |
|
|
|
|
|
|
148
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
|
|
149
|
|
|
[], |
|
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 Bitrix24( |
|
171
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
|
|
172
|
|
|
$client, |
|
|
|
|
|
|
173
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
|
|
174
|
|
|
[], |
|
175
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: