-
Notifications
You must be signed in to change notification settings - Fork 26
/
AccessTokenProviderTest.php
138 lines (108 loc) · 4.28 KB
/
AccessTokenProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace MatthiasNoback\Tests\MicrosoftOAuth;
use MatthiasNoback\MicrosoftOAuth\AccessTokenProvider;
class AccessTokenProviderTest extends \PHPUnit_Framework_TestCase
{
public function testGetTokenWithoutCache()
{
$clientId = 'theClientId';
$clientSecret = 'theClientSecret';
$scope = 'theScope';
$grantType = 'theGrantType';
$accessToken = 'accessToken';
$response = $this->createMockResponse('{"access_token":"'.$accessToken.'"}');
$browser = $this->createMockBrowser();
$browser
->expects($this->once())
->method('post')
->with(
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
array(),
'client_id='.$clientId.'&client_secret='.$clientSecret.'&scope='.$scope.'&grant_type='.$grantType
)
->will($this->returnValue($response));
$accessTokenProvider = new AccessTokenProvider($browser, $clientId, $clientSecret);
$actualAccessToken = $accessTokenProvider->getAccessToken($scope, $grantType);
$this->assertSame($accessToken, $actualAccessToken);
}
public function testGetTokenWithCacheMiss()
{
$scope = 'theScope';
$grantType = 'theGrantType';
$accessToken = 'accessToken';
// the cache does not contain an access token yet
$accessTokenCache = $this->createMockAccessTokenCache();
$accessTokenCache
->expects($this->once())
->method('has')
->with($scope, $grantType)
->will($this->returnValue(false));
// the browser will used to fetch a fresh access token
$response = $this->createMockResponse('{"access_token":"'.$accessToken.'"}');
$browser = $this->createMockBrowser();
$browser
->expects($this->once())
->method('post')
->will($this->returnValue($response));
// finally, the access token should be stored in the cache
$accessTokenCache
->expects($this->once())
->method('set')
->with($scope, $grantType, $accessToken);
$accessTokenProvider = new AccessTokenProvider($browser, 'theClientId', 'theClientSecret');
$accessTokenProvider->setCache($accessTokenCache);
$actualAccessToken = $accessTokenProvider->getAccessToken($scope, $grantType);
$this->assertSame($accessToken, $actualAccessToken);
}
public function testGetTokenWithCacheHit()
{
$scope = 'theScope';
$grantType = 'theGrantType';
$accessToken = 'accessToken';
// the cache already contains an access token
$accessTokenCache = $this->createMockAccessTokenCache();
$accessTokenCache
->expects($this->once())
->method('has')
->with($scope, $grantType)
->will($this->returnValue(true));
// the browser should not be used
$browser = $this->createMockBrowser();
$browser
->expects($this->never())
->method('post');
// the access token will be retrieved from the cache
$accessTokenCache
->expects($this->once())
->method('get')
->with($scope, $grantType)
->will($this->returnValue($accessToken));
$accessTokenProvider = new AccessTokenProvider($browser, 'theClientId', 'theClientSecret');
$accessTokenProvider->setCache($accessTokenCache);
$accessTokenProvider->getAccessToken($scope, $grantType);
}
private function createMockBrowser()
{
return $this
->getMockBuilder('Buzz\\Browser')
->disableOriginalConstructor()
->getMock();
}
private function createMockResponse($content)
{
$response = $this->getMock('Buzz\Message\Response');
$response
->expects($this->any())
->method('getContent')
->will($this->returnValue($content));
$response
->expects($this->any())
->method('isSuccessful')
->will($this->returnValue(true));
return $response;
}
private function createMockAccessTokenCache()
{
return $this->getMock('MatthiasNoback\MicrosoftOAuth\AccessTokenCacheInterface');
}
}