Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: w7corp/easywechat
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6.12.8
Choose a base ref
...
head repository: w7corp/easywechat
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6.12.9
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Aug 30, 2023

  1. 1
    Copy the full SHA
    5169192 View commit details
Showing with 45 additions and 9 deletions.
  1. +1 −1 src/MiniApp/AccessToken.php
  2. +28 −8 src/OfficialAccount/AccessToken.php
  3. +16 −0 tests/MiniApp/AccessTokenTest.php
2 changes: 1 addition & 1 deletion src/MiniApp/AccessToken.php
Original file line number Diff line number Diff line change
@@ -6,5 +6,5 @@

class AccessToken extends \EasyWeChat\OfficialAccount\AccessToken
{
//
const CACHE_KEY_PREFIX = 'mini_app';
}
36 changes: 28 additions & 8 deletions src/OfficialAccount/AccessToken.php
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@ class AccessToken implements RefreshableAccessTokenInterface

protected CacheInterface $cache;

const CACHE_KEY_PREFIX = 'official_account';

public function __construct(
protected string $appId,
protected string $secret,
@@ -44,7 +46,7 @@ public function __construct(

public function getKey(): string
{
return $this->key ?? $this->key = sprintf('official_account.access_token.%s.%s', $this->appId, $this->secret);
return $this->key ?? $this->key = sprintf('%s.access_token.%s.%s', static::CACHE_KEY_PREFIX, $this->appId, $this->secret);
}

public function setKey(string $key): static
@@ -92,19 +94,28 @@ public function toQuery(): array
}

/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws InvalidArgumentException
* @throws ClientExceptionInterface
* @throws HttpException
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function refresh(): string
{
return $this->stable ? $this->getStableAccessToken() : $this->getAccessToken();
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
*/
public function getStableAccessToken(bool $force_refresh = false): string
{
$response = $this->httpClient->request(
@@ -129,6 +140,15 @@ public function getStableAccessToken(bool $force_refresh = false): string
return $response['access_token'];
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
*/
public function getAccessToken(): string
{
$response = $this->httpClient->request(
16 changes: 16 additions & 0 deletions tests/MiniApp/AccessTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace EasyWeChat\Tests\MiniApp;

use EasyWeChat\MiniApp\AccessToken;
use EasyWeChat\Tests\TestCase;

class AccessTokenTest extends TestCase
{
public function test_it_will_use_mini_app_cache_prefix()
{
$accessToken = new AccessToken('mock-app-id', 'mock-secret');

$this->assertStringStartsWith('mini_app.access_token', $accessToken->getKey());
}
}