From 7803e734f5c18d715cbb5a462751a96cafd15e1f Mon Sep 17 00:00:00 2001 From: LianBo Date: Fri, 6 May 2022 13:59:41 +0800 Subject: [PATCH] Turns a PSR-6 cache into a PSR-16 one. (#12) --- composer.json | 4 +++- src/Application.php | 6 +++--- src/Providers/CacheServiceProvider.php | 7 ++++--- src/Support/BaiduAccessToken.php | 14 +++++++------- tests/ApplicationTest.php | 8 +++++--- tests/Clients/BaiduClientTest.php | 7 +++++++ tests/Requests/BaseRequestTest.php | 5 +++-- tests/Support/BaiduAccessTokenTest.php | 5 +++-- 8 files changed, 35 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 09965a2..d99a2f3 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,9 @@ "monolog/monolog": "^1.22 || ^2.0", "psr/log": "^1.0", "godruoyi/easy-container": "^1.1", - "symfony/cache": "^4.4 || ^5.0 || ^6.0" + "psr/simple-cache": "^1.0 || ^2.0 || ^3.0", + "symfony/cache": "^4.4 || ^5.0 || ^6.0", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": ">=6.0", diff --git a/src/Application.php b/src/Application.php index 3b78bc5..c597c1c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -16,7 +16,7 @@ use Godruoyi\Container\ServiceProviderInterface; use Godruoyi\OCR\Contracts\Client; use InvalidArgumentException; -use Psr\Cache\CacheItemPoolInterface; +use Psr\SimpleCache\CacheInterface; class Application extends Manager { @@ -150,10 +150,10 @@ public function __get($key) /** * RebindCache cache support. * - * @param CacheItemPoolInterface $cache + * @param CacheInterface $cache * @return Application */ - public function rebindCache(CacheItemPoolInterface $cache): self + public function rebindCache(CacheInterface $cache): self { $this->container->instance('cache', $cache); diff --git a/src/Providers/CacheServiceProvider.php b/src/Providers/CacheServiceProvider.php index 0ae2118..289fb7b 100644 --- a/src/Providers/CacheServiceProvider.php +++ b/src/Providers/CacheServiceProvider.php @@ -12,17 +12,18 @@ use Godruoyi\Container\ContainerInterface; use Godruoyi\Container\ServiceProviderInterface; -use Psr\Cache\CacheItemPoolInterface; +use Psr\SimpleCache\CacheInterface; use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Component\Cache\Psr16Cache; class CacheServiceProvider implements ServiceProviderInterface { public function register(ContainerInterface $container) { $container->singleton('cache', function ($container) { - return new FilesystemAdapter('ocr.cache'); + return new Psr16Cache(new FilesystemAdapter('ocr.cache')); }); - $container->alias('cache', CacheItemPoolInterface::class); + $container->alias('cache', CacheInterface::class); } } diff --git a/src/Support/BaiduAccessToken.php b/src/Support/BaiduAccessToken.php index 84c8aad..88eacd7 100644 --- a/src/Support/BaiduAccessToken.php +++ b/src/Support/BaiduAccessToken.php @@ -10,7 +10,8 @@ namespace Godruoyi\OCR\Support; -use Psr\Cache\CacheItemPoolInterface; +use Psr\SimpleCache\CacheInterface; +use Psr\SimpleCache\InvalidArgumentException; use RuntimeException; class BaiduAccessToken @@ -27,7 +28,7 @@ class BaiduAccessToken protected $cache; - public function __construct(Http $http, CacheItemPoolInterface $cache, string $secretID = null, string $secretKey = null) + public function __construct(Http $http, CacheInterface $cache, string $secretID = null, string $secretKey = null) { $this->secretID = $secretID; $this->secretKey = $secretKey; @@ -37,16 +38,15 @@ public function __construct(Http $http, CacheItemPoolInterface $cache, string $s public function getAccessToken(): string { - $cacheItem = $this->cache->getItem(self::CACHE_KEY); + $accessToken = $this->cache->get(self::CACHE_KEY); - if ($cacheItem->isHit()) { - return $cacheItem->get(); + if ($accessToken) { + return $accessToken; } [$accessToken, $expiresIn] = $this->requestAccessToken(); - $cacheItem->set($accessToken)->expiresAfter($expiresIn - 10); - $this->cache->save($cacheItem); + $this->cache->set(self::CACHE_KEY, $accessToken, $expiresIn - 10); return $accessToken; } diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index c038def..6e548a8 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -18,7 +18,9 @@ use Godruoyi\OCR\Config; use Godruoyi\OCR\Support\Response; use InvalidArgumentException; +use Psr\SimpleCache\CacheInterface; use Symfony\Component\Cache\Adapter\NullAdapter; +use Symfony\Component\Cache\Psr16Cache; class ApplicationTest extends TestCase { @@ -111,7 +113,7 @@ public function testGetUndefinedService() $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Driver [foo] not supported.'); - $this->application->foo; + $this->application->foo->x(); } public function testGetDefaultDriver() @@ -227,8 +229,8 @@ public function testCallMethodWithDefault() public function testRebindCache() { - $this->application->rebindCache(new NullAdapter()); + $this->application->rebindCache(new Psr16Cache(new NullAdapter())); - $this->assertInstanceOf(NullAdapter::class, $this->application->getContainer()['cache']); + $this->assertInstanceOf(CacheInterface::class, $this->application->getContainer()['cache']); } } diff --git a/tests/Clients/BaiduClientTest.php b/tests/Clients/BaiduClientTest.php index 656c8d8..65017bc 100644 --- a/tests/Clients/BaiduClientTest.php +++ b/tests/Clients/BaiduClientTest.php @@ -103,6 +103,13 @@ public function testAllMethods() public function testGeneralBasic() { + $http = $this->application->getContainer()['http']; + $http = $this->mockHttpWithResponse([ + new Response(200, [], '{"access_token":"123456","expires_in":7200}'), + new Response(200, [], 'OK'), + ], $http); + $this->application->getContainer()['http'] = $http; + $response = $this->application->baidu->generalBasic(__DIR__ . '/../stubs/common.png', [ 'language_type' => 'ENG', ]); diff --git a/tests/Requests/BaseRequestTest.php b/tests/Requests/BaseRequestTest.php index 0c72284..0d7485a 100644 --- a/tests/Requests/BaseRequestTest.php +++ b/tests/Requests/BaseRequestTest.php @@ -16,6 +16,7 @@ use Godruoyi\OCR\Support\FileConverter; use Godruoyi\OCR\Support\Response; use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Component\Cache\Psr16Cache; use Test\TestCase; class BaseRequestTest extends TestCase @@ -46,7 +47,7 @@ public function testUrlAuto2Base64() $accessToken = new BaiduAccessToken( $http, - new FilesystemAdapter(time()), // set random cache dir + new Psr16Cache(new FilesystemAdapter(time())), // set random cache dir 'access_key', 'secret_key' ); @@ -80,7 +81,7 @@ public function testSupportOnlineInamge() $accessToken = new BaiduAccessToken( $http, - new FilesystemAdapter(time() . '_' . uniqid()), // set random cache dir + new Psr16Cache(new FilesystemAdapter(time() . '_' . uniqid())), // set random cache dir 'access_key', 'secret_key' ); diff --git a/tests/Support/BaiduAccessTokenTest.php b/tests/Support/BaiduAccessTokenTest.php index 17608b8..53f9f27 100644 --- a/tests/Support/BaiduAccessTokenTest.php +++ b/tests/Support/BaiduAccessTokenTest.php @@ -14,6 +14,7 @@ use Godruoyi\OCR\Support\Response; use RuntimeException; use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Component\Cache\Psr16Cache; use Test\TestCase; class BaiduAccessTokenTest extends TestCase @@ -31,7 +32,7 @@ public function testGetAccessToken() $token = new BaiduAccessToken( $http, - new FilesystemAdapter(time() . '_' . uniqid()), + new Psr16Cache(new FilesystemAdapter(time() . '_' . uniqid())), 'secretID', 'secretKey' ); @@ -53,7 +54,7 @@ public function testGetAccessTokenFail() $token = new BaiduAccessToken( $http, - new FilesystemAdapter(time() . '_' . uniqid()), + new Psr16Cache(new FilesystemAdapter(time() . '_' . uniqid())), 'secretID', 'secretKey' );