From b605c3d62338c5ff693d03bb82f32d0fc53d41e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=93=AD=E9=98=B3?= Date: Thu, 13 Apr 2017 17:13:21 +0800 Subject: [PATCH] Merge from open-platform branch. (#651) * Remove stale code. * Full namespace. * protected vars. * Cleanup * Do nothing on the auth events. * tweak. * Remove @method announce * rename. * Unused code --- .../OpenPlatformServiceProvider.php | 22 ++-- src/OpenPlatform/AccessToken.php | 16 ++- src/OpenPlatform/Api/AbstractComponent.php | 111 ------------------ src/OpenPlatform/Api/AbstractOpenPlatform.php | 65 ++++++++++ src/OpenPlatform/Api/BaseApi.php | 6 +- src/OpenPlatform/Api/PreAuthorization.php | 2 +- src/OpenPlatform/Authorization.php | 98 +--------------- src/OpenPlatform/AuthorizerAccessToken.php | 2 +- src/OpenPlatform/EventHandlers/Authorized.php | 26 +--- .../EventHandlers/ComponentVerifyTicket.php | 5 +- .../EventHandlers/EventHandler.php | 8 +- .../EventHandlers/Unauthorized.php | 12 +- .../EventHandlers/UpdateAuthorized.php | 10 +- src/OpenPlatform/OpenPlatform.php | 3 +- tests/OpenPlatform/Api/ApiTest.php | 80 +++++++++++++ tests/OpenPlatform/Api/BaseApiTest.php | 45 +------ .../OpenPlatform/Api/PreAuthorizationTest.php | 39 +----- .../OpenPlatform/AuthorizationHandlerTest.php | 88 -------------- tests/OpenPlatform/AuthorizationTest.php | 34 ++---- tests/OpenPlatform/OpenPlatformTest.php | 4 +- 20 files changed, 214 insertions(+), 462 deletions(-) delete mode 100644 src/OpenPlatform/Api/AbstractComponent.php create mode 100644 src/OpenPlatform/Api/AbstractOpenPlatform.php create mode 100644 tests/OpenPlatform/Api/ApiTest.php delete mode 100644 tests/OpenPlatform/AuthorizationHandlerTest.php diff --git a/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php b/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php index 3f5f0adb8..3703b72ce 100644 --- a/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php +++ b/src/Foundation/ServiceProviders/OpenPlatformServiceProvider.php @@ -62,12 +62,14 @@ public function register(Container $pimple) }; $pimple['open_platform.access_token'] = function ($pimple) { - return new AccessToken( + $accessToken = new AccessToken( $pimple['config']['open_platform']['app_id'], $pimple['config']['open_platform']['secret'], - $pimple['open_platform.verify_ticket'], $pimple['cache'] ); + $accessToken->setVerifyTicket($pimple['open_platform.verify_ticket']); + + return $accessToken; }; $pimple['open_platform.encryptor'] = function ($pimple) { @@ -99,14 +101,14 @@ public function register(Container $pimple) $pimple['open_platform.pre_auth'] = $pimple['open_platform.pre_authorization'] = function ($pimple) { return new PreAuthorization( $pimple['open_platform.access_token'], - $pimple['config']['open_platform'] + $pimple['request'] ); }; $pimple['open_platform.api'] = function ($pimple) { return new BaseApi( $pimple['open_platform.access_token'], - $pimple['config']['open_platform'] + $pimple['request'] ); }; @@ -129,14 +131,14 @@ public function register(Container $pimple) $pimple['open_platform.handlers.component_verify_ticket'] = function ($pimple) { return new EventHandlers\ComponentVerifyTicket($pimple['open_platform.verify_ticket']); }; - $pimple['open_platform.handlers.authorized'] = function ($pimple) { - return new EventHandlers\Authorized($pimple['open_platform.authorization']); + $pimple['open_platform.handlers.authorized'] = function () { + return new EventHandlers\Authorized(); }; - $pimple['open_platform.handlers.updateauthorized'] = function ($pimple) { - return new EventHandlers\UpdateAuthorized($pimple['open_platform.authorization']); + $pimple['open_platform.handlers.updateauthorized'] = function () { + return new EventHandlers\UpdateAuthorized(); }; - $pimple['open_platform.handlers.unauthorized'] = function ($pimple) { - return new EventHandlers\Unauthorized($pimple['open_platform.authorization']); + $pimple['open_platform.handlers.unauthorized'] = function () { + return new EventHandlers\Unauthorized(); }; $pimple['open_platform.app'] = function ($pimple) { diff --git a/src/OpenPlatform/AccessToken.php b/src/OpenPlatform/AccessToken.php index 2704885b5..d3d288da4 100644 --- a/src/OpenPlatform/AccessToken.php +++ b/src/OpenPlatform/AccessToken.php @@ -26,7 +26,6 @@ namespace EasyWeChat\OpenPlatform; -use Doctrine\Common\Cache\Cache; use EasyWeChat\Core\AccessToken as CoreAccessToken; use EasyWeChat\Core\Exceptions\HttpException; @@ -60,18 +59,17 @@ class AccessToken extends CoreAccessToken protected $prefix = 'easywechat.open_platform.component_access_token.'; /** - * AccessToken constructor. + * Set VerifyTicket. * - * @param string $appId - * @param string $secret - * @param \Doctrine\Common\Cache\Cache $cache - * @param \EasyWeChat\OpenPlatform\VerifyTicket $verifyTicket + * @param EasyWeChat\OpenPlatform\VerifyTicket $verifyTicket + * + * @return $this */ - public function __construct($appId, $secret, VerifyTicket $verifyTicket, Cache $cache = null) + public function setVerifyTicket(VerifyTicket $verifyTicket) { - parent::__construct($appId, $secret, $cache); - $this->verifyTicket = $verifyTicket; + + return $this; } /** diff --git a/src/OpenPlatform/Api/AbstractComponent.php b/src/OpenPlatform/Api/AbstractComponent.php deleted file mode 100644 index 5f99782fd..000000000 --- a/src/OpenPlatform/Api/AbstractComponent.php +++ /dev/null @@ -1,111 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -/** - * AbstractComponent.php. - * - * Part of Overtrue\WeChat. - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * @author mingyoung - * @author lixiao - * @copyright 2016 - * - * @see https://github.com/overtrue - * @see http://overtrue.me - */ - -namespace EasyWeChat\OpenPlatform\Api; - -use EasyWeChat\Core\AbstractAPI; -use EasyWeChat\Core\AccessToken; -use EasyWeChat\Core\Exception; -use Symfony\Component\HttpFoundation\Request; - -abstract class AbstractComponent extends AbstractAPI -{ - /** - * Config. - * - * @var array - */ - protected $config; - - /** - * AppId, component app id. - * - * @var string - */ - private $appId; - - /** - * Request. - * - * @var Request - */ - protected $request; - - /** - * AbstractComponent constructor. - * - * @param AccessToken $accessToken - * @param array $config - * @param $request - */ - public function __construct($accessToken, array $config, $request = null) - { - parent::__construct($accessToken); - $this->config = $config; - $this->request = $request ?: Request::createFromGlobals(); - } - - /** - * Get AppId. - * - * @return string - * - * @throws Exception when app id is not present - */ - public function getAppId() - { - if ($this->appId) { - return $this->appId; - } - - if (isset($this->config['open_platform'])) { - $this->appId = $this->config['open_platform']['app_id']; - } else { - $this->appId = $this->config['app_id']; - } - - if (empty($this->appId)) { - throw new Exception('App Id is not present.'); - } - - return $this->appId; - } - - /** - * Set AppId. - * - * @param string $appId - * - * @return $this - */ - public function setAppId($appId) - { - $this->appId = $appId; - - return $this; - } -} diff --git a/src/OpenPlatform/Api/AbstractOpenPlatform.php b/src/OpenPlatform/Api/AbstractOpenPlatform.php new file mode 100644 index 000000000..96af9c2f2 --- /dev/null +++ b/src/OpenPlatform/Api/AbstractOpenPlatform.php @@ -0,0 +1,65 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * AbstractOpenPlatform.php. + * + * Part of Overtrue\WeChat. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @author mingyoung + * @author lixiao + * @copyright 2016 + * + * @see https://github.com/overtrue + * @see http://overtrue.me + */ + +namespace EasyWeChat\OpenPlatform\Api; + +use EasyWeChat\Core\AbstractAPI; +use EasyWeChat\OpenPlatform\AccessToken; +use Symfony\Component\HttpFoundation\Request; + +abstract class AbstractOpenPlatform extends AbstractAPI +{ + /** + * Request. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** + * AbstractOpenPlatform constructor. + * + * @param \EasyWeChat\OpenPlatform\AccessToken $accessToken + * @param \Symfony\Component\HttpFoundation\Request $request + */ + public function __construct(AccessToken $accessToken, Request $request) + { + parent::__construct($accessToken); + + $this->request = $request; + } + + /** + * Get OpenPlatform AppId. + * + * @return string + */ + public function getAppId() + { + return $this->getAccessToken()->getAppId(); + } +} diff --git a/src/OpenPlatform/Api/BaseApi.php b/src/OpenPlatform/Api/BaseApi.php index 75166d0fb..d0fad4f12 100644 --- a/src/OpenPlatform/Api/BaseApi.php +++ b/src/OpenPlatform/Api/BaseApi.php @@ -27,7 +27,7 @@ namespace EasyWeChat\OpenPlatform\Api; -class BaseApi extends AbstractComponent +class BaseApi extends AbstractOpenPlatform { /** * Get auth info api. @@ -74,6 +74,10 @@ public function getAuthorizationInfo($authCode = null) /** * Get authorizer token. * + * It doesn't cache the authorizer-access-token. + * So developers should NEVER call this method. + * It'll called by: AuthorizerAccessToken::refreshToken() + * * @param $appId * @param $refreshToken * diff --git a/src/OpenPlatform/Api/PreAuthorization.php b/src/OpenPlatform/Api/PreAuthorization.php index 405e257cc..851f8fabb 100644 --- a/src/OpenPlatform/Api/PreAuthorization.php +++ b/src/OpenPlatform/Api/PreAuthorization.php @@ -30,7 +30,7 @@ use EasyWeChat\Core\Exceptions\InvalidArgumentException; use Symfony\Component\HttpFoundation\RedirectResponse; -class PreAuthorization extends AbstractComponent +class PreAuthorization extends AbstractOpenPlatform { /** * Create pre auth code url. diff --git a/src/OpenPlatform/Authorization.php b/src/OpenPlatform/Authorization.php index 840703d91..8ef2d23c5 100644 --- a/src/OpenPlatform/Authorization.php +++ b/src/OpenPlatform/Authorization.php @@ -30,7 +30,6 @@ use Doctrine\Common\Cache\Cache; use EasyWeChat\Core\Exception; use EasyWeChat\OpenPlatform\Api\BaseApi; -use EasyWeChat\Support\Collection; class Authorization { @@ -49,34 +48,25 @@ class Authorization * * @var \EasyWeChat\OpenPlatform\Api\BaseApi */ - private $api; + protected $api; /** * Open Platform App Id, aka, Component App Id. * * @var string */ - private $appId; + protected $appId; /** * Authorizer App Id. * * @var string */ - private $authorizerAppId; - - /** - * Auth code. - * - * @var string - */ - private $authCode; + protected $authorizerAppId; /** * Authorization Constructor. * - * Users need not concern the details. - * * @param \EasyWeChat\OpenPlatform\Api\BaseApi $api * @param string $appId * @param \Doctrine\Common\Cache\Cache $cache @@ -130,86 +120,6 @@ public function getAuthorizerAppId() return $this->authorizerAppId; } - /** - * Sets the auth code. - * - * @param $code - */ - public function setAuthCode($code) - { - $this->authCode = $code; - } - - /** - * Gets the auth code. - * - * @return string - */ - public function getAuthCode() - { - return $this->authCode; - } - - /** - * Sets the auth info from the message of the auth event sent by WeChat. - * - * @param \EasyWeChat\Support\Collection $message - */ - public function setFromAuthMessage(Collection $message) - { - if ($authorizerAppId = $message->get('AuthorizerAppid')) { - $this->setAuthorizerAppId($authorizerAppId); - } - if ($authorizationCode = $message->get('AuthorizationCode')) { - $this->setAuthCode($authorizationCode); - } - } - - /** - * Handles authorization: calls the API, saves the tokens. - * - * @return Collection - */ - public function handleAuthorization() - { - $info = $this->getAuthorizationInfo(); - - $appId = $info['authorization_info']['authorizer_appid']; - $this->setAuthorizerAppId($appId); - - $this->setAuthorizerAccessToken($info['authorization_info']['authorizer_access_token']); - $this->setAuthorizerRefreshToken($info['authorization_info']['authorizer_refresh_token']); - - $authorizerInfo = $this->getAuthorizerInfo(); - // Duplicated info. - $authorizerInfo->forget('authorization_info'); - $info->merge($authorizerInfo->all()); - - return $info; - } - - /** - * Gets the authorization information. - * Like authorizer app id, access token, refresh token, function scope, etc. - * - * @return \EasyWeChat\Support\Collection - */ - public function getAuthorizationInfo() - { - return $this->api->getAuthorizationInfo($this->getAuthCode()); - } - - /** - * Gets the authorizer information. - * Like authorizer name, logo, business, etc. - * - * @return \EasyWeChat\Support\Collection - */ - public function getAuthorizerInfo() - { - return $this->api->getAuthorizerInfo($this->getAuthorizerAppId()); - } - /** * Saves the authorizer access token in cache. * @@ -249,7 +159,7 @@ public function setAuthorizerRefreshToken($refreshToken) * * @return string * - * @throws Exception when refresh token is not present + * @throws \EasyWeChat\Core\Exception when refresh token is not present */ public function getAuthorizerRefreshToken() { diff --git a/src/OpenPlatform/AuthorizerAccessToken.php b/src/OpenPlatform/AuthorizerAccessToken.php index e38a32106..f97d8f3c9 100644 --- a/src/OpenPlatform/AuthorizerAccessToken.php +++ b/src/OpenPlatform/AuthorizerAccessToken.php @@ -98,7 +98,7 @@ protected function refreshToken() } /** - * Get AppId for Authorizer. + * Return the AuthorizerAppId. * * @return string */ diff --git a/src/OpenPlatform/EventHandlers/Authorized.php b/src/OpenPlatform/EventHandlers/Authorized.php index c6a7d77a8..0ddec4caf 100644 --- a/src/OpenPlatform/EventHandlers/Authorized.php +++ b/src/OpenPlatform/EventHandlers/Authorized.php @@ -27,33 +27,13 @@ namespace EasyWeChat\OpenPlatform\EventHandlers; -use EasyWeChat\OpenPlatform\Authorization; -use EasyWeChat\Support\Collection; - -class Authorized implements EventHandler +class Authorized extends EventHandler { - /** - * @var \EasyWeChat\OpenPlatform\Authorization - */ - protected $authorization; - - /** - * Constructor. - * - * @param \EasyWeChat\OpenPlatform\Authorization $authorization - */ - public function __construct(Authorization $authorization) - { - $this->authorization = $authorization; - } - /** * {@inheritdoc}. */ - public function handle(Collection $message) + public function handle($message) { - // $this->authorization->setFromAuthMessage($message); - - // return $this->authorization->handleAuthorization(); + // Do nothing for the time being. } } diff --git a/src/OpenPlatform/EventHandlers/ComponentVerifyTicket.php b/src/OpenPlatform/EventHandlers/ComponentVerifyTicket.php index f0f100f97..17c123f6f 100644 --- a/src/OpenPlatform/EventHandlers/ComponentVerifyTicket.php +++ b/src/OpenPlatform/EventHandlers/ComponentVerifyTicket.php @@ -28,9 +28,8 @@ namespace EasyWeChat\OpenPlatform\EventHandlers; use EasyWeChat\OpenPlatform\VerifyTicket; -use EasyWeChat\Support\Collection; -class ComponentVerifyTicket implements EventHandler +class ComponentVerifyTicket extends EventHandler { /** * VerifyTicket. @@ -52,7 +51,7 @@ public function __construct(VerifyTicket $verifyTicket) /** * {@inheritdoc}. */ - public function handle(Collection $message) + public function handle($message) { $this->verifyTicket->setTicket($message->get('ComponentVerifyTicket')); } diff --git a/src/OpenPlatform/EventHandlers/EventHandler.php b/src/OpenPlatform/EventHandlers/EventHandler.php index fb799591c..ed5860ba8 100644 --- a/src/OpenPlatform/EventHandlers/EventHandler.php +++ b/src/OpenPlatform/EventHandlers/EventHandler.php @@ -27,14 +27,12 @@ namespace EasyWeChat\OpenPlatform\EventHandlers; -use EasyWeChat\Support\Collection; - -interface EventHandler +abstract class EventHandler { /** - * Handle an incoming event from WeChat server-side. + * Handle an incoming event message from WeChat server-side. * * @param \EasyWeChat\Support\Collection $message */ - public function handle(Collection $message); + abstract public function handle($message); } diff --git a/src/OpenPlatform/EventHandlers/Unauthorized.php b/src/OpenPlatform/EventHandlers/Unauthorized.php index 3ba3b1242..0bae5c8a6 100644 --- a/src/OpenPlatform/EventHandlers/Unauthorized.php +++ b/src/OpenPlatform/EventHandlers/Unauthorized.php @@ -27,17 +27,13 @@ namespace EasyWeChat\OpenPlatform\EventHandlers; -use EasyWeChat\Support\Collection; - -class Unauthorized extends Authorized +class Unauthorized extends EventHandler { /** - * {@inheritdoc} + * {@inheritdoc}. */ - public function handle(Collection $message) + public function handle($message) { - // $this->authorization->setFromAuthMessage($message); - // $this->authorization->removeAuthorizerAccessToken(); - // $this->authorization->removeAuthorizerRefreshToken(); + // Do nothing for the time being. } } diff --git a/src/OpenPlatform/EventHandlers/UpdateAuthorized.php b/src/OpenPlatform/EventHandlers/UpdateAuthorized.php index c01a1176e..209c21e9e 100644 --- a/src/OpenPlatform/EventHandlers/UpdateAuthorized.php +++ b/src/OpenPlatform/EventHandlers/UpdateAuthorized.php @@ -27,7 +27,13 @@ namespace EasyWeChat\OpenPlatform\EventHandlers; -class UpdateAuthorized extends Authorized +class UpdateAuthorized extends EventHandler { - // Is this necessary to handle differently from Authorized? + /** + * {@inheritdoc} + */ + public function handle($message) + { + // Do nothing for the time being. + } } diff --git a/src/OpenPlatform/OpenPlatform.php b/src/OpenPlatform/OpenPlatform.php index 4869ab724..9d8a20aa5 100644 --- a/src/OpenPlatform/OpenPlatform.php +++ b/src/OpenPlatform/OpenPlatform.php @@ -37,7 +37,6 @@ * @property \EasyWeChat\OpenPlatform\AccessToken $access_token * * @method \EasyWeChat\Support\Collection getAuthorizationInfo($authCode = null) - * @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) @@ -54,7 +53,7 @@ class OpenPlatform * * @return \EasyWeChat\Foundation\Application */ - public function createAuthorizer($appId, $refreshToken) + public function createAuthorizerApplication($appId, $refreshToken) { $this->fetch('authorization') ->setAuthorizerAppId($appId) diff --git a/tests/OpenPlatform/Api/ApiTest.php b/tests/OpenPlatform/Api/ApiTest.php new file mode 100644 index 000000000..6df0c48b9 --- /dev/null +++ b/tests/OpenPlatform/Api/ApiTest.php @@ -0,0 +1,80 @@ + + */ + +namespace EasyWeChat\Tests\OpenPlatform\Api; + +use Doctrine\Common\Cache\ArrayCache; +use EasyWeChat\OpenPlatform\AccessToken; +use EasyWeChat\OpenPlatform\Api\AbstractOpenPlatform; +use EasyWeChat\Tests\TestCase; +use Mockery as m; +use Symfony\Component\HttpFoundation\Request; + +class ApiTest extends TestCase +{ + protected function getAccessToken($appId) + { + $accessToken = new AccessToken( + $appId, + 'secret', + new ArrayCache() + ); + + return $accessToken->setVerifyTicket(m::mock('EasyWeChat\OpenPlatform\VerifyTicket')); + } + + protected function getRequest() + { + return new Request(); + } + + public function mockBaseApi($appId) + { + $baseApi = m::mock('EasyWeChat\OpenPlatform\Api\BaseApi[parseJSON]', [$this->getAccessToken($appId), $this->getRequest()]); + /* @noinspection PhpUnusedParameterInspection */ + $baseApi->shouldReceive('parseJSON') + ->andReturnUsing(function ($method, $params) { + return [ + 'api' => $params[0], + 'params' => empty($params[1]) ? null : $params[1], + ]; + }); + + return $baseApi; + } + + protected function mockPreAuthorization($appId, $code = null) + { + $preAuth = m::mock('EasyWeChat\OpenPlatform\Api\PreAuthorization[parseJSON]', + [$this->getAccessToken($appId), $this->getRequest()] + ); + /* @noinspection PhpUnusedParameterInspection */ + $preAuth + ->shouldReceive('parseJSON') + ->andReturnUsing(function ($method, $params) use ($code) { + return [ + 'api' => $params[0], + 'params' => empty($params[1]) ? null : $params[1], + 'pre_auth_code' => $code, + ]; + }); + + return $preAuth; + } + + public function testGetAppId() + { + $api = new OpenPlatformApi($this->getAccessToken('app_id'), $this->getRequest()); + + $this->assertEquals('app_id', $api->getAppId()); + } +} + +class OpenPlatformApi extends AbstractOpenPlatform +{ +} diff --git a/tests/OpenPlatform/Api/BaseApiTest.php b/tests/OpenPlatform/Api/BaseApiTest.php index fc32caeac..06959c98c 100644 --- a/tests/OpenPlatform/Api/BaseApiTest.php +++ b/tests/OpenPlatform/Api/BaseApiTest.php @@ -8,46 +8,13 @@ namespace EasyWeChat\Tests\OpenPlatform\Api; -use EasyWeChat\OpenPlatform\AccessToken; use EasyWeChat\OpenPlatform\Api\BaseApi; -use EasyWeChat\Tests\TestCase; -use Mockery as m; -class BaseApiTest extends TestCase +class BaseApiTest extends ApiTest { - /** - * Authorization mock. - * - * @param string $appId - * - * @return \Mockery\MockInterface|\EasyWeChat\OpenPlatform\Api\BaseApi - */ - public function mockAuthorization($appId) - { - $authorization = m::mock( - 'EasyWeChat\OpenPlatform\Api\BaseApi[parseJSON]', - [ - m::mock(AccessToken::class), - ['open_platform' => ['app_id' => $appId]], - ] - ); - - /* @noinspection PhpUnusedParameterInspection */ - $authorization - ->shouldReceive('parseJSON') - ->andReturnUsing(function ($method, $params) { - return [ - 'api' => $params[0], - 'params' => empty($params[1]) ? null : $params[1], - ]; - }); - - return $authorization; - } - public function testGetAuthorizationInfo() { - $authorizer = $this->mockAuthorization('appid@123'); + $authorizer = $this->mockBaseApi('appid@123'); $result = $authorizer->getAuthorizationInfo('code@123'); $expected = [ 'component_appid' => 'appid@123', @@ -60,7 +27,7 @@ public function testGetAuthorizationInfo() public function testGetAuthorizerToken() { - $authorizer = $this->mockAuthorization('appid@123'); + $authorizer = $this->mockBaseApi('appid@123'); $result = $authorizer->getAuthorizerToken('appid@456', 'refresh@123'); $expected = [ 'component_appid' => 'appid@123', @@ -74,7 +41,7 @@ public function testGetAuthorizerToken() public function testGetAuthorizerInfo() { - $authorizer = $this->mockAuthorization('appid@123'); + $authorizer = $this->mockBaseApi('appid@123'); $result = $authorizer->getAuthorizerInfo('appid@456'); $expected = [ 'component_appid' => 'appid@123', @@ -87,7 +54,7 @@ public function testGetAuthorizerInfo() public function testGetAuthorizerOption() { - $authorizer = $this->mockAuthorization('appid@123'); + $authorizer = $this->mockBaseApi('appid@123'); $result = $authorizer->getAuthorizerOption('appid@456', 'option@123'); $expected = [ 'component_appid' => 'appid@123', @@ -101,7 +68,7 @@ public function testGetAuthorizerOption() public function testSetAuthorizerOption() { - $authorizer = $this->mockAuthorization('appid@123'); + $authorizer = $this->mockBaseApi('appid@123'); $result = $authorizer->setAuthorizerOption('appid@456', 'option@123', 'value@123'); $expected = [ 'component_appid' => 'appid@123', diff --git a/tests/OpenPlatform/Api/PreAuthorizationTest.php b/tests/OpenPlatform/Api/PreAuthorizationTest.php index 4f5de9eb6..37202014c 100644 --- a/tests/OpenPlatform/Api/PreAuthorizationTest.php +++ b/tests/OpenPlatform/Api/PreAuthorizationTest.php @@ -8,45 +8,8 @@ namespace EasyWeChat\Tests\OpenPlatform\Api; -use EasyWeChat\OpenPlatform\AccessToken; -use EasyWeChat\OpenPlatform\Api\PreAuthorization; -use EasyWeChat\Tests\TestCase; -use Mockery as m; - -class PreAuthorizationTest extends TestCase +class PreAuthorizationTest extends ApiTest { - /** - * PreAuth mock. - * - * @param string $appId - * @param string $code - * - * @return \Mockery\MockInterface|PreAuth - */ - public function mockPreAuthorization($appId, $code = null) - { - $preAuth = m::mock( - PreAuthorization::class.'[parseJSON]', - [ - m::mock(AccessToken::class), - ['open_platform' => ['app_id' => $appId]], - ] - ); - - /* @noinspection PhpUnusedParameterInspection */ - $preAuth - ->shouldReceive('parseJSON') - ->andReturnUsing(function ($method, $params) use ($code) { - return [ - 'api' => $params[0], - 'params' => empty($params[1]) ? null : $params[1], - 'pre_auth_code' => $code, - ]; - }); - - return $preAuth; - } - public function testGetAppId() { $this->assertEquals('appid@foobar', $this->mockPreAuthorization('appid@foobar')->getAppId()); diff --git a/tests/OpenPlatform/AuthorizationHandlerTest.php b/tests/OpenPlatform/AuthorizationHandlerTest.php deleted file mode 100644 index 5d9c4794c..000000000 --- a/tests/OpenPlatform/AuthorizationHandlerTest.php +++ /dev/null @@ -1,88 +0,0 @@ - - */ - -namespace EasyWeChat\Tests\OpenPlatform; - -use EasyWeChat\OpenPlatform\EventHandlers\Authorized; -use EasyWeChat\OpenPlatform\EventHandlers\Unauthorized; -use EasyWeChat\Support\Collection; - -class AuthorizationHandlerTest extends AuthorizationTest -{ - public function testAuthorized() - { - return; - $appId = 'appid@123'; - $authorizerAppId = 'appid@456'; - $authorizerAccessToken = 'access@123'; - $authorizerRefreshToken = 'refresh@123'; - $authorization = $this->make( - $appId, $authorizerAppId, - $authorizerAccessToken, $authorizerRefreshToken - ); - - $message = [ - 'AppId' => 'open-platform-app-id', - 'CreateTIme' => '1413192760', - 'InfoType' => 'authorized', - 'AuthorizerAppid' => 'authorizer-app-id', - 'AuthorizationCode' => 'auth-code', - 'AuthorizationCodeExpiredTime' => '600', - ]; - $authorized = new Authorized($authorization); - $authorized->handle(new Collection($message)); - - $this->assertEquals( - $authorizerAccessToken, - $authorization->getAuthorizerAccessToken() - ); - $this->assertEquals( - $authorizerRefreshToken, - $authorization->getAuthorizerRefreshToken() - ); - } - - public function testUnauthorized() - { - return; - $appId = 'appid@123'; - $authorizerAppId = 'appid@456'; - $authorizerAccessToken = 'access@123'; - $authorizerRefreshToken = 'refresh@123'; - $authorization = $this->make( - $appId, $authorizerAppId, - $authorizerAccessToken, $authorizerRefreshToken - ); - - // Authorized => saves the tokens. - $message = [ - 'AppId' => 'open-platform-app-id', - 'CreateTIme' => '1413192760', - 'InfoType' => 'authorized', - 'AuthorizerAppid' => 'authorizer-app-id', - 'AuthorizationCode' => 'auth-code', - 'AuthorizationCodeExpiredTime' => '600', - ]; - $authorized = new Authorized($authorization); - $authorized->handle(new Collection($message)); - - // Unauthorized => removes the tokens. - $message = [ - 'AppId' => 'open-platform-app-id', - 'CreateTIme' => '1413192760', - 'InfoType' => 'authorized', - 'AuthorizerAppid' => 'authorizer-app-id', - ]; - $authorized = new Unauthorized($authorization); - $authorized->handle(new Collection($message)); - - $this->assertFalse($authorization->getAuthorizerAccessToken()); - $this->setExpectedException(\EasyWeChat\Core\Exception::class); - $this->assertFalse($authorization->getAuthorizerRefreshToken()); - } -} diff --git a/tests/OpenPlatform/AuthorizationTest.php b/tests/OpenPlatform/AuthorizationTest.php index 65d268c3f..e809b529a 100644 --- a/tests/OpenPlatform/AuthorizationTest.php +++ b/tests/OpenPlatform/AuthorizationTest.php @@ -15,13 +15,20 @@ class AuthorizationTest extends TestCase { + public function testGetApi() + { + $authorization = $this->make('appid', 'authorizer-appid'); + + $this->assertInstanceOf('EasyWeChat\OpenPlatform\Api\BaseApi', $authorization->getApi()); + } + public function testGetAuthorizationInfo() { $appId = 'appid@123'; $authorizerAppId = 'appid@456'; $authorization = $this->make($appId, $authorizerAppId); - $result = $authorization->getAuthorizationInfo(); + $result = $authorization->getApi()->getAuthorizationInfo(); $this->assertEquals($this->stubAuthorizationInfo($authorizerAppId), $result); } @@ -31,7 +38,7 @@ public function testGetAuthorizerInfo() $authorizerAppId = 'appid@456'; $authorization = $this->make($appId, $authorizerAppId); - $result = $authorization->getAuthorizerInfo(); + $result = $authorization->getApi()->getAuthorizerInfo('appid@123'); $this->assertEquals($this->stubAuthorizerInfo($authorizerAppId), $result); } @@ -57,29 +64,6 @@ public function testSetAndGetAuthorizerRefreshToken() $this->assertEquals('refresh@123', $authorization->getAuthorizerRefreshToken()); } - public function testHandleAuthorization() - { - $appId = 'appid@123'; - $authorizerAppId = 'appid@456'; - $authorizerAccessToken = 'access@123'; - $authorizerRefreshToken = 'refresh@123'; - $authorization = $this->make( - $appId, $authorizerAppId, - $authorizerAccessToken, $authorizerRefreshToken - ); - - $stub = $this->stubAuthorizationAll($authorizerAppId, - $authorizerAccessToken, $authorizerRefreshToken); - $result = $authorization->handleAuthorization(); - $this->assertEquals($stub, $result); - - $savedAccessToken = $authorization->getAuthorizerAccessToken(); - $savedRefreshToken = $authorization->getAuthorizerRefreshToken(); - - $this->assertEquals($authorizerAccessToken, $savedAccessToken); - $this->assertEquals($authorizerRefreshToken, $savedRefreshToken); - } - /** * Authorization mock. * diff --git a/tests/OpenPlatform/OpenPlatformTest.php b/tests/OpenPlatform/OpenPlatformTest.php index 306cb5e3d..5c0b961f2 100644 --- a/tests/OpenPlatform/OpenPlatformTest.php +++ b/tests/OpenPlatform/OpenPlatformTest.php @@ -41,13 +41,13 @@ public function testMakeAuthorizer() $accessToken = new AccessToken( 'open-platform-appid@999', 'open-platform-secret', - $verifyTicket, $cache ); + $accessToken->setVerifyTicket($verifyTicket); $app = $this->make(); $app['open_platform.access_token'] = $accessToken; - $newApp = $app->open_platform->createAuthorizer('authorizer-appid@999', 'authorizer-refresh-token'); + $newApp = $app->open_platform->createAuthorizerApplication('authorizer-appid@999', 'authorizer-refresh-token'); $this->assertInstanceOf('EasyWeChat\OpenPlatform\AuthorizerAccessToken', $newApp->access_token); $this->assertEquals('authorizer-appid@999', $newApp->access_token->getAppId());