Skip to content

Commit

Permalink
Turns a PSR-6 cache into a PSR-16 one. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
godruoyi authored May 6, 2022
1 parent d6ab682 commit 7803e73
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 4 additions & 3 deletions src/Providers/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 7 additions & 7 deletions src/Support/BaiduAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace Godruoyi\OCR\Support;

use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
use RuntimeException;

class BaiduAccessToken
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
8 changes: 5 additions & 3 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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']);
}
}
7 changes: 7 additions & 0 deletions tests/Clients/BaiduClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand Down
5 changes: 3 additions & 2 deletions tests/Requests/BaseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
);
Expand Down Expand Up @@ -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'
);
Expand Down
5 changes: 3 additions & 2 deletions tests/Support/BaiduAccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +32,7 @@ public function testGetAccessToken()

$token = new BaiduAccessToken(
$http,
new FilesystemAdapter(time() . '_' . uniqid()),
new Psr16Cache(new FilesystemAdapter(time() . '_' . uniqid())),
'secretID',
'secretKey'
);
Expand All @@ -53,7 +54,7 @@ public function testGetAccessTokenFail()

$token = new BaiduAccessToken(
$http,
new FilesystemAdapter(time() . '_' . uniqid()),
new Psr16Cache(new FilesystemAdapter(time() . '_' . uniqid())),
'secretID',
'secretKey'
);
Expand Down

0 comments on commit 7803e73

Please sign in to comment.