Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit c3ceac7

Browse files
author
Jens Schulze
committed
feat(Cache): support PSR-16 cache implementations
Closes #297
1 parent 60bb81d commit c3ceac7

File tree

6 files changed

+63
-32
lines changed

6 files changed

+63
-32
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"guzzlehttp/guzzle": "^6.0 || ^5.0",
3131
"guzzlehttp/psr7": "^1.1",
3232
"psr/cache": "^1.0",
33+
"psr/simple-cache": "^1.0",
3334
"cache/apcu-adapter": "^0.2",
3435
"ext-intl": "*",
3536
"ext-mbstring": "*"

src/Cache/CacheAdapterFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use League\Flysystem\Adapter\Local;
1717
use League\Flysystem\Filesystem;
1818
use Psr\Cache\CacheItemPoolInterface;
19+
use Psr\SimpleCache\CacheInterface;
1920

2021
/**
2122
* @package Commercetools\Core\Cache
@@ -70,7 +71,7 @@ public function registerCallback(callable $callback)
7071
* returns the cache adapter interface for the application cache
7172
*
7273
* @param $cache
73-
* @return CacheItemPoolInterface
74+
* @return CacheItemPoolInterface|CacheInterface
7475
* @throws \InvalidArgumentException
7576
*/
7677
public function get($cache = null)
@@ -82,12 +83,18 @@ public function get($cache = null)
8283
if ($cache instanceof CacheItemPoolInterface) {
8384
return $cache;
8485
}
86+
if ($cache instanceof CacheInterface) {
87+
return $cache;
88+
}
8589

8690
foreach ($this->callbacks as $callBack) {
8791
$result = call_user_func($callBack, $cache);
8892
if ($result instanceof CacheItemPoolInterface) {
8993
return $result;
9094
}
95+
if ($result instanceof CacheInterface) {
96+
return $result;
97+
}
9198
}
9299

93100
throw new InvalidArgumentException(Message::INVALID_CACHE_ADAPTER);

src/Client/OAuth/Manager.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use Psr\Http\Message\ResponseInterface;
1313
use Commercetools\Core\AbstractHttpClient;
1414
use Commercetools\Core\Cache\CacheAdapterFactory;
15-
use Commercetools\Core\Cache\CacheAdapterInterface;
1615
use Commercetools\Core\Error\InvalidClientCredentialsException;
16+
use Psr\SimpleCache\CacheInterface;
1717

1818
/**
1919
* @package Commercetools\Core\OAuth
@@ -37,7 +37,7 @@ class Manager extends AbstractHttpClient
3737
protected $cacheKeys;
3838

3939
/**
40-
* @var CacheAdapterInterface|CacheItemPoolInterface
40+
* @var CacheItemPoolInterface|CacheInterface
4141
*/
4242
protected $cacheAdapter;
4343

@@ -77,9 +77,9 @@ public function setCacheAdapter($cache)
7777

7878
/**
7979
* @internal will become protected in version 2.0
80-
* @return CacheAdapterInterface|CacheItemPoolInterface
80+
* @return CacheItemPoolInterface|CacheInterface
8181
*/
82-
public function getCacheAdapter()
82+
protected function getCacheAdapter()
8383
{
8484
return $this->cacheAdapter;
8585
}
@@ -151,27 +151,27 @@ protected function cache(Token $token, $ttl, $cacheKey = null)
151151
$cacheKey = $this->getCacheKey();
152152
}
153153
$cache = $this->getCacheAdapter();
154-
if ($cache instanceof CacheAdapterInterface) {
155-
$cache->store($cacheKey, $token->getToken(), (int)$ttl);
156-
}
157154
if ($cache instanceof CacheItemPoolInterface) {
158155
$item = $cache->getItem($cacheKey)->set($token->getToken())->expiresAfter((int)$ttl);
159156
$cache->save($item);
160157
}
158+
if ($cache instanceof CacheInterface) {
159+
$cache->set($cacheKey, $token->getToken(), (int)$ttl);
160+
}
161161
}
162162

163163
protected function getCacheToken()
164164
{
165165
$cache = $this->getCacheAdapter();
166-
if ($cache instanceof CacheAdapterInterface) {
167-
return $cache->fetch($this->getCacheKey());
168-
}
169166
if ($cache instanceof CacheItemPoolInterface) {
170167
$item = $cache->getItem($this->getCacheKey());
171168
if ($item->isHit()) {
172169
return $item->get();
173170
}
174171
}
172+
if ($cache instanceof CacheInterface) {
173+
return $cache->get($this->getCacheKey(), false);
174+
}
175175

176176
return false;
177177
}

tests/integration/Errors/ErrorResponseTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Cache\Adapter\Common\CacheItem;
1010
use Commercetools\Core\ApiTestCase;
11+
use Commercetools\Core\Cache\CacheAdapterFactory;
1112
use Commercetools\Core\Cache\CacheAdapterInterface;
1213
use Commercetools\Core\Client\OAuth\Manager;
1314
use Commercetools\Core\Error\AccessDeniedError;
@@ -613,16 +614,12 @@ public function testInsufficientScope()
613614

614615
public function testInvalidToken()
615616
{
617+
$factory = new CacheAdapterFactory();
618+
$cacheAdapter = $factory->get();
616619
$client = $this->getClient('manage_project');
617620
$cacheScope = $client->getConfig()->getScope() . '-' . $client->getConfig()->getGrantType();
618621
$cacheKey = Manager::TOKEN_CACHE_KEY . '_' . sha1($cacheScope);
619-
$cacheAdapter = $client->getOauthManager()->getCacheAdapter();
620-
if ($cacheAdapter instanceof CacheAdapterInterface) {
621-
$cacheAdapter->store($cacheKey, '1234');
622-
}
623-
if ($cacheAdapter instanceof CacheItemPoolInterface) {
624-
$cacheAdapter->save(new CacheItem($cacheKey, true, '1234'));
625-
}
622+
$cacheAdapter->save(new CacheItem($cacheKey, true, '1234'));
626623

627624
$request = ProductQueryRequest::of();
628625
$client->addBatchRequest($request);

tests/unit/Cache/CacheAdapterFactoryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Cache\Adapter\PHPArray\ArrayCachePool;
1212
use Cache\Adapter\Redis\RedisCachePool;
1313
use Doctrine\Common\Cache\ArrayCache;
14+
use Psr\SimpleCache\CacheInterface;
1415

1516
class CacheAdapterFactoryTest extends \PHPUnit_Framework_TestCase
1617
{
@@ -72,6 +73,15 @@ public function testPsrCache()
7273
$this->assertInstanceOf(ArrayCachePool::class, $adapter);
7374
}
7475

76+
public function testSimpleCache()
77+
{
78+
$cache = $this->prophesize(CacheInterface::class);
79+
$factory = $this->getFactory();
80+
$adapter = $factory->get($cache->reveal());
81+
82+
$this->assertInstanceOf(CacheInterface::class, $adapter);
83+
}
84+
7585
/**
7686
* test correct type handling
7787
*

tests/unit/Client/OAuth/ManagerTest.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Cache\Adapter\PHPArray\ArrayCachePool;
1010
use Cache\Adapter\Void\VoidCachePool;
11+
use Commercetools\Core\Cache\CacheAdapterFactory;
1112
use GuzzleHttp\Client as HttpClient;
1213
use GuzzleHttp\Handler\MockHandler;
1314
use GuzzleHttp\HandlerStack;
@@ -16,6 +17,8 @@
1617
use GuzzleHttp\Subscriber\Mock;
1718
use Commercetools\Core\Cache\NullCacheAdapter;
1819
use Commercetools\Core\Config;
20+
use Prophecy\Argument;
21+
use Psr\SimpleCache\CacheInterface;
1922

2023
class ManagerTest extends \PHPUnit_Framework_TestCase
2124
{
@@ -191,31 +194,44 @@ public function testCache()
191194
$this->assertEmpty($manager->getToken()->getTtl()); // ttl should be empty as token comes from cache
192195
}
193196

194-
public function testCacheAdapter()
197+
public function testPsrCacheAdapter()
195198
{
196199
$cache1 = new VoidCachePool();
197200
$manager = new Manager($this->getConfig(), $cache1);
198201

199-
$cache2 = new VoidCachePool();
202+
$cache2 = new ArrayCachePool();
200203
$this->assertInstanceOf(Manager::class, $manager->setCacheAdapter($cache2));
201-
$this->assertSame($cache2, $manager->getCacheAdapter());
202204
}
203205

204-
public function testPsrCacheAdapter()
206+
public function testPsrSimpleCacheAdapter()
205207
{
206-
if (version_compare(phpversion(), '5.5.0', '<')) {
207-
$this->markTestSkipped(
208-
'PHP >= 5.5 needed to run this test'
209-
);
210-
}
211-
$cache1 = new VoidCachePool();
212-
$manager = new Manager($this->getConfig(), $cache1);
208+
$cache = $this->prophesize(CacheInterface::class);
209+
$cache->get(Argument::type('string'), false)->willReturn('test')->shouldBeCalled();
210+
$manager = new Manager($this->getConfig(), $cache->reveal());
213211

214-
$cache2 = new ArrayCachePool();
215-
$this->assertInstanceOf(Manager::class, $manager->setCacheAdapter($cache2));
216-
$this->assertSame($cache2, $manager->getCacheAdapter());
212+
$this->assertSame('test', $manager->getToken()->getToken());
213+
}
214+
215+
public function testSetPsrSimpleCacheAdapter()
216+
{
217+
$cache = $this->prophesize(CacheInterface::class);
218+
$cache->get(Argument::type('string'), false)->willReturn(false)->shouldBeCalled();
219+
$cache->set(Argument::type('string'), 'myToken', 500)->shouldBeCalled();
220+
221+
$manager = $this->getManager(
222+
$this->getConfig(),
223+
[
224+
"access_token" => "myToken",
225+
"token_type" => "Bearer",
226+
"expires_in" => 1000,
227+
"scope" => "manage_project:testCache"
228+
]
229+
);
230+
$manager->setCacheAdapter($cache->reveal());
231+
$this->assertSame('myToken', $manager->getToken()->getToken());
217232
}
218233

234+
219235
/**
220236
* @expectedException \Commercetools\Core\Error\InvalidClientCredentialsException
221237
*/

0 commit comments

Comments
 (0)