BaseOAuthTest.php 6.89 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\authclient;
4

5 6
use yii\authclient\signature\PlainText;
use yii\authclient\OAuthToken;
7
use yiiunit\extensions\authclient\TestCase;
8
use yii\authclient\BaseOAuth;
9

10
class BaseOAuthTest extends TestCase
11 12 13
{
	/**
	 * Creates test OAuth client instance.
14
	 * @return BaseOAuth oauth client.
15 16 17
	 */
	protected function createOAuthClient()
	{
18
		$oauthClient = $this->getMock(BaseOAuth::className(), ['setState', 'getState', 'composeRequestCurlOptions', 'refreshAccessToken', 'apiInternal']);
19 20 21 22 23 24 25
		$oauthClient->expects($this->any())->method('setState')->will($this->returnValue($oauthClient));
		$oauthClient->expects($this->any())->method('getState')->will($this->returnValue(null));
		return $oauthClient;
	}

	/**
	 * Invokes the OAuth client method even if it is protected.
26
	 * @param BaseOAuth $oauthClient OAuth client instance.
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
	 * @param string $methodName name of the method to be invoked.
	 * @param array $arguments method arguments.
	 * @return mixed method invoke result.
	 */
	protected function invokeOAuthClientMethod($oauthClient, $methodName, array $arguments = [])
	{
		$classReflection = new \ReflectionClass(get_class($oauthClient));
		$methodReflection = $classReflection->getMethod($methodName);
		$methodReflection->setAccessible(true);
		$result = $methodReflection->invokeArgs($oauthClient, $arguments);
		$methodReflection->setAccessible(false);
		return $result;
	}

	// Tests :

	public function testSetGet()
	{
		$oauthClient = $this->createOAuthClient();

		$returnUrl = 'http://test.return.url';
		$oauthClient->setReturnUrl($returnUrl);
		$this->assertEquals($returnUrl, $oauthClient->getReturnUrl(), 'Unable to setup return URL!');

		$curlOptions = [
			'option1' => 'value1',
			'option2' => 'value2',
		];
		$oauthClient->setCurlOptions($curlOptions);
		$this->assertEquals($curlOptions, $oauthClient->getCurlOptions(), 'Unable to setup cURL options!');
	}

	public function testSetupComponents()
	{
		$oauthClient = $this->createOAuthClient();

63
		$oauthToken = new OAuthToken();
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		$oauthClient->setAccessToken($oauthToken);
		$this->assertEquals($oauthToken, $oauthClient->getAccessToken(), 'Unable to setup token!');

		$oauthSignatureMethod = new PlainText();
		$oauthClient->setSignatureMethod($oauthSignatureMethod);
		$this->assertEquals($oauthSignatureMethod, $oauthClient->getSignatureMethod(), 'Unable to setup signature method!');
	}

	/**
	 * @depends testSetupComponents
	 */
	public function testSetupComponentsByConfig()
	{
		$oauthClient = $this->createOAuthClient();

		$oauthToken = [
			'token' => 'test_token',
			'tokenSecret' => 'test_token_secret',
		];
		$oauthClient->setAccessToken($oauthToken);
		$this->assertEquals($oauthToken['token'], $oauthClient->getAccessToken()->getToken(), 'Unable to setup token as config!');

		$oauthSignatureMethod = [
87
			'class' => 'yii\authclient\signature\PlainText'
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		];
		$oauthClient->setSignatureMethod($oauthSignatureMethod);
		$returnedSignatureMethod = $oauthClient->getSignatureMethod();
		$this->assertEquals($oauthSignatureMethod['class'], get_class($returnedSignatureMethod), 'Unable to setup signature method as config!');
	}

	/**
	 * Data provider for [[testComposeUrl()]].
	 * @return array test data.
	 */
	public function composeUrlDataProvider()
	{
		return [
			[
				'http://test.url',
				[
					'param1' => 'value1',
					'param2' => 'value2',
				],
				'http://test.url?param1=value1&param2=value2',
			],
			[
				'http://test.url?with=some',
				[
					'param1' => 'value1',
					'param2' => 'value2',
				],
				'http://test.url?with=some&param1=value1&param2=value2',
			],
		];
	}

	/**
	 * @dataProvider composeUrlDataProvider
	 *
	 * @param string $url request URL.
	 * @param array $params request params
	 * @param string $expectedUrl expected composed URL.
	 */
	public function testComposeUrl($url, array $params, $expectedUrl)
	{
		$oauthClient = $this->createOAuthClient();
		$composedUrl = $this->invokeOAuthClientMethod($oauthClient, 'composeUrl', [$url, $params]);
		$this->assertEquals($expectedUrl, $composedUrl);
	}

	/**
	 * Data provider for {@link testDetermineContentTypeByHeaders}.
	 * @return array test data.
	 */
	public function determineContentTypeByHeadersDataProvider()
	{
		return [
			[
				['content_type' => 'application/json'],
				'json'
			],
			[
				['content_type' => 'application/x-www-form-urlencoded'],
				'urlencoded'
			],
			[
				['content_type' => 'application/xml'],
				'xml'
			],
			[
				['some_header' => 'some_header_value'],
				'auto'
			],
			[
				['content_type' => 'unknown'],
				'auto'
			],
		];
	}

	/**
	 * @dataProvider determineContentTypeByHeadersDataProvider
	 *
	 * @param array $headers request headers.
	 * @param string $expectedResponseType expected response type.
	 */
	public function testDetermineContentTypeByHeaders(array $headers, $expectedResponseType)
	{
		$oauthClient = $this->createOAuthClient();
		$responseType = $this->invokeOAuthClientMethod($oauthClient, 'determineContentTypeByHeaders', [$headers]);
		$this->assertEquals($expectedResponseType, $responseType);
	}

	/**
	 * Data provider for [[testDetermineContentTypeByRaw]].
	 * @return array test data.
	 */
	public function determineContentTypeByRawDataProvider()
	{
Luciano Baraglia committed
183
		return [
184 185 186 187 188
			['{name: value}', 'json'],
			['name=value', 'urlencoded'],
			['name1=value1&name2=value2', 'urlencoded'],
			['<?xml version="1.0" encoding="UTF-8"?><tag>Value</tag>', 'xml'],
			['<tag>Value</tag>', 'xml'],
Luciano Baraglia committed
189
		];
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	}

	/**
	 * @dataProvider determineContentTypeByRawDataProvider
	 *
	 * @param string $rawResponse raw response content.
	 * @param string $expectedResponseType expected response type.
	 */
	public function testDetermineContentTypeByRaw($rawResponse, $expectedResponseType)
	{
		$oauthClient = $this->createOAuthClient();
		$responseType = $this->invokeOAuthClientMethod($oauthClient, 'determineContentTypeByRaw', [$rawResponse]);
		$this->assertEquals($expectedResponseType, $responseType);
	}

	/**
	 * Data provider for [[testApiUrl]].
	 * @return array test data.
	 */
	public function apiUrlDataProvider()
	{
		return [
			[
				'http://api.base.url',
				'sub/url',
				'http://api.base.url/sub/url',
			],
			[
				'http://api.base.url',
				'http://api.base.url/sub/url',
				'http://api.base.url/sub/url',
			],
			[
				'http://api.base.url',
				'https://api.base.url/sub/url',
				'https://api.base.url/sub/url',
			],
		];
	}

	/**
	 * @dataProvider apiUrlDataProvider
	 *
	 * @param $apiBaseUrl
	 * @param $apiSubUrl
	 * @param $expectedApiFullUrl
	 */
	public function testApiUrl($apiBaseUrl, $apiSubUrl, $expectedApiFullUrl)
	{
		$oauthClient = $this->createOAuthClient();
		$oauthClient->expects($this->any())->method('apiInternal')->will($this->returnArgument(1));

242
		$accessToken = new OAuthToken();
243 244 245 246 247 248 249 250
		$accessToken->setToken('test_access_token');
		$accessToken->setExpireDuration(1000);
		$oauthClient->setAccessToken($accessToken);

		$oauthClient->apiBaseUrl = $apiBaseUrl;

		$this->assertEquals($expectedApiFullUrl, $oauthClient->api($apiSubUrl));
	}
Luciano Baraglia committed
251
}