|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Utopia\Cache\Adapter\None; |
| 7 | +use Utopia\Cache\Cache; |
| 8 | +use Utopia\Database\Adapter; |
| 9 | +use Utopia\Database\Database; |
| 10 | + |
| 11 | +class CacheKeyTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @param array<string, array{encode: callable, decode: callable}> $instanceFilters |
| 15 | + */ |
| 16 | + private function createDatabase(array $instanceFilters = []): Database |
| 17 | + { |
| 18 | + $adapter = $this->createMock(Adapter::class); |
| 19 | + $adapter->method('getSupportForHostname')->willReturn(false); |
| 20 | + $adapter->method('getTenant')->willReturn(null); |
| 21 | + $adapter->method('getNamespace')->willReturn('test'); |
| 22 | + |
| 23 | + return new Database($adapter, new Cache(new None()), $instanceFilters); |
| 24 | + } |
| 25 | + |
| 26 | + private function getHashKey(Database $db, string $collection = 'col', string $docId = 'doc1'): string |
| 27 | + { |
| 28 | + [, , $hashKey] = $db->getCacheKeys($collection, $docId); |
| 29 | + return $hashKey; |
| 30 | + } |
| 31 | + |
| 32 | + public function testSameConfigProducesSameCacheKey(): void |
| 33 | + { |
| 34 | + $db1 = $this->createDatabase(); |
| 35 | + $db2 = $this->createDatabase(); |
| 36 | + |
| 37 | + $this->assertEquals($this->getHashKey($db1), $this->getHashKey($db2)); |
| 38 | + } |
| 39 | + |
| 40 | + public function testDifferentSelectsProduceDifferentCacheKeys(): void |
| 41 | + { |
| 42 | + $db = $this->createDatabase(); |
| 43 | + |
| 44 | + [, , $hashA] = $db->getCacheKeys('col', 'doc1', ['name']); |
| 45 | + [, , $hashB] = $db->getCacheKeys('col', 'doc1', ['email']); |
| 46 | + |
| 47 | + $this->assertNotEquals($hashA, $hashB); |
| 48 | + } |
| 49 | + |
| 50 | + public function testSelectOrderDoesNotAffectCacheKey(): void |
| 51 | + { |
| 52 | + $db = $this->createDatabase(); |
| 53 | + |
| 54 | + [, , $hashA] = $db->getCacheKeys('col', 'doc1', ['name', 'email']); |
| 55 | + [, , $hashB] = $db->getCacheKeys('col', 'doc1', ['email', 'name']); |
| 56 | + |
| 57 | + $this->assertEquals($hashA, $hashB); |
| 58 | + } |
| 59 | + |
| 60 | + public function testInstanceFilterOverrideProducesDifferentCacheKey(): void |
| 61 | + { |
| 62 | + $noop = function (mixed $value) { |
| 63 | + return $value; |
| 64 | + }; |
| 65 | + |
| 66 | + $dbDefault = $this->createDatabase(); |
| 67 | + $dbOverride = $this->createDatabase([ |
| 68 | + 'json' => [ |
| 69 | + 'encode' => $noop, |
| 70 | + 'decode' => $noop, |
| 71 | + ], |
| 72 | + ]); |
| 73 | + |
| 74 | + $this->assertNotEquals( |
| 75 | + $this->getHashKey($dbDefault), |
| 76 | + $this->getHashKey($dbOverride) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function testDifferentInstanceFilterCallablesProduceDifferentCacheKeys(): void |
| 81 | + { |
| 82 | + $noopA = function (mixed $value) { |
| 83 | + return $value; |
| 84 | + }; |
| 85 | + $noopB = function (mixed $value) { |
| 86 | + return $value; |
| 87 | + }; |
| 88 | + |
| 89 | + $dbA = $this->createDatabase([ |
| 90 | + 'myFilter' => [ |
| 91 | + 'encode' => $noopA, |
| 92 | + 'decode' => $noopA, |
| 93 | + ], |
| 94 | + ]); |
| 95 | + $dbB = $this->createDatabase([ |
| 96 | + 'myFilter' => [ |
| 97 | + 'encode' => $noopB, |
| 98 | + 'decode' => $noopB, |
| 99 | + ], |
| 100 | + ]); |
| 101 | + |
| 102 | + $this->assertNotEquals( |
| 103 | + $this->getHashKey($dbA), |
| 104 | + $this->getHashKey($dbB) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public function testDisabledFiltersProduceDifferentCacheKey(): void |
| 109 | + { |
| 110 | + $db = $this->createDatabase(); |
| 111 | + |
| 112 | + $hashEnabled = $this->getHashKey($db); |
| 113 | + |
| 114 | + $hashDisabled = $db->skipFilters(function () use ($db) { |
| 115 | + return $this->getHashKey($db); |
| 116 | + }, ['json']); |
| 117 | + |
| 118 | + $this->assertNotEquals($hashEnabled, $hashDisabled); |
| 119 | + } |
| 120 | + |
| 121 | + public function testFiltersDisabledEntirelyProducesDifferentCacheKey(): void |
| 122 | + { |
| 123 | + $db = $this->createDatabase(); |
| 124 | + |
| 125 | + $hashEnabled = $this->getHashKey($db); |
| 126 | + |
| 127 | + $db->disableFilters(); |
| 128 | + $hashDisabled = $this->getHashKey($db); |
| 129 | + $db->enableFilters(); |
| 130 | + |
| 131 | + $this->assertNotEquals($hashEnabled, $hashDisabled); |
| 132 | + } |
| 133 | +} |
0 commit comments