You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.6 KiB
133 lines
3.6 KiB
11 years ago
|
<?php
|
||
|
|
||
|
namespace yiiunit\extensions\authclient\oauth;
|
||
|
|
||
11 years ago
|
use yii\authclient\OAuthToken;
|
||
11 years ago
|
use yiiunit\extensions\authclient\TestCase;
|
||
|
|
||
|
class TokenTest extends TestCase
|
||
|
{
|
||
|
public function testCreate()
|
||
|
{
|
||
|
$config = [
|
||
|
'tokenParamKey' => 'test_token_param_key',
|
||
|
'tokenSecretParamKey' => 'test_token_secret_param_key',
|
||
|
];
|
||
11 years ago
|
$oauthToken = new OAuthToken($config);
|
||
11 years ago
|
$this->assertTrue(is_object($oauthToken), 'Unable to create access token!');
|
||
|
foreach ($config as $name => $value) {
|
||
|
$this->assertEquals($value, $oauthToken->$name, 'Unable to setup attributes by constructor!');
|
||
|
}
|
||
|
$this->assertTrue($oauthToken->createTimestamp > 0, 'Unable to fill create timestamp!');
|
||
|
}
|
||
|
|
||
|
public function testSetupParams()
|
||
|
{
|
||
11 years ago
|
$oauthToken = new OAuthToken();
|
||
11 years ago
|
|
||
|
$params = [
|
||
|
'name_1' => 'value_1',
|
||
|
'name_2' => 'value_2',
|
||
|
];
|
||
|
$oauthToken->setParams($params);
|
||
|
$this->assertEquals($params, $oauthToken->getParams(), 'Unable to setup params!');
|
||
|
|
||
|
$newParamName = 'new_param_name';
|
||
|
$newParamValue = 'new_param_value';
|
||
|
$oauthToken->setParam($newParamName, $newParamValue);
|
||
|
$this->assertEquals($newParamValue, $oauthToken->getParam($newParamName), 'Unable to setup param by name!');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testSetupParams
|
||
|
*/
|
||
|
public function testSetupParamsShortcuts()
|
||
|
{
|
||
11 years ago
|
$oauthToken = new OAuthToken();
|
||
11 years ago
|
|
||
|
$token = 'test_token_value';
|
||
|
$oauthToken->setToken($token);
|
||
|
$this->assertEquals($token, $oauthToken->getToken(), 'Unable to setup token!');
|
||
|
|
||
|
$tokenSecret = 'test_token_secret';
|
||
|
$oauthToken->setTokenSecret($tokenSecret);
|
||
|
$this->assertEquals($tokenSecret, $oauthToken->getTokenSecret(), 'Unable to setup token secret!');
|
||
|
|
||
|
$tokenExpireDuration = rand(1000, 2000);
|
||
|
$oauthToken->setExpireDuration($tokenExpireDuration);
|
||
|
$this->assertEquals($tokenExpireDuration, $oauthToken->getExpireDuration(), 'Unable to setup expire duration!');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Data provider for {@link testAutoFetchExpireDuration}.
|
||
|
* @return array test data.
|
||
|
*/
|
||
|
public function autoFetchExpireDurationDataProvider()
|
||
|
{
|
||
|
return [
|
||
|
[
|
||
|
['expire_in' => 123345],
|
||
|
123345
|
||
|
],
|
||
|
[
|
||
|
['expire' => 233456],
|
||
|
233456
|
||
|
],
|
||
|
[
|
||
|
['expiry_in' => 34567],
|
||
|
34567
|
||
|
],
|
||
|
[
|
||
|
['expiry' => 45678],
|
||
|
45678
|
||
|
],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testSetupParamsShortcuts
|
||
|
* @dataProvider autoFetchExpireDurationDataProvider
|
||
|
*
|
||
|
* @param array $params
|
||
|
* @param $expectedExpireDuration
|
||
|
*/
|
||
|
public function testAutoFetchExpireDuration(array $params, $expectedExpireDuration)
|
||
|
{
|
||
11 years ago
|
$oauthToken = new OAuthToken();
|
||
11 years ago
|
$oauthToken->setParams($params);
|
||
|
$this->assertEquals($expectedExpireDuration, $oauthToken->getExpireDuration());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testSetupParamsShortcuts
|
||
|
*/
|
||
|
public function testGetIsExpired()
|
||
|
{
|
||
11 years ago
|
$oauthToken = new OAuthToken();
|
||
11 years ago
|
$expireDuration = 3600;
|
||
|
$oauthToken->setExpireDuration($expireDuration);
|
||
|
|
||
|
$this->assertFalse($oauthToken->getIsExpired(), 'Not expired token check fails!');
|
||
|
|
||
|
$oauthToken->createTimestamp = $oauthToken->createTimestamp - ($expireDuration +1);
|
||
|
$this->assertTrue($oauthToken->getIsExpired(), 'Expired token check fails!');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testGetIsExpired
|
||
|
*/
|
||
|
public function testGetIsValid()
|
||
|
{
|
||
11 years ago
|
$oauthToken = new OAuthToken();
|
||
11 years ago
|
$expireDuration = 3600;
|
||
|
$oauthToken->setExpireDuration($expireDuration);
|
||
|
|
||
|
$this->assertFalse($oauthToken->getIsValid(), 'Empty token is valid!');
|
||
|
|
||
|
$oauthToken->setToken('test_token');
|
||
|
$this->assertTrue($oauthToken->getIsValid(), 'Filled up token is invalid!');
|
||
|
|
||
|
$oauthToken->createTimestamp = $oauthToken->createTimestamp - ($expireDuration +1);
|
||
|
$this->assertFalse($oauthToken->getIsValid(), 'Expired token is valid!');
|
||
|
}
|
||
|
}
|