Skip to content

Commit

Permalink
Merge 6fac7c9 into 23d621d
Browse files Browse the repository at this point in the history
  • Loading branch information
iganev committed Jul 27, 2020
2 parents 23d621d + 6fac7c9 commit 13f0a27
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/Psr/SimpleCache/SimpleCacheDecorator.php
Expand Up @@ -347,10 +347,20 @@ private function validateKey($key)
));
}

if (preg_match('/^.{65,}/u', $key)) {
// get storage adapter capability value
$max_key_length = $this->storage->getCapabilities()->getMaxKeyLength();

if ($max_key_length === -1) {
// default to <= 64 characters as per PSR-16
$max_key_length = 64;
}

//skip length check, if adapter capability reports unlimited key length
if ($max_key_length > 0 && preg_match('/^.{'.($max_key_length+1).',}/u', $key)) {
throw new SimpleCacheInvalidArgumentException(sprintf(
'Invalid key "%s" provided; key is too long. Must be no more than 64 characters',
$key
'Invalid key "%s" provided; key is too long. Must be no more than %d characters',
$key,
$max_key_length
));
}
}
Expand Down
33 changes: 30 additions & 3 deletions test/Psr/SimpleCache/SimpleCacheDecoratorTest.php
Expand Up @@ -72,13 +72,15 @@ public function setUp()
private function getMockCapabilities(
array $supportedDataTypes = null,
$staticTtl = true,
$minTtl = 60
$minTtl = 60,
$maxKeyLength = -1
) {
$supportedDataTypes = $supportedDataTypes ?: $this->requiredTypes;
$capabilities = $this->prophesize(Capabilities::class);
$capabilities->getSupportedDatatypes()->willReturn($supportedDataTypes);
$capabilities->getStaticTtl()->willReturn($staticTtl);
$capabilities->getMinTtl()->willReturn($minTtl);
$capabilities->getMaxKeyLength()->willReturn($maxKeyLength);

return $capabilities;
}
Expand All @@ -91,9 +93,10 @@ public function mockCapabilities(
ObjectProphecy $storage,
array $supportedDataTypes = null,
$staticTtl = true,
$minTtl = 60
$minTtl = 60,
$maxKeyLength = -1
) {
$capabilities = $this->getMockCapabilities($supportedDataTypes, $staticTtl, $minTtl);
$capabilities = $this->getMockCapabilities($supportedDataTypes, $staticTtl, $minTtl, $maxKeyLength);

$storage->getCapabilities()->will([$capabilities, 'reveal']);
}
Expand Down Expand Up @@ -338,6 +341,30 @@ public function testSetShouldRaisePsrInvalidArgumentExceptionForInvalidKeys($key
$this->expectExceptionMessage($expectedMessage);
$this->cache->set($key, 'value');
}

/**
* @depends testSetShouldRaisePsrInvalidArgumentExceptionForInvalidKeys
*/
public function testSetShouldAcknowledgeStorageAdapterMaxKeyLengthWithPsrDecorator()
{
$key_valid_length = str_repeat('abcd', 17); // length 68
$key_invalid_length = str_repeat('abcd', 63); // length 252

$storage = $this->prophesize(StorageInterface::class);
$storage->getOptions()->will([$this->options, 'reveal']);
$this->mockCapabilities($storage, null, false, 60, 251);
$storage->setItem($key_valid_length, 'value')->shouldBeCalledTimes(1)->willReturn(true);
$storage->setItem($key_invalid_length, 'value')->shouldNotBeCalled();

$cache = new SimpleCacheDecorator($storage->reveal());

$this->assertTrue($cache->set($key_valid_length, 'value'));

$this->expectException(SimpleCacheInvalidArgumentException::class);
$this->expectExceptionMessage('too long');

$cache->set($key_invalid_length, 'value');
}

public function testSetShouldReRaiseExceptionThrownByStorage()
{
Expand Down

0 comments on commit 13f0a27

Please sign in to comment.