@@ -130,4 +130,60 @@ public function testFiltersDisabledEntirelyProducesDifferentCacheKey(): void
130130
131131 $ this ->assertNotEquals ($ hashEnabled , $ hashDisabled );
132132 }
133+
134+ public function testParseHostname (): void
135+ {
136+ $ hostname = 'appwrite://database_db_nyc3_self_hosted_0_0?database=appwrite&namespace=_1 ' ;
137+
138+ $ adapter = $ this ->createMock (Adapter::class);
139+ $ adapter ->method ('getSupportForHostname ' )->willReturn (true );
140+ $ adapter ->method ('getHostname ' )->willReturn ($ hostname );
141+ $ adapter ->method ('getTenant ' )->willReturn (999 );
142+ $ adapter ->method ('getSharedTables ' )->willReturn (true );
143+ $ adapter ->method ('getNamespace ' )->willReturn ('_ns ' );
144+
145+ $ db = new Database ($ adapter , new Cache (new None ()), []);
146+
147+ /**
148+ * Check DSN is parsed correctly
149+ */
150+ [$ collectionKey , $ documentKey ] = $ db ->getCacheKeys ('users ' );
151+ $ this ->assertEquals ('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:users ' , $ collectionKey );
152+ $ this ->assertEquals ('' , $ documentKey );
153+
154+ $ db ->setGlobalCollections (['users ' ]);
155+ $ this ->assertEquals (['users ' ], $ db ->getGlobalCollections ());
156+
157+ /**
158+ * Check that tenant 999 exists
159+ */
160+
161+ [$ collectionKey , $ documentKey ] = $ db ->getCacheKeys (Database::METADATA , 'audit ' );
162+ $ this ->assertEquals ('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:_metadata ' , $ collectionKey );
163+ $ this ->assertEquals ('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:_metadata:audit ' , $ documentKey );
164+
165+ /**
166+ * Check that tenant 999 was removed
167+ */
168+ [$ collectionKey , $ documentKey ] = $ db ->getCacheKeys (Database::METADATA , 'users ' );
169+ $ this ->assertEquals ('default-cache-database_db_nyc3_self_hosted_0_0:_ns::collection:_metadata ' , $ collectionKey );
170+ $ this ->assertEquals ('default-cache-database_db_nyc3_self_hosted_0_0:_ns::collection:_metadata:users ' , $ documentKey );
171+ }
172+
173+ public function testSimpleHostname (): void
174+ {
175+ $ hostname = 'database_db_nyc3_self_hosted_0_0 ' ;
176+
177+ $ adapter = $ this ->createMock (Adapter::class);
178+ $ adapter ->method ('getSupportForHostname ' )->willReturn (true );
179+ $ adapter ->method ('getHostname ' )->willReturn ($ hostname );
180+ $ adapter ->method ('getTenant ' )->willReturn (999 );
181+ $ adapter ->method ('getSharedTables ' )->willReturn (true );
182+ $ adapter ->method ('getNamespace ' )->willReturn ('_ns ' );
183+
184+ $ db = new Database ($ adapter , new Cache (new None ()), []);
185+
186+ [$ collectionKey ] = $ db ->getCacheKeys ('animals ' );
187+ $ this ->assertEquals ('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:animals ' , $ collectionKey );
188+ }
133189}
0 commit comments