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..7e5e41d44 --- /dev/null +++ b/tests/unit/CacheKeyTest.php @@ -0,0 +1,133 @@ + $instanceFilters + */ + private function createDatabase(array $instanceFilters = []): Database + { + $adapter = $this->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); + } +}