From 68ce6c442bb5761fea45d966fbdecdb969f5c899 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 6 Mar 2026 21:25:52 +1300 Subject: [PATCH 1/2] Add cache key tests --- src/Database/Database.php | 2 +- tests/unit/CacheKeyTest.php | 130 ++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tests/unit/CacheKeyTest.php diff --git a/src/Database/Database.php b/src/Database/Database.php index 52d2b2761..23eb1e0bc 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1141,7 +1141,7 @@ public function skipFilters(callable $callback, ?array $filters = null): mixed /** * Get instance filters * - * @return array + * @return array */ public function getInstanceFilters(): array { diff --git a/tests/unit/CacheKeyTest.php b/tests/unit/CacheKeyTest.php new file mode 100644 index 000000000..542c1df5c --- /dev/null +++ b/tests/unit/CacheKeyTest.php @@ -0,0 +1,130 @@ +createMock(Adapter::class); + $adapter->method('getSupportForHostname')->willReturn(false); + $adapter->method('getTenant')->willReturn(null); + $adapter->method('getNamespace')->willReturn('test'); + + return new Database($adapter, new Cache(new None()), $instanceFilters); + } + + private function getHashKey(Database $db, string $collection = 'col', string $docId = 'doc1'): string + { + [, , $hashKey] = $db->getCacheKeys($collection, $docId); + return $hashKey; + } + + public function testSameConfigProducesSameCacheKey(): void + { + $db1 = $this->createDatabase(); + $db2 = $this->createDatabase(); + + $this->assertEquals($this->getHashKey($db1), $this->getHashKey($db2)); + } + + public function testDifferentSelectsProduceDifferentCacheKeys(): void + { + $db = $this->createDatabase(); + + [, , $hashA] = $db->getCacheKeys('col', 'doc1', ['name']); + [, , $hashB] = $db->getCacheKeys('col', 'doc1', ['email']); + + $this->assertNotEquals($hashA, $hashB); + } + + public function testSelectOrderDoesNotAffectCacheKey(): void + { + $db = $this->createDatabase(); + + [, , $hashA] = $db->getCacheKeys('col', 'doc1', ['name', 'email']); + [, , $hashB] = $db->getCacheKeys('col', 'doc1', ['email', 'name']); + + $this->assertEquals($hashA, $hashB); + } + + public function testInstanceFilterOverrideProducesDifferentCacheKey(): void + { + $noop = function (mixed $value) { + return $value; + }; + + $dbDefault = $this->createDatabase(); + $dbOverride = $this->createDatabase([ + 'json' => [ + 'encode' => $noop, + 'decode' => $noop, + ], + ]); + + $this->assertNotEquals( + $this->getHashKey($dbDefault), + $this->getHashKey($dbOverride) + ); + } + + public function testDifferentInstanceFilterCallablesProduceDifferentCacheKeys(): void + { + $noopA = function (mixed $value) { + return $value; + }; + $noopB = function (mixed $value) { + return $value; + }; + + $dbA = $this->createDatabase([ + 'myFilter' => [ + 'encode' => $noopA, + 'decode' => $noopA, + ], + ]); + $dbB = $this->createDatabase([ + 'myFilter' => [ + 'encode' => $noopB, + 'decode' => $noopB, + ], + ]); + + $this->assertNotEquals( + $this->getHashKey($dbA), + $this->getHashKey($dbB) + ); + } + + public function testDisabledFiltersProduceDifferentCacheKey(): void + { + $db = $this->createDatabase(); + + $hashEnabled = $this->getHashKey($db); + + $hashDisabled = $db->skipFilters(function () use ($db) { + return $this->getHashKey($db); + }, ['json']); + + $this->assertNotEquals($hashEnabled, $hashDisabled); + } + + public function testFiltersDisabledEntirelyProducesDifferentCacheKey(): void + { + $db = $this->createDatabase(); + + $hashEnabled = $this->getHashKey($db); + + $db->disableFilters(); + $hashDisabled = $this->getHashKey($db); + $db->enableFilters(); + + $this->assertNotEquals($hashEnabled, $hashDisabled); + } +} From 19ef2d452f440dc136057a2a65f31f6dbd0361e0 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 6 Mar 2026 21:33:31 +1300 Subject: [PATCH 2/2] fix: add PHPStan type annotation for $instanceFilters parameter Co-Authored-By: Claude Opus 4.6 --- tests/unit/CacheKeyTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/unit/CacheKeyTest.php b/tests/unit/CacheKeyTest.php index 542c1df5c..7e5e41d44 100644 --- a/tests/unit/CacheKeyTest.php +++ b/tests/unit/CacheKeyTest.php @@ -10,6 +10,9 @@ class CacheKeyTest extends TestCase { + /** + * @param array $instanceFilters + */ private function createDatabase(array $instanceFilters = []): Database { $adapter = $this->createMock(Adapter::class);