Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code for open-platform #649

Merged
merged 14 commits into from
Apr 13, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function register(Container $pimple)
);
};

$pimple['open_platform.authorizer_token'] = function ($pimple) {
$pimple['open_platform.authorizer_access_token'] = function ($pimple) {
return new AuthorizerAccessToken(
$pimple['config']['open_platform']['app_id'],
$pimple['open_platform.authorization']
Expand Down
14 changes: 7 additions & 7 deletions src/OpenPlatform/Api/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BaseApi extends AbstractComponent
const GET_AUTH_INFO = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth';

/**
* Authorization token api.
* Get authorizer token api.
*/
const GET_AUTHORIZER_TOKEN = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token';

Expand Down Expand Up @@ -72,19 +72,19 @@ public function getAuthorizationInfo($authCode = null)
}

/**
* Get authorization token.
* Get authorizer token.
*
* @param $authorizerAppId
* @param $authorizerRefreshToken
* @param $appId
* @param $refreshToken
*
* @return \EasyWeChat\Support\Collection
*/
public function getAuthorizationToken($authorizerAppId, $authorizerRefreshToken)
public function getAuthorizerToken($appId, $refreshToken)
{
$params = [
'component_appid' => $this->getAppId(),
'authorizer_appid' => $authorizerAppId,
'authorizer_refresh_token' => $authorizerRefreshToken,
'authorizer_appid' => $appId,
'authorizer_refresh_token' => $refreshToken,
];

return $this->parseJSON('json', [self::GET_AUTHORIZER_TOKEN, $params]);
Expand Down
33 changes: 15 additions & 18 deletions src/OpenPlatform/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,28 @@ public function __construct(BaseApi $api, $appId, Cache $cache)
$this->cache = $cache;
}

/**
* Gets the base api.
*
* @return \EasyWeChat\OpenPlatform\Api\BaseApi
*/
public function getApi()
{
return $this->api;
}

/**
* Sets the authorizer app id.
*
* @param string $authorizerAppId
*
* @return $this
*/
public function setAuthorizerAppId($authorizerAppId)
{
$this->authorizerAppId = $authorizerAppId;

return $this;
}

/**
Expand Down Expand Up @@ -174,23 +188,6 @@ public function handleAuthorization()
return $info;
}

/**
* Handles the authorizer access token: calls the API, saves the token.
*
* @return string the authorizer access token
*/
public function handleAuthorizerAccessToken()
{
$data = $this->api->getAuthorizationToken(
$this->getAuthorizerAppId(),
$this->getAuthorizerRefreshToken()
);

$this->setAuthorizerAccessToken($data);

return $data['authorizer_access_token'];
}

/**
* Gets the authorization information.
* Like authorizer app id, access token, refresh token, function scope, etc.
Expand Down Expand Up @@ -222,7 +219,7 @@ public function getAuthorizerInfo()
*/
public function setAuthorizerAccessToken($token, $expires = 7200)
{
return $this->cache->save($this->getAuthorizerAccessTokenKey(), $token, $expires - 1500);
return $this->cache->save($this->getAuthorizerAccessTokenKey(), $token, $expires);
}

/**
Expand Down
20 changes: 19 additions & 1 deletion src/OpenPlatform/AuthorizerAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,30 @@ public function getToken($forceRefresh = false)
$cached = $this->authorization->getAuthorizerAccessToken();

if ($forceRefresh || empty($cached)) {
return $this->authorization->handleAuthorizerAccessToken();
return $this->refreshToken();
}

return $cached;
}

/**
* Refresh authorizer access token.
*
* @return string
*/
protected function refreshToken()
{
$token = $this->authorization->getApi()
->getAuthorizerToken(
$this->authorization->getAuthorizerAppId(),
$this->authorization->getAuthorizerRefreshToken()
);

$this->authorization->setAuthorizerAccessToken($token['authorizer_access_token'], $token['expires_in'] - 1500);

return $token['authorizer_access_token'];
}

/**
* Get AppId for Authorizer.
*
Expand Down
14 changes: 8 additions & 6 deletions src/OpenPlatform/OpenPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
/**
* Class OpenPlatform.
*
* @property \EasyWeChat\OpenPlatform\Api\BaseApi $api
* @property \EasyWeChat\OpenPlatform\Api\PreAuthorization $pre_auth
* @property \EasyWeChat\OpenPlatform\Guard $server
* @property \EasyWeChat\OpenPlatform\AccessToken $access_token
*
* @method \EasyWeChat\Support\Collection getAuthorizationInfo($authCode = null)
* @method \EasyWeChat\Support\Collection getAuthorizationToken($authorizerAppId, $authorizerRefreshToken)
* @method \EasyWeChat\Support\Collection getAuthorizerToken($appId, $refreshToken)
* @method \EasyWeChat\Support\Collection getAuthorizerInfo($authorizerAppId)
* @method \EasyWeChat\Support\Collection getAuthorizerOption($authorizerAppId, $optionName)
* @method \EasyWeChat\Support\Collection setAuthorizerOption($authorizerAppId, $optionName, $optionValue)
Expand All @@ -55,12 +56,13 @@ class OpenPlatform
*/
public function createAuthorizer($appId, $refreshToken)
{
$this->authorization->setAuthorizerAppId($appId);
$this->authorization->setAuthorizerRefreshToken($refreshToken);
$this->fetch('authorization')
->setAuthorizerAppId($appId)
->setAuthorizerRefreshToken($refreshToken);

$application = $this->app;
$application['access_token'] = $this->authorizer_token;
$application['oauth'] = $this->oauth;
$application = $this->fetch('app');
$application['access_token'] = $this->fetch('authorizer_access_token');
$application['oauth'] = $this->fetch('oauth');

return $application;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Support/Traits/PrefixedContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public function __construct(Container $container)
$this->container = $container;
}

/**
* Fetches from pimple container.
*
* @param string $key
*
* @return mixed
*/
public function fetch($key)
{
return $this->$key;
}

/**
* Gets a parameter or an object from pimple container.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/OpenPlatform/Api/BaseApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function testGetAuthorizationInfo()
$this->assertEquals($expected, $result['params']);
}

public function testGetAuthorizationToken()
public function testGetAuthorizerToken()
{
$authorizer = $this->mockAuthorization('appid@123');
$result = $authorizer->getAuthorizationToken('appid@456', 'refresh@123');
$result = $authorizer->getAuthorizerToken('appid@456', 'refresh@123');
$expected = [
'component_appid' => 'appid@123',
'authorizer_appid' => 'appid@456',
Expand Down
20 changes: 1 addition & 19 deletions tests/OpenPlatform/AuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ public function testSetAndGetAuthorizerRefreshToken()
$this->assertEquals('refresh@123', $authorization->getAuthorizerRefreshToken());
}

public function testHandleAuthorizerAccessToken()
{
$appId = 'appid@123';
$authorizerAppId = 'appid@456';
$authorizerAccessToken = 'access@123';
$authorizerRefreshToken = 'refresh@123';
$authorization = $this->make(
$appId, $authorizerAppId,
$authorizerAccessToken, $authorizerRefreshToken
);

$stub = $this->stubAuthorizationInfo($authorizerAppId, $authorizerRefreshToken);
$authorization->setAuthorizerRefreshToken($stub['authorization_info']);

$result = $authorization->handleAuthorizerAccessToken();
$this->assertEquals($authorizerAccessToken, $result);
}

public function testHandleAuthorization()
{
$appId = 'appid@123';
Expand Down Expand Up @@ -134,7 +116,7 @@ protected function make($appId, $authorizerAppId,
);
/* @noinspection PhpUnusedParameterInspection */
$mockAuthorizer
->shouldReceive('getAuthorizationToken')
->shouldReceive('getAuthorizerToken')
->andReturnUsing(
function ($appId, $authorizerRefreshToken) use ($stub) {
return $stub;
Expand Down
10 changes: 8 additions & 2 deletions tests/OpenPlatform/AuthorizerAccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace EasyWeChat\Tests\OpenPlatform;

use EasyWeChat\OpenPlatform\AuthorizerAccessToken;
use EasyWeChat\Support\Collection;
use EasyWeChat\Tests\TestCase;

class AuthorizerAccessTokenTest extends TestCase
Expand Down Expand Up @@ -48,10 +49,15 @@ private function make($appId, $cachedToken, $newToken)
{
/** @var Authorization|\Mockery\MockInterface $mock */
$mock = \Mockery::mock('EasyWeChat\OpenPlatform\Authorization');
$mock->shouldReceive('getAuthorizerAppId')->andReturn($appId);
$mock->shouldReceive('getAuthorizerRefreshToken')->andReturn($newToken);
$mock->shouldReceive('setAuthorizerAccessToken')->andReturn(true);
$mock->shouldReceive('getAuthorizerAccessToken')
->andReturn($cachedToken);
$mock->shouldReceive('handleAuthorizerAccessToken')
->andReturn($newToken);
$mock->shouldReceive('getApi')
->andReturn(\Mockery::mock('EasyWeChat\OpenPlatform\Api\BaseApi', function ($mock) use ($newToken) {
$mock->shouldReceive('getAuthorizerToken')->andReturn(new Collection(['authorizer_access_token' => $newToken, 'expires_in' => 7200]));
}));

return new AuthorizerAccessToken($appId, $mock);
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Support/Traits/PrefixedContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace EasyWeChat\Tests\Support\Traits;

use EasyWeChat\Tests\TestCase;
use Pimple\Container;

class PrefixedContainerTest extends TestCase
{
protected function make(array $values = [])
{
return new Application(
new Container($values)
);
}

public function testFetch()
{
$app = $this->make([
'application.foo' => 'foo@application',
]);

$this->assertEquals('foo@application', $app->fetch('foo'));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testCanThrownInvalidArgumentException()
{
$app = $this->make();

$app->fetch('not-exists');
}

public function testMagicGet()
{
$app = $this->make([
'application.bar' => 'bar@application',
]);

$this->assertEquals('bar@application', $app->fetch('bar'));
}
}

class Application
{
use \EasyWeChat\Support\Traits\PrefixedContainer;
}