Skip to content

Commit

Permalink
Fix key sanitizer behaviour for PSR16
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed May 6, 2024
1 parent 07b7842 commit 8824fc3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/Jackalope/Transport/DoctrineDBAL/CachedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function __construct(FactoryInterface $factory, Connection $conn, array $
$this->caches = $caches;
$this->keySanitizer = static function ($cacheKey) {
return str_replace(
[' ', ':', '{', '}', '(', ')', '.', '@', '/', '"', '\\'],
['_', '|', '[', ']', '[', ']', '|', 'a', '|', '\'', '|'],
$cacheKey
['%', '.'],
['_', '|'],
\urlencode($cacheKey)
);
};
}
Expand Down
22 changes: 9 additions & 13 deletions tests/Jackalope/Transport/DoctrineDBAL/CachedClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,25 @@ public function testArrayObjectIsConvertedToArray()
public function testCacheHit()
{
$cache = new \stdClass();
$this->cacheMock->method('fetch')->with('nodes|_|test,_tests')->willReturn($cache);
$this->cacheMock->method('fetch')->with('nodes_3A+_2Ftest_2C+tests')->willReturn($cache);

$this->assertSame($cache, $this->transport->getNode('/test'));
}

/**
* The default key sanitizer replaces spaces with underscores
* The default key sanitizer keeps the cache key compatible with PSR16
*/
public function testDefaultKeySanitizer()
{
$first = true;
$this->cacheMock
->method('fetch')
->with(self::callback(function ($arg) use (&$first) {
self::assertEquals($first ? 'nodetypes|_a|0|[]' : 'node_types', $arg);
$first = false;
$client = $this->getClient($this->getConnection());
$reflection = new \ReflectionClass($client);
$keySanitizerProperty = $reflection->getProperty('keySanitizer');
$keySanitizerProperty->setAccessible(true);
$defaultKeySanitizer = $keySanitizerProperty->getValue($client);

return true;
}));
$result = $defaultKeySanitizer(' :{}().@/"\\'); // not allowed PSR16 keys

/** @var CachedClient $cachedClient */
$cachedClient = $this->transport;
$cachedClient->getNodeTypes();
$this->assertEquals('+_3A_7B_7D_28_29|_40_2F_22_5C', $result);
}

public function testCustomkeySanitizer()
Expand Down

0 comments on commit 8824fc3

Please sign in to comment.