diff --git a/composer.json b/composer.json index bc2d47b..e8a8ef5 100644 --- a/composer.json +++ b/composer.json @@ -19,16 +19,15 @@ } ], "require": { - "php": "^7.4 || ^8.0", - "silverstripe/framework": "^4.10", - "silverstripe/reports": "^4.4", - "symbiote/silverstripe-queuedjobs": "^4.1", - "guzzlehttp/guzzle": "^6.3 || ^7.0" + "php": "^8.1", + "silverstripe/framework": "^5", + "silverstripe/reports": "^5", + "symbiote/silverstripe-queuedjobs": "^5" }, "require-dev": { "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "^3", - "symfony/thanks": "^1.0" + "symfony/thanks": "^1.2" }, "autoload": { "psr-4": { diff --git a/src/Util/ApiLoader.php b/src/Util/ApiLoader.php index 65939b4..f6f05fd 100644 --- a/src/Util/ApiLoader.php +++ b/src/Util/ApiLoader.php @@ -134,7 +134,7 @@ public function getClientOptions() */ protected function getFromCache() { - $cacheKey = $this->getCacheKey(); + $cacheKey = $this->getCacheKey() ?? ''; $result = $this->getCache()->get($cacheKey, false); if ($result === false) { return false; @@ -155,6 +155,8 @@ protected function getFromCache() */ protected function setToCache($cacheKey, $value, $ttl = null) { + $cacheKey = $cacheKey ?? ''; + // Seralize as JSON to ensure array etc can be stored $value = json_encode($value); diff --git a/tests/Util/ApiLoaderTest.php b/tests/Util/ApiLoaderTest.php index 023ff2d..9ca6e74 100644 --- a/tests/Util/ApiLoaderTest.php +++ b/tests/Util/ApiLoaderTest.php @@ -9,7 +9,7 @@ use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; use SilverStripe\Dev\SapphireTest; -use Symfony\Component\Cache\Simple\NullCache; +use Psr\SimpleCache\CacheInterface; class ApiLoaderTest extends SapphireTest { @@ -88,9 +88,7 @@ public function testCacheControlSettingsAreRespected() { $fakeAddons = ['foo/bar', 'bin/baz']; - $cacheMock = $this->getMockBuilder(NullCache::class) - ->setMethods(['get', 'set']) - ->getMock(); + $cacheMock = $this->getMockCacheInterface(); $cacheMock->expects($this->once())->method('get')->will($this->returnValue(false)); $cacheMock->expects($this->once()) @@ -114,9 +112,7 @@ public function testCachedAddonsAreUsedWhenAvailable() { $fakeAddons = ['foo/bar', 'bin/baz']; - $cacheMock = $this->getMockBuilder(NullCache::class) - ->setMethods(['get', 'set']) - ->getMock(); + $cacheMock = $this->getMockCacheInterface(); $cacheMock->expects($this->once())->method('get')->will($this->returnValue(json_encode($fakeAddons))); $loader = $this->getLoader($cacheMock); @@ -153,9 +149,7 @@ protected function getMockClient(Response $withResponse) protected function getLoader($cacheMock = false) { if (!$cacheMock) { - $cacheMock = $this->getMockBuilder(NullCache::class) - ->setMethods(['get', 'set']) - ->getMock(); + $cacheMock = $this->getMockCacheInterface(); $cacheMock->expects($this->any())->method('get')->will($this->returnValue(false)); $cacheMock->expects($this->any())->method('set')->will($this->returnValue(true)); @@ -168,4 +162,14 @@ protected function getLoader($cacheMock = false) return $loader; } + + protected function getMockCacheInterface() + { + $methods = ['get', 'set', 'has', 'delete', 'getMultiple', 'setMultiple', 'clear', 'deleteMultiple']; + $mock = $this->getMockBuilder(CacheInterface::class) + ->setMethods($methods) + ->getMock(); + + return $mock; + } }