Skip to content

Commit 506e1c6

Browse files
committed
Added test for reserved characters in cache keys.
1 parent e6d5d46 commit 506e1c6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Connector/CachingConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ public function isCacheEnabled()
7676

7777
private function hash(array $structure)
7878
{
79-
return str_replace(str_split('{}()/\@:'), '.', json_encode($structure));
79+
return str_replace(str_split('{}()/\@:'), '.', json_encode($structure, JSON_UNESCAPED_SLASHES));
8080
}
8181
}

test/Integration/Porter/Connector/CachingConnectorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
55
use Mockery\MockInterface;
6+
use Psr\Cache\CacheItemInterface;
67
use Psr\Cache\CacheItemPoolInterface;
78
use ScriptFUSION\Porter\Cache\MemoryCache;
89
use ScriptFUSION\Porter\Connector\CachingConnector;
@@ -88,4 +89,24 @@ public function testEnableCache()
8889
$this->connector->enableCache();
8990
self::assertTrue($this->connector->isCacheEnabled());
9091
}
92+
93+
public function testCacheKeyExcludesReservedCharacters()
94+
{
95+
$reservedCharacters = '{}()/\@:';
96+
97+
$this->connector->setCache($cache = \Mockery::spy(CacheItemPoolInterface::class));
98+
99+
$cache->shouldReceive('hasItem')
100+
->andReturnUsing(
101+
function ($key) use ($reservedCharacters) {
102+
foreach (str_split($reservedCharacters) as $reservedCharacter) {
103+
self::assertNotContains($reservedCharacter, $key);
104+
}
105+
}
106+
)->once()
107+
->shouldReceive('getItem')->andReturnSelf()
108+
->shouldReceive('set')->andReturn(\Mockery::mock(CacheItemInterface::class));
109+
110+
$this->connector->fetch($reservedCharacters, (new TestOptions)->setFoo($reservedCharacters));
111+
}
91112
}

0 commit comments

Comments
 (0)