Skip to content

Commit 84cf843

Browse files
authored
make cache keys PSR-6 compatible by not using, and disallowing, illegal characters (#36)
1 parent 58bb16b commit 84cf843

16 files changed

+117
-44
lines changed

src/Contentful.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ private function generateCacheKey($spaceKey, $queryType, $isPreview, $disambigua
918918
{
919919
$key = $spaceKey . '-' . $queryType . (($isPreview) ? '-preview' : '');
920920
if ($disambiguator) {
921-
$key .= ':' . $disambiguator;
921+
$key .= '' . $disambiguator;
922922
}
923923
if (count($parameters) > 0) {
924924
//sort parameters by name, then key
@@ -932,11 +932,12 @@ private function generateCacheKey($spaceKey, $queryType, $isPreview, $disambigua
932932
};
933933
usort($parameters, $paramSort);
934934
$key .= '-' . implode(',', array_map(function (ParameterInterface $param) {
935-
return sprintf('(%s)%s:%s', $param->getName(), $param->getKey(), $param->getValue());
935+
return sprintf('|%s|%s↦%s', $param->getName(), $param->getKey(), $param->getValue());
936936
}, $parameters));
937937
}
938+
$illegalPsr6Characters = '{}()/\@:';
938939

939-
return $key;
940+
return strtr($key, array_fill_keys(str_split($illegalPsr6Characters), ''));
940941
}
941942

942943
/**

tests/ContentfulTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function testGetEntriesAsync()
283283
public function testCacheMissDoesFetch()
284284
{
285285
$handlerOption = $this->getSuccessHandlerOption($this->getEntriesData(), '235345lj34h53j4h');
286-
$expectedCacheKey = 'jskdfjhsdfk-entries-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
286+
$expectedCacheKey = 'jskdfjhsdfk-entries-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
287287
$cachePool = $this->getMockCachePool();
288288
$cacheItem = $this->getMockCacheItem();
289289
$cachePool
@@ -306,7 +306,7 @@ public function testCacheMissDoesFetch()
306306
public function testCacheHitUsesCacheAndDoesNotFetch()
307307
{
308308
$handlerOption = $this->getExplodyHandlerOption();
309-
$expectedCacheKey = 'jskdfjhsdfk-entries-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
309+
$expectedCacheKey = 'jskdfjhsdfk-entries-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
310310
$cachePool = $this->getMockCachePool();
311311
$cacheItem = $this->getMockCacheItem();
312312
$cachePool
@@ -332,7 +332,7 @@ public function testCacheHitUsesCacheAndDoesNotFetch()
332332
public function testUsesFallbackCacheOnRequestFailure()
333333
{
334334
$handlerOption = $this->getExplodyHandlerOption();
335-
$expectedCacheKey = 'jskdfjhsdfk-entries-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
335+
$expectedCacheKey = 'jskdfjhsdfk-entries-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
336336
$frontCacheItem = $this->getMockCacheItem();
337337
$frontCachePool = $this->getMockCachePool();
338338
$frontCachePool
@@ -366,7 +366,7 @@ public function testUsesFallbackCacheOnRequestFailure()
366366
public function testThrowsResourceUnavailableExceptionIfFailResponseCachedInFallbackCache()
367367
{
368368
$handlerOption = $this->getExplodyHandlerOption();
369-
$expectedCacheKey = 'jskdfjhsdfk-entries-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
369+
$expectedCacheKey = 'jskdfjhsdfk-entries-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
370370
$frontCacheItem = $this->getMockCacheItem();
371371
$frontCachePool = $this->getMockCachePool();
372372
$frontCachePool
@@ -398,7 +398,7 @@ public function testThrowsResourceUnavailableExceptionIfFailResponseCachedInFall
398398
public function testFailResponseDoesNotSaveIntoFallbackCacheEvenIfCachingFailResponses()
399399
{
400400
$handlerOption = $this->getExplodyHandlerOption();
401-
$expectedCacheKey = 'jskdfjhsdfk-entries-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
401+
$expectedCacheKey = 'jskdfjhsdfk-entries-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
402402
$frontCacheItem = $this->getMockCacheItem();
403403
$frontCachePool = $this->getMockCachePool();
404404
$frontCachePool
@@ -424,7 +424,7 @@ public function testFailResponseDoesNotSaveIntoFallbackCacheEvenIfCachingFailRes
424424
public function testUsesFallbackCacheOnRequestSuccessfulButInvalid()
425425
{
426426
$handlerOption = $this->getSuccessHandlerOption($this->getEmptyEntriesData(), '235345lj34h53j4h');
427-
$expectedCacheKey = 'jskdfjhsdfk-entries-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
427+
$expectedCacheKey = 'jskdfjhsdfk-entries-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
428428
$frontCacheItem = $this->getMockCacheItem();
429429
$frontCachePool = $this->getMockCachePool();
430430
$frontCachePool
@@ -466,7 +466,7 @@ public function testUsesFallbackCacheOnRequestSuccessfulButInvalid()
466466
public function testInvalidResponseDoesNotSaveIntoFallbackCacheEvenIfCachingFailResponses()
467467
{
468468
$handlerOption = $this->getSuccessHandlerOption($this->getEmptyEntriesData(), '235345lj34h53j4h');
469-
$expectedCacheKey = 'jskdfjhsdfk-entries-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
469+
$expectedCacheKey = 'jskdfjhsdfk-entries-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
470470
$frontCacheItem = $this->getMockCacheItem();
471471
$frontCachePool = $this->getMockCachePool();
472472
$frontCachePool
@@ -543,7 +543,7 @@ public function testQueryIsLoggedIfLoggerTrue()
543543
public function testUsePreviewApiForCachedGetEntriesCall()
544544
{
545545
$handlerOption = $this->getExplodyHandlerOption();
546-
$expectedCacheKey = 'jskdfjhsdfk-entries-preview-(equal)fields.old:6,(less_than)fields.ghosts[lt]:6';
546+
$expectedCacheKey = 'jskdfjhsdfk-entries-preview-|equal|fields.old↦6,|less_than|fields.ghosts[lt]6';
547547
$cachePool = $this->getMockCachePool();
548548
$cacheItem = $this->getMockCacheItem();
549549
$cachePool
@@ -562,8 +562,8 @@ public function testUsePreviewApiForCachedGetEntriesCall()
562562
$parameters = [new LessThanFilter(new FieldProperty('ghosts'), 6), new EqualFilter(new FieldProperty('old'), 6)];
563563
$entries = $contentful->getEntries($parameters);
564564
$entry = array_values(iterator_to_array($entries))[0];
565-
$this->assertInstanceOf('Markup\Contentful\EntryInterface', $entry);
566-
$this->assertInstanceOf('Markup\Contentful\EntryInterface', $entry['bestFriend']);
565+
$this->assertInstanceOf(EntryInterface::class, $entry);
566+
$this->assertInstanceOf(EntryInterface::class, $entry['bestFriend']);
567567
}
568568

569569
public function testGetContentTypes()
@@ -620,7 +620,7 @@ public function testGetContentTypeByNameAsync()
620620

621621
public function testGetNonExistentResourceWhenCachedThrowsWithoutRequest()
622622
{
623-
$expectedCacheKey = 'jskdfjhsdfk-entry:cat';
623+
$expectedCacheKey = 'jskdfjhsdfk-entrycat';
624624
$cachePool = $this->getMockCachePool();
625625
$cacheItem = $this->getMockCacheItem();
626626
$cachePool

tests/Decorator/CompositeAssetDecoratorTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Markup\Contentful\Tests\Decorator;
44

5+
use Markup\Contentful\AssetInterface;
6+
use Markup\Contentful\Decorator\AssetDecoratorInterface;
57
use Markup\Contentful\Decorator\CompositeAssetDecorator;
68
use Mockery as m;
79

@@ -19,7 +21,7 @@ protected function tearDown()
1921

2022
public function testIsDecorator()
2123
{
22-
$this->assertInstanceOf('Markup\Contentful\Decorator\AssetDecoratorInterface', $this->composite);
24+
$this->assertInstanceOf(AssetDecoratorInterface::class, $this->composite);
2325
}
2426

2527
public function testCompositeDoesNullDecorationByDefault()
@@ -54,11 +56,11 @@ public function testCompositeWithTwoDecoratorsDecoratesInLifoOrder()
5456

5557
private function getMockAsset()
5658
{
57-
return m::mock('Markup\Contentful\AssetInterface')->shouldIgnoreMissing();
59+
return m::mock(AssetInterface::class)->shouldIgnoreMissing();
5860
}
5961

6062
private function getMockDecorator()
6163
{
62-
return m::mock('Markup\Contentful\Decorator\AssetDecoratorInterface')->shouldIgnoreMissing();
64+
return m::mock(AssetDecoratorInterface::class)->shouldIgnoreMissing();
6365
}
6466
}

tests/Decorator/DecoratedAssetTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Markup\Contentful\Tests\Decorator;
44

5+
use Markup\Contentful\AssetInterface;
56
use Mockery as m;
67

78
class DecoratedAssetTest extends \PHPUnit_Framework_TestCase
89
{
910
protected function setUp()
1011
{
11-
$this->asset = m::mock('Markup\Contentful\AssetInterface');
12+
$this->asset = m::mock(AssetInterface::class);
1213
$this->decorated = new ConcreteDecoratedAsset($this->asset);
1314
}
1415

tests/Filter/ContentTypeFilterProviderTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace Markup\Contentful\Tests\Filter;
44

5+
use Markup\Contentful\Contentful;
6+
use Markup\Contentful\ContentTypeInterface;
7+
use Markup\Contentful\Filter\ContentTypeFilter;
58
use Markup\Contentful\Filter\ContentTypeFilterProvider;
69
use Mockery as m;
710

811
class ContentTypeFilterProviderTest extends \PHPUnit_Framework_TestCase
912
{
1013
protected function setUp()
1114
{
12-
$this->contentful = m::mock('Markup\Contentful\Contentful');
15+
$this->contentful = m::mock(Contentful::class);
1316
$this->provider = new ContentTypeFilterProvider($this->contentful);
1417
}
1518

@@ -20,7 +23,7 @@ protected function tearDown()
2023

2124
public function testCreateForExistingContentType()
2225
{
23-
$contentType = m::mock('Markup\Contentful\ContentTypeInterface');
26+
$contentType = m::mock(ContentTypeInterface::class);
2427
$id = 42;
2528
$contentType
2629
->shouldReceive('getId')
@@ -34,7 +37,7 @@ public function testCreateForExistingContentType()
3437
->with($name, m::any())
3538
->andReturn($contentType);
3639
$filter = $this->provider->createForContentTypeName($name);
37-
$this->assertInstanceOf('Markup\Contentful\Filter\ContentTypeFilter', $filter);
40+
$this->assertInstanceOf(ContentTypeFilter::class, $filter);
3841
$this->assertEquals($id, $filter->getValue());
3942
}
4043

tests/Filter/ContentTypeFilterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Markup\Contentful\Tests\Filter;
44

55
use Markup\Contentful\Filter\ContentTypeFilter;
6+
use Markup\Contentful\FilterInterface;
67

78
class ContentTypeFilterTest extends \PHPUnit_Framework_TestCase
89
{
@@ -14,7 +15,7 @@ protected function setUp()
1415

1516
public function testIsFilter()
1617
{
17-
$this->assertInstanceOf('Markup\Contentful\FilterInterface', $this->filter);
18+
$this->assertInstanceOf(FilterInterface::class, $this->filter);
1819
}
1920

2021
public function testGetKey()

tests/Filter/EqualFilterTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@
33
namespace Markup\Contentful\Tests\Filter;
44

55
use Markup\Contentful\Filter\EqualFilter;
6+
use Markup\Contentful\Filter\PropertyFilter;
7+
use Markup\Contentful\FilterInterface;
8+
use Markup\Contentful\PropertyInterface;
69
use Mockery as m;
710

811
class EqualFilterTest extends \PHPUnit_Framework_TestCase
912
{
13+
/**
14+
* @var PropertyInterface|m\MockInterface
15+
*/
16+
private $property;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $value;
22+
23+
/**
24+
* @var EqualFilter
25+
*/
26+
private $filter;
27+
1028
protected function setUp()
1129
{
12-
$this->property = m::mock('Markup\Contentful\PropertyInterface');
30+
$this->property = m::mock(PropertyInterface::class);
1331
$this->value = 'value';
1432
$this->filter = new EqualFilter($this->property, $this->value);
1533
}
@@ -21,12 +39,12 @@ protected function tearDown()
2139

2240
public function testIsFilter()
2341
{
24-
$this->assertInstanceOf('Markup\Contentful\FilterInterface', $this->filter);
42+
$this->assertInstanceOf(FilterInterface::class, $this->filter);
2543
}
2644

2745
public function testIsPropertyFilter()
2846
{
29-
$this->assertInstanceOf('Markup\Contentful\Filter\PropertyFilter', $this->filter);
47+
$this->assertInstanceOf(PropertyFilter::class, $this->filter);
3048
}
3149

3250
public function testGetKey()

tests/Filter/ExcludeFilterTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@
33
namespace Markup\Contentful\Tests\Filter;
44

55
use Markup\Contentful\Filter\ExcludeFilter;
6+
use Markup\Contentful\Filter\PropertyFilter;
7+
use Markup\Contentful\FilterInterface;
8+
use Markup\Contentful\PropertyInterface;
69
use Mockery as m;
710

811
class ExcludeFilterTest extends \PHPUnit_Framework_TestCase
912
{
13+
/**
14+
* @var PropertyInterface|m\MockInterface
15+
*/
16+
private $property;
17+
18+
/**
19+
* @var string[]
20+
*/
21+
private $values;
22+
23+
/**
24+
* @var ExcludeFilter
25+
*/
26+
private $filter;
27+
1028
protected function setUp()
1129
{
12-
$this->property = m::mock('Markup\Contentful\PropertyInterface');
30+
$this->property = m::mock(PropertyInterface::class);
1331
$this->values = ['foo', 'bar'];
1432
$this->filter = new ExcludeFilter($this->property, $this->values);
1533
}
@@ -21,12 +39,12 @@ protected function tearDown()
2139

2240
public function testIsFilter()
2341
{
24-
$this->assertInstanceOf('Markup\Contentful\FilterInterface', $this->filter);
42+
$this->assertInstanceOf(FilterInterface::class, $this->filter);
2543
}
2644

2745
public function testIsPropertyFilter()
2846
{
29-
$this->assertInstanceOf('Markup\Contentful\Filter\PropertyFilter', $this->filter);
47+
$this->assertInstanceOf(PropertyFilter::class, $this->filter);
3048
}
3149

3250
public function testGetKey()

tests/Filter/ExistsFilterTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@
33
namespace Markup\Contentful\Tests\Filter;
44

55
use Markup\Contentful\Filter\ExistsFilter;
6+
use Markup\Contentful\FilterInterface;
7+
use Markup\Contentful\PropertyInterface;
68
use Mockery as m;
79

810
class ExistsFilterTest extends \PHPUnit_Framework_TestCase
911
{
12+
/**
13+
* @var PropertyInterface|m\MockInterface
14+
*/
15+
private $property;
16+
17+
/**
18+
* @var bool
19+
*/
20+
private $value;
21+
22+
/**
23+
* @var ExistsFilter
24+
*/
25+
private $filter;
26+
1027
protected function setUp()
1128
{
12-
$this->property = m::mock('Markup\Contentful\PropertyInterface');
29+
$this->property = m::mock(PropertyInterface::class);
1330
$this->propertyKey = 'prop';
1431
$this->property
1532
->shouldReceive('getKey')
@@ -25,7 +42,7 @@ protected function tearDown()
2542

2643
public function testIsFilter()
2744
{
28-
$this->assertInstanceOf('Markup\Contentful\FilterInterface', $this->filter);
45+
$this->assertInstanceOf(FilterInterface::class, $this->filter);
2946
}
3047

3148
public function testGetKey()

tests/Filter/MimeTypeGroupFilterTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Markup\Contentful\Tests\Filter;
44

55
use Markup\Contentful\Filter\MimeTypeGroupFilter;
6+
use Markup\Contentful\Filter\PropertyFilter;
7+
use Markup\Contentful\FilterInterface;
68

79
class MimeTypeGroupFilterTest extends \PHPUnit_Framework_TestCase
810
{
@@ -14,12 +16,12 @@ protected function setUp()
1416

1517
public function testIsFilter()
1618
{
17-
$this->assertInstanceOf('Markup\Contentful\FilterInterface', $this->filter);
19+
$this->assertInstanceOf(FilterInterface::class, $this->filter);
1820
}
1921

2022
public function testIsPropertyFilter()
2123
{
22-
$this->assertInstanceOf('Markup\Contentful\Filter\PropertyFilter', $this->filter);
24+
$this->assertInstanceOf(PropertyFilter::class, $this->filter);
2325
}
2426

2527
public function testGetKey()

0 commit comments

Comments
 (0)