TokenTest.php 4.15 KB
Newer Older
1 2 3 4
<?php

namespace yiiunit\extensions\authclient\oauth;

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

class TokenTest extends TestCase
{
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
    public function testCreate()
    {
        $config = [
            'tokenParamKey' => 'test_token_param_key',
            'tokenSecretParamKey' => 'test_token_secret_param_key',
        ];
        $oauthToken = new OAuthToken($config);
        $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()
    {
        $oauthToken = new OAuthToken();

        $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()
    {
        $oauthToken = new OAuthToken();

        $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)
    {
        $oauthToken = new OAuthToken();
        $oauthToken->setParams($params);
        $this->assertEquals($expectedExpireDuration, $oauthToken->getExpireDuration());
    }

    /**
     * @depends testSetupParamsShortcuts
     */
    public function testGetIsExpired()
    {
        $oauthToken = new OAuthToken();
        $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()
    {
        $oauthToken = new OAuthToken();
        $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!');
    }
AlexGx committed
133
}