Skip to content

Commit

Permalink
Spark: Use Psr\Cache instead of CAS for caching credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Jan 15, 2024
1 parent baaae31 commit 1f560d2
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 173 deletions.
29 changes: 17 additions & 12 deletions src/Lunr/Spark/Contentful/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Lunr\Spark\Contentful;

use Lunr\Spark\CentralAuthenticationStore;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use WpOrg\Requests\Session;

Expand All @@ -30,10 +30,10 @@ class Api
protected const URL = 'https://www.contentful.com';

/**
* Shared instance of the CentralAuthenticationStore
* @var CentralAuthenticationStore
* Shared instance of the credentials cache
* @var CacheItemPoolInterface
*/
protected $cas;
protected $cache;

/**
* Shared instance of a Logger class.
Expand Down Expand Up @@ -62,13 +62,13 @@ class Api
/**
* Constructor.
*
* @param CentralAuthenticationStore $cas Shared instance of the credentials store
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
* @param CacheItemPoolInterface $cache Shared instance of the credentials cache
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
*/
public function __construct($cas, $logger, $http)
public function __construct($cache, $logger, $http)
{
$this->cas = $cas;
$this->cache = $cache;
$this->logger = $logger;
$this->http = $http;
$this->space = '';
Expand All @@ -80,7 +80,7 @@ public function __construct($cas, $logger, $http)
*/
public function __destruct()
{
unset($this->cas);
unset($this->cache);
unset($this->logger);
unset($this->http);
unset($this->space);
Expand All @@ -100,7 +100,7 @@ public function __get($key)
{
case 'access_token':
case 'management_token':
return $this->cas->get('contentful', $key);
return $this->cache->getItem('contentful.' . $key)->get();
default:
return NULL;
}
Expand All @@ -120,7 +120,12 @@ public function __set($key, $value)
{
case 'access_token':
case 'management_token':
$this->cas->add('contentful', $key, $value);
$item = $this->cache->getItem('contentful.' . $key);

$item->expiresAfter(600);
$item->set($value);

$this->cache->save($item);
break;
default:
break;
Expand Down
12 changes: 6 additions & 6 deletions src/Lunr/Spark/Contentful/DeliveryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Lunr\Spark\Contentful;

use Lunr\Spark\CentralAuthenticationStore;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use WpOrg\Requests\Exception as RequestsException;
use WpOrg\Requests\Exception\Http as RequestsExceptionHTTP;
Expand All @@ -32,13 +32,13 @@ class DeliveryApi extends Api
/**
* Constructor.
*
* @param CentralAuthenticationStore $cas Shared instance of the credentials store
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
* @param CacheItemPoolInterface $cache Shared instance of the credentials cache
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
*/
public function __construct($cas, $logger, $http)
public function __construct($cache, $logger, $http)
{
parent::__construct($cas, $logger, $http);
parent::__construct($cache, $logger, $http);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Lunr/Spark/Contentful/ManagementApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Lunr\Spark\Contentful;

use Lunr\Spark\CentralAuthenticationStore;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use WpOrg\Requests\Exception as RequestsException;
use WpOrg\Requests\Requests;
Expand All @@ -32,13 +32,13 @@ class ManagementApi extends Api
/**
* Constructor.
*
* @param CentralAuthenticationStore $cas Shared instance of the credentials store
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
* @param CacheItemPoolInterface $cache Shared instance of the credentials cache
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
*/
public function __construct($cas, $logger, $http)
public function __construct($cache, $logger, $http)
{
parent::__construct($cas, $logger, $http);
parent::__construct($cache, $logger, $http);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Lunr/Spark/Contentful/PreviewApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Lunr\Spark\Contentful;

use Lunr\Spark\CentralAuthenticationStore;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use WpOrg\Requests\Session;

Expand All @@ -29,13 +29,13 @@ class PreviewApi extends DeliveryApi
/**
* Constructor.
*
* @param CentralAuthenticationStore $cas Shared instance of the credentials store
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
* @param CacheItemPoolInterface $cache Shared instance of the credentials cache
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param Session $http Shared instance of the Requests\Session class.
*/
public function __construct($cas, $logger, $http)
public function __construct($cache, $logger, $http)
{
parent::__construct($cas, $logger, $http);
parent::__construct($cache, $logger, $http);
}

/**
Expand Down
56 changes: 38 additions & 18 deletions src/Lunr/Spark/Contentful/Tests/ApiBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class ApiBaseTest extends ApiTest
use PsrLoggerTestTrait;

/**
* Test that the CentralAuthenticationStore class is passed correctly.
* Test that the credentials cache is passed correctly.
*/
public function testCasIsSetCorrectly(): void
public function testCacheIsSetCorrectly(): void
{
$this->assertPropertySame('cas', $this->cas);
$this->assertPropertySame('cache', $this->cache);
}

/**
Expand All @@ -39,7 +39,7 @@ public function testRequestsSessionIsSetCorrectly(): void
}

/**
* Test that __get() gets existing credential values from the CAS.
* Test that __get() gets existing credential values from the credentials cache.
*
* @param string $key Credential key
*
Expand All @@ -48,10 +48,14 @@ public function testRequestsSessionIsSetCorrectly(): void
*/
public function testGetExistingCredentials($key): void
{
$this->cas->expects($this->once())
->method('get')
->with('contentful', $this->equalTo($key))
->willReturn('value');
$this->cache->expects($this->once())
->method('getItem')
->with('contentful.' . $key)
->willReturn($this->item);

$this->item->expects($this->once())
->method('get')
->willReturn('value');

$this->assertEquals('value', $this->class->{$key});
}
Expand All @@ -63,14 +67,14 @@ public function testGetExistingCredentials($key): void
*/
public function testGetNonExistingCredentials(): void
{
$this->cas->expects($this->never())
->method('get');
$this->cache->expects($this->never())
->method('getItem');

$this->assertNull($this->class->invalid);
}

/**
* Test that __set() sets general credential values in the CAS.
* Test that __set() sets general credential values in the credentials cache.
*
* @param string $key Credential key
*
Expand All @@ -79,22 +83,38 @@ public function testGetNonExistingCredentials(): void
*/
public function testSetGeneralCredentials($key): void
{
$this->cas->expects($this->once())
->method('add')
->with('contentful', $key, 'value');
$this->cache->expects($this->once())
->method('getItem')
->with('contentful.' . $key)
->willReturn($this->item);

$this->cache->expects($this->once())
->method('save')
->with($this->item);

$this->item->expects($this->once())
->method('expiresAfter')
->with(600);

$this->item->expects($this->once())
->method('set')
->with('value');

$this->class->{$key} = 'value';
}

/**
* Test that setting an invalid key does not touch the CAS.
* Test that setting an invalid key does not touch the credentials cache.
*
* @covers Lunr\Spark\Contentful\Api::__set
*/
public function testSetInvalidKeyDoesNotAlterCAS(): void
public function testSetInvalidKeyDoesNotAlterCredentialsCache(): void
{
$this->cas->expects($this->never())
->method('add');
$this->cache->expects($this->never())
->method('getItem');

$this->cache->expects($this->never())
->method('save');

$this->class->invalid = 'value';
}
Expand Down
23 changes: 16 additions & 7 deletions src/Lunr/Spark/Contentful/Tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace Lunr\Spark\Contentful\Tests;

use Lunr\Spark\CentralAuthenticationStore;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheItemInterface;
use Lunr\Spark\Contentful\Api;
use Lunr\Halo\LunrBaseTest;
use Psr\Log\LoggerInterface;
Expand All @@ -27,10 +28,16 @@ abstract class ApiTest extends LunrBaseTest
{

/**
* Mock instance of the CentralAuthenticationStore class.
* @var CentralAuthenticationStore
* Mock instance of the credentials cache.
* @var CacheItemPoolInterface
*/
protected $cas;
protected $cache;

/**
* Shared instance of the cache item class.
* @var CacheItemInterface
*/
protected $item;

/**
* Mock instance of the Requests\Session class.
Expand Down Expand Up @@ -61,12 +68,13 @@ abstract class ApiTest extends LunrBaseTest
*/
public function setUp(): void
{
$this->cas = $this->getMockBuilder('Lunr\Spark\CentralAuthenticationStore')->getMock();
$this->cache = $this->getMockBuilder(CacheItemPoolInterface::class)->getMock();
$this->item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
$this->http = $this->getMockBuilder('WpOrg\Requests\Session')->getMock();
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$this->response = $this->getMockBuilder('WpOrg\Requests\Response')->getMock();

$this->class = new Api($this->cas, $this->logger, $this->http);
$this->class = new Api($this->cache, $this->logger, $this->http);

parent::baseSetUp($this->class);
}
Expand All @@ -77,7 +85,8 @@ public function setUp(): void
public function tearDown(): void
{
unset($this->class);
unset($this->cas);
unset($this->item);
unset($this->cache);
unset($this->http);
unset($this->logger);
unset($this->response);
Expand Down
6 changes: 3 additions & 3 deletions src/Lunr/Spark/Contentful/Tests/DeliveryApiBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class DeliveryApiBaseTest extends DeliveryApiTest
use PsrLoggerTestTrait;

/**
* Test that the CentralAuthenticationStore class is passed correctly.
* Test that the credentials cache is passed correctly.
*/
public function testCasIsSetCorrectly(): void
public function testCacheIsSetCorrectly(): void
{
$this->assertPropertySame('cas', $this->cas);
$this->assertPropertySame('cache', $this->cache);
}

/**
Expand Down
Loading

0 comments on commit 1f560d2

Please sign in to comment.