1<?php
2
3use GuzzleHttp\Client;
4
5/**
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements.  See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership.  The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License.  You may obtain a copy of the License at
13 *
14 *     http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied.  See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 */
23
24class Google_AccessToken_RevokeTest extends BaseTest
25{
26  public function testRevokeAccessGuzzle5()
27  {
28    $this->onlyGuzzle5();
29
30    $accessToken = 'ACCESS_TOKEN';
31    $refreshToken = 'REFRESH_TOKEN';
32    $token = '';
33
34    $response = $this->getMock('GuzzleHttp\Message\ResponseInterface');
35    $response->expects($this->exactly(3))
36      ->method('getStatusCode')
37      ->will($this->returnValue(200));
38    $http = $this->getMock('GuzzleHttp\ClientInterface');
39    $http->expects($this->exactly(3))
40      ->method('send')
41      ->will($this->returnCallback(
42            function ($request) use (&$token, $response) {
43              parse_str((string) $request->getBody(), $fields);
44              $token = isset($fields['token']) ? $fields['token'] : null;
45
46              return $response;
47            }
48        ));
49
50    $requestToken = null;
51    $request = $this->getMock('GuzzleHttp\Message\RequestInterface');
52    $request->expects($this->exactly(3))
53        ->method('getBody')
54        ->will($this->returnCallback(
55            function () use (&$requestToken) {
56              return 'token='.$requestToken;
57            }));
58    $http->expects($this->exactly(3))
59      ->method('createRequest')
60      ->will($this->returnCallback(
61            function ($method, $url, $params) use (&$requestToken, $request) {
62              parse_str((string) $params['body'], $fields);
63              $requestToken = isset($fields['token']) ? $fields['token'] : null;
64
65              return $request;
66            }
67        ));
68
69    $t = array(
70      'access_token' => $accessToken,
71      'created' => time(),
72      'expires_in' => '3600'
73    );
74
75    // Test with access token.
76    $revoke = new Google_AccessToken_Revoke($http);
77    $this->assertTrue($revoke->revokeToken($t));
78    $this->assertEquals($accessToken, $token);
79
80    // Test with refresh token.
81    $revoke = new Google_AccessToken_Revoke($http);
82    $t = array(
83      'access_token' => $accessToken,
84      'refresh_token' => $refreshToken,
85      'created' => time(),
86      'expires_in' => '3600'
87    );
88    $this->assertTrue($revoke->revokeToken($t));
89    $this->assertEquals($refreshToken, $token);
90
91    // Test with token string.
92    $revoke = new Google_AccessToken_Revoke($http);
93    $t = $accessToken;
94    $this->assertTrue($revoke->revokeToken($t));
95    $this->assertEquals($accessToken, $token);
96  }
97
98  public function testRevokeAccessGuzzle6()
99  {
100    $this->onlyGuzzle6();
101
102    $accessToken = 'ACCESS_TOKEN';
103    $refreshToken = 'REFRESH_TOKEN';
104    $token = '';
105
106    $response = $this->getMock('Psr\Http\Message\ResponseInterface');
107    $response->expects($this->exactly(3))
108      ->method('getStatusCode')
109      ->will($this->returnValue(200));
110    $http = $this->getMock('GuzzleHttp\ClientInterface');
111    $http->expects($this->exactly(3))
112      ->method('send')
113      ->will($this->returnCallback(
114            function ($request) use (&$token, $response) {
115              parse_str((string) $request->getBody(), $fields);
116              $token = isset($fields['token']) ? $fields['token'] : null;
117
118              return $response;
119            }
120        ));
121
122    $t = array(
123      'access_token' => $accessToken,
124      'created' => time(),
125      'expires_in' => '3600'
126    );
127
128    // Test with access token.
129    $revoke = new Google_AccessToken_Revoke($http);
130    $this->assertTrue($revoke->revokeToken($t));
131    $this->assertEquals($accessToken, $token);
132
133    // Test with refresh token.
134    $revoke = new Google_AccessToken_Revoke($http);
135    $t = array(
136      'access_token' => $accessToken,
137      'refresh_token' => $refreshToken,
138      'created' => time(),
139      'expires_in' => '3600'
140    );
141    $this->assertTrue($revoke->revokeToken($t));
142    $this->assertEquals($refreshToken, $token);
143
144    // Test with token string.
145    $revoke = new Google_AccessToken_Revoke($http);
146    $t = $accessToken;
147    $this->assertTrue($revoke->revokeToken($t));
148    $this->assertEquals($accessToken, $token);
149  }
150}
151