From 1f560d251229e97e38293ed335857f5ae2cd5733 Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Thu, 7 Dec 2023 20:30:50 +0100 Subject: [PATCH] Spark: Use Psr\Cache instead of CAS for caching credentials Reviewed at https://reviews.lunr.nl/r/1078/ --- src/Lunr/Spark/Contentful/Api.php | 29 ++++---- src/Lunr/Spark/Contentful/DeliveryApi.php | 12 ++-- src/Lunr/Spark/Contentful/ManagementApi.php | 12 ++-- src/Lunr/Spark/Contentful/PreviewApi.php | 12 ++-- .../Spark/Contentful/Tests/ApiBaseTest.php | 56 ++++++++++----- src/Lunr/Spark/Contentful/Tests/ApiTest.php | 23 ++++-- .../Contentful/Tests/DeliveryApiBaseTest.php | 6 +- .../Tests/DeliveryApiGetAssetsTest.php | 72 ++++++++++++------- .../Tests/DeliveryApiGetEntriesTest.php | 72 ++++++++++++------- .../Contentful/Tests/DeliveryApiTest.php | 23 ++++-- .../Tests/ManagementApiBaseTest.php | 6 +- .../Tests/ManagementApiCreateEntryTest.php | 24 ++++--- .../Tests/ManagementApiGetEntryTest.php | 24 ++++--- .../Tests/ManagementApiPublishEntryTest.php | 24 ++++--- .../Contentful/Tests/ManagementApiTest.php | 23 ++++-- .../Tests/ManagementApiUnpublishEntryTest.php | 24 ++++--- .../Tests/ManagementApiUpdateEntryTest.php | 24 ++++--- .../Contentful/Tests/PreviewApiBaseTest.php | 6 +- .../Spark/Contentful/Tests/PreviewApiTest.php | 23 ++++-- 19 files changed, 322 insertions(+), 173 deletions(-) diff --git a/src/Lunr/Spark/Contentful/Api.php b/src/Lunr/Spark/Contentful/Api.php index 0f6a6b2b..934e252a 100644 --- a/src/Lunr/Spark/Contentful/Api.php +++ b/src/Lunr/Spark/Contentful/Api.php @@ -10,7 +10,7 @@ namespace Lunr\Spark\Contentful; -use Lunr\Spark\CentralAuthenticationStore; +use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; use WpOrg\Requests\Session; @@ -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. @@ -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 = ''; @@ -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); @@ -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; } @@ -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; diff --git a/src/Lunr/Spark/Contentful/DeliveryApi.php b/src/Lunr/Spark/Contentful/DeliveryApi.php index d015aaad..5becb3cd 100644 --- a/src/Lunr/Spark/Contentful/DeliveryApi.php +++ b/src/Lunr/Spark/Contentful/DeliveryApi.php @@ -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; @@ -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); } /** diff --git a/src/Lunr/Spark/Contentful/ManagementApi.php b/src/Lunr/Spark/Contentful/ManagementApi.php index f78758bb..51c3c8c3 100644 --- a/src/Lunr/Spark/Contentful/ManagementApi.php +++ b/src/Lunr/Spark/Contentful/ManagementApi.php @@ -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; @@ -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); } /** diff --git a/src/Lunr/Spark/Contentful/PreviewApi.php b/src/Lunr/Spark/Contentful/PreviewApi.php index 5322f44e..58ceb87c 100644 --- a/src/Lunr/Spark/Contentful/PreviewApi.php +++ b/src/Lunr/Spark/Contentful/PreviewApi.php @@ -10,7 +10,7 @@ namespace Lunr\Spark\Contentful; -use Lunr\Spark\CentralAuthenticationStore; +use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; use WpOrg\Requests\Session; @@ -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); } /** diff --git a/src/Lunr/Spark/Contentful/Tests/ApiBaseTest.php b/src/Lunr/Spark/Contentful/Tests/ApiBaseTest.php index 3807893d..275ccbad 100644 --- a/src/Lunr/Spark/Contentful/Tests/ApiBaseTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ApiBaseTest.php @@ -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); } /** @@ -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 * @@ -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}); } @@ -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 * @@ -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'; } diff --git a/src/Lunr/Spark/Contentful/Tests/ApiTest.php b/src/Lunr/Spark/Contentful/Tests/ApiTest.php index 05a60c03..981173aa 100644 --- a/src/Lunr/Spark/Contentful/Tests/ApiTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ApiTest.php @@ -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; @@ -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. @@ -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); } @@ -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); diff --git a/src/Lunr/Spark/Contentful/Tests/DeliveryApiBaseTest.php b/src/Lunr/Spark/Contentful/Tests/DeliveryApiBaseTest.php index c0f4fc7c..2406f13a 100644 --- a/src/Lunr/Spark/Contentful/Tests/DeliveryApiBaseTest.php +++ b/src/Lunr/Spark/Contentful/Tests/DeliveryApiBaseTest.php @@ -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); } /** diff --git a/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetAssetsTest.php b/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetAssetsTest.php index 51157564..0fd0102c 100644 --- a/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetAssetsTest.php +++ b/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetAssetsTest.php @@ -30,10 +30,14 @@ public function testGetAssetsWithoutFiltersReturnsEmptyResultOnRequestError(): v { $this->set_reflection_property_value('space', '5p4c31D'); - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/spaces/5p4c31D/assets'; $params = [ 'access_token' => 'token' ]; @@ -85,10 +89,14 @@ public function testGetAssetsWithFiltersReturnsEmptyResultOnRequestError(): void { $this->set_reflection_property_value('space', '5p4c31D'); - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/spaces/5p4c31D/assets'; $params = [ 'mimetype_group' => 'image', 'access_token' => 'token' ]; @@ -140,10 +148,14 @@ public function testGetAssetsWithoutFiltersReturnsEmptyResultOnRequestFailure(): { $this->set_reflection_property_value('space', '5p4c31D'); - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/spaces/5p4c31D/assets'; $params = [ 'access_token' => 'token' ]; @@ -183,10 +195,14 @@ public function testGetAssetsWithFiltersReturnsEmptyResultOnRequestFailure(): vo { $this->set_reflection_property_value('space', '5p4c31D'); - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/spaces/5p4c31D/assets'; $params = [ 'mimetype_group' => 'image', 'access_token' => 'token' ]; @@ -231,10 +247,14 @@ public function testGetAssetsWithoutFiltersReturnsResultsOnSuccessfulRequest(): 'param2' => 2, ]; - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/spaces/5p4c31D/assets'; $params = [ 'access_token' => 'token' ]; @@ -266,10 +286,14 @@ public function testGetAssetsWithFiltersReturnsResultsOnSuccessfulRequest(): voi 'param2' => 2, ]; - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/spaces/5p4c31D/assets'; $params = [ 'mimetype_group' => 'image', 'access_token' => 'token' ]; diff --git a/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetEntriesTest.php b/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetEntriesTest.php index 8fbb7820..0ffaa0b3 100644 --- a/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetEntriesTest.php +++ b/src/Lunr/Spark/Contentful/Tests/DeliveryApiGetEntriesTest.php @@ -28,10 +28,14 @@ class DeliveryApiGetEntriesTest extends DeliveryApiTest */ public function testGetEntriesWithoutFiltersReturnsEmptyResultOnRequestError(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/entries'; $params = [ 'access_token' => 'token', 'content_type' => 'foo' ]; @@ -81,10 +85,14 @@ public function testGetEntriesWithoutFiltersReturnsEmptyResultOnRequestError(): */ public function testGetEntriesWithFiltersReturnsEmptyResultOnRequestError(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/entries'; $params = [ 'field.urlName[match]' => 'bar', 'access_token' => 'token', 'content_type' => 'foo' ]; @@ -134,10 +142,14 @@ public function testGetEntriesWithFiltersReturnsEmptyResultOnRequestError(): voi */ public function testGetEntriesWithoutFiltersReturnsEmptyResultOnRequestFailure(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/entries'; $params = [ 'access_token' => 'token', 'content_type' => 'foo' ]; @@ -175,10 +187,14 @@ public function testGetEntriesWithoutFiltersReturnsEmptyResultOnRequestFailure() */ public function testGetEntriesWithFiltersReturnsEmptyResultOnRequestFailure(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/entries'; $params = [ 'field.urlName[match]' => 'bar', 'access_token' => 'token', 'content_type' => 'foo' ]; @@ -221,10 +237,14 @@ public function testGetEntriesWithoutFiltersReturnsResultsOnSuccessfulRequest(): 'param2' => 2, ]; - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/entries'; $params = [ 'access_token' => 'token', 'content_type' => 'foo' ]; @@ -254,10 +274,14 @@ public function testGetEntriesWithFiltersReturnsResultsOnSuccessfulRequest(): vo 'param2' => 2, ]; - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'access_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.access_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://cdn.contentful.com/entries'; $params = [ 'field.urlName[match]' => 'bar', 'access_token' => 'token', 'content_type' => 'foo' ]; diff --git a/src/Lunr/Spark/Contentful/Tests/DeliveryApiTest.php b/src/Lunr/Spark/Contentful/Tests/DeliveryApiTest.php index 9a12a7e3..ca018b35 100644 --- a/src/Lunr/Spark/Contentful/Tests/DeliveryApiTest.php +++ b/src/Lunr/Spark/Contentful/Tests/DeliveryApiTest.php @@ -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\DeliveryApi; use Lunr\Halo\LunrBaseTest; use Psr\Log\LoggerInterface; @@ -27,10 +28,16 @@ abstract class DeliveryApiTest 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. @@ -61,12 +68,13 @@ abstract class DeliveryApiTest 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 DeliveryApi($this->cas, $this->logger, $this->http); + $this->class = new DeliveryApi($this->cache, $this->logger, $this->http); parent::baseSetUp($this->class); } @@ -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); diff --git a/src/Lunr/Spark/Contentful/Tests/ManagementApiBaseTest.php b/src/Lunr/Spark/Contentful/Tests/ManagementApiBaseTest.php index a2a01cd3..064019ef 100644 --- a/src/Lunr/Spark/Contentful/Tests/ManagementApiBaseTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ManagementApiBaseTest.php @@ -23,11 +23,11 @@ class ManagementApiBaseTest extends ManagementApiTest 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); } /** diff --git a/src/Lunr/Spark/Contentful/Tests/ManagementApiCreateEntryTest.php b/src/Lunr/Spark/Contentful/Tests/ManagementApiCreateEntryTest.php index 48de6a9f..71dbdc44 100644 --- a/src/Lunr/Spark/Contentful/Tests/ManagementApiCreateEntryTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ManagementApiCreateEntryTest.php @@ -28,10 +28,14 @@ class ManagementApiCreateEntryTest extends ManagementApiTest */ public function testCreateEntryOnRequestError(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('Token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('Token'); $url = 'https://api.contentful.com/entries'; $headers = [ @@ -68,10 +72,14 @@ public function testCreateEntryOnRequestError(): void */ public function testCreateEntryOnSuccessfulRequest(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('Token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('Token'); $url = 'https://api.contentful.com/entries'; $headers = [ diff --git a/src/Lunr/Spark/Contentful/Tests/ManagementApiGetEntryTest.php b/src/Lunr/Spark/Contentful/Tests/ManagementApiGetEntryTest.php index 5d109190..99a462d9 100644 --- a/src/Lunr/Spark/Contentful/Tests/ManagementApiGetEntryTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ManagementApiGetEntryTest.php @@ -28,10 +28,14 @@ class ManagementApiGetEntryTest extends ManagementApiTest */ public function testGetEntryOnRequestError(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('Token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('Token'); $url = 'https://api.contentful.com/entries/123456'; $headers = [ 'Authorization' => 'Bearer Token' ]; @@ -65,10 +69,14 @@ public function testGetEntryOnRequestError(): void */ public function testGetEntryOnSuccessfulRequest(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('Token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('Token'); $url = 'https://api.contentful.com/entries/123456'; $headers = [ 'Authorization' => 'Bearer Token' ]; diff --git a/src/Lunr/Spark/Contentful/Tests/ManagementApiPublishEntryTest.php b/src/Lunr/Spark/Contentful/Tests/ManagementApiPublishEntryTest.php index 9a36c348..484d27cc 100644 --- a/src/Lunr/Spark/Contentful/Tests/ManagementApiPublishEntryTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ManagementApiPublishEntryTest.php @@ -28,10 +28,14 @@ class ManagementApiPublishEntryTest extends ManagementApiTest */ public function testPublishEntryOnRequestError(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://api.contentful.com/entries/123456/published'; $headers = [ @@ -68,10 +72,14 @@ public function testPublishEntryOnRequestError(): void */ public function testPublishEntryOnSuccessfulRequest(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://api.contentful.com/entries/123456/published'; $headers = [ diff --git a/src/Lunr/Spark/Contentful/Tests/ManagementApiTest.php b/src/Lunr/Spark/Contentful/Tests/ManagementApiTest.php index c194d5fb..c4dc899c 100644 --- a/src/Lunr/Spark/Contentful/Tests/ManagementApiTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ManagementApiTest.php @@ -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\ManagementApi; use Lunr\Halo\LunrBaseTest; use Psr\Log\LoggerInterface; @@ -27,10 +28,16 @@ abstract class ManagementApiTest 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. @@ -61,12 +68,13 @@ abstract class ManagementApiTest 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 ManagementApi($this->cas, $this->logger, $this->http); + $this->class = new ManagementApi($this->cache, $this->logger, $this->http); parent::baseSetUp($this->class); } @@ -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); diff --git a/src/Lunr/Spark/Contentful/Tests/ManagementApiUnpublishEntryTest.php b/src/Lunr/Spark/Contentful/Tests/ManagementApiUnpublishEntryTest.php index 56c28a54..379f3fa9 100644 --- a/src/Lunr/Spark/Contentful/Tests/ManagementApiUnpublishEntryTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ManagementApiUnpublishEntryTest.php @@ -28,10 +28,14 @@ class ManagementApiUnpublishEntryTest extends ManagementApiTest */ public function testUnpublishEntryOnRequestError(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('Token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('Token'); $url = 'https://api.contentful.com/entries/123456/published'; $headers = [ 'Authorization' => 'Bearer Token' ]; @@ -65,10 +69,14 @@ public function testUnpublishEntryOnRequestError(): void */ public function testUnpublishEntryOnSuccessfulRequest(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('Token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('Token'); $url = 'https://api.contentful.com/entries/123456/published'; $headers = [ 'Authorization' => 'Bearer Token' ]; diff --git a/src/Lunr/Spark/Contentful/Tests/ManagementApiUpdateEntryTest.php b/src/Lunr/Spark/Contentful/Tests/ManagementApiUpdateEntryTest.php index c4b3d832..218fa414 100644 --- a/src/Lunr/Spark/Contentful/Tests/ManagementApiUpdateEntryTest.php +++ b/src/Lunr/Spark/Contentful/Tests/ManagementApiUpdateEntryTest.php @@ -28,10 +28,14 @@ class ManagementApiUpdateEntryTest extends ManagementApiTest */ public function testUpdateEntryOnRequestError(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://api.contentful.com/entries/123456'; $headers = [ @@ -68,10 +72,14 @@ public function testUpdateEntryOnRequestError(): void */ public function testUpdateEntryOnSuccessfulRequest(): void { - $this->cas->expects($this->once()) - ->method('get') - ->with('contentful', 'management_token') - ->willReturn('token'); + $this->cache->expects($this->once()) + ->method('getItem') + ->with('contentful.management_token') + ->willReturn($this->item); + + $this->item->expects($this->once()) + ->method('get') + ->willReturn('token'); $url = 'https://api.contentful.com/entries/123456'; $headers = [ diff --git a/src/Lunr/Spark/Contentful/Tests/PreviewApiBaseTest.php b/src/Lunr/Spark/Contentful/Tests/PreviewApiBaseTest.php index 46fee7ee..6ff3fa50 100644 --- a/src/Lunr/Spark/Contentful/Tests/PreviewApiBaseTest.php +++ b/src/Lunr/Spark/Contentful/Tests/PreviewApiBaseTest.php @@ -23,11 +23,11 @@ class PreviewApiBaseTest extends PreviewApiTest 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); } /** diff --git a/src/Lunr/Spark/Contentful/Tests/PreviewApiTest.php b/src/Lunr/Spark/Contentful/Tests/PreviewApiTest.php index 4f562275..e0cfb4a7 100644 --- a/src/Lunr/Spark/Contentful/Tests/PreviewApiTest.php +++ b/src/Lunr/Spark/Contentful/Tests/PreviewApiTest.php @@ -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\PreviewApi; use Lunr\Halo\LunrBaseTest; use Psr\Log\LoggerInterface; @@ -27,10 +28,16 @@ abstract class PreviewApiTest 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. @@ -61,12 +68,13 @@ abstract class PreviewApiTest 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 PreviewApi($this->cas, $this->logger, $this->http); + $this->class = new PreviewApi($this->cache, $this->logger, $this->http); parent::baseSetUp($this->class); } @@ -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);