From 1344f96a8390d83c3796671bebbab0c4656bf1eb Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 9 Aug 2026 00:29:38 +1200 Subject: [PATCH] test(databases): keep the reachability flag alive across the refusal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The flag lived on the call's return value, but the call under test throws on the path the assertion cares about, so it never came back and the catch decided the outcome on its own — the assertion passed whether or not the backend had been queried. Moving it onto the instance keeps what the action did before it threw. Also takes Rector's createStub for the response, which is never asserted on. Co-Authored-By: Claude Fable 5 --- .../LegacyCollectionsRefusalTest.php | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/tests/unit/Platform/Modules/Databases/LegacyCollectionsRefusalTest.php b/tests/unit/Platform/Modules/Databases/LegacyCollectionsRefusalTest.php index 42aee62bbf..4fd21e5c8a 100644 --- a/tests/unit/Platform/Modules/Databases/LegacyCollectionsRefusalTest.php +++ b/tests/unit/Platform/Modules/Databases/LegacyCollectionsRefusalTest.php @@ -23,6 +23,15 @@ use Utopia\Database\Validator\Authorization; */ final class LegacyCollectionsRefusalTest extends TestCase { + /** + * Whether the action queried `database_{sequence}`. + * + * An instance property rather than a return value, because the call under test + * throws on the path this suite cares about — a returned flag would be lost with + * the stack, leaving the assertion to pass on the catch alone. + */ + private bool $reached = false; + protected function setUp(): void { parent::setUp(); @@ -32,11 +41,10 @@ final class LegacyCollectionsRefusalTest extends TestCase /** * @param array $attributes - * @return array{reached: bool} */ - private function listCollections(array $attributes): array + private function listCollections(array $attributes): void { - $reached = false; + $this->reached = false; $dbForProject = $this->createMock(Database::class); $dbForProject @@ -44,15 +52,15 @@ final class LegacyCollectionsRefusalTest extends TestCase ->willReturn(new Document(['$id' => 'db', '$sequence' => '7', ...$attributes])); $dbForProject ->method('find') - ->willReturnCallback(function () use (&$reached): array { - $reached = true; + ->willReturnCallback(function (): array { + $this->reached = true; return []; }); $dbForProject ->method('count') - ->willReturnCallback(function () use (&$reached): int { - $reached = true; + ->willReturnCallback(function (): int { + $this->reached = true; return 0; }); @@ -62,12 +70,10 @@ final class LegacyCollectionsRefusalTest extends TestCase [], '', false, - $this->createMock(UtopiaResponse::class), + $this->createStub(UtopiaResponse::class), $dbForProject, new Authorization(), ); - - return ['reached' => $reached]; } public function testAVectorsDatabaseIsRefusedBeforeTheBackendIsQueried(): void @@ -92,20 +98,18 @@ final class LegacyCollectionsRefusalTest extends TestCase /** * The mismatched call must not reach `database_{sequence}` at all — that query is - * where the 500 came from. + * where the 500 came from. Refusing and querying anyway would still 500. */ public function testTheRefusedCallNeverReachesTheBackend(): void { foreach ([DATABASE_TYPE_VECTORSDB, DATABASE_TYPE_DOCUMENTSDB] as $type) { - $reached = true; - try { - $reached = $this->listCollections(['type' => $type])['reached']; + $this->listCollections(['type' => $type]); } catch (Exception) { - $reached = false; + // The refusal is expected here; what it did on the way is the assertion. } - $this->assertFalse($reached, "a '{$type}' database must be refused before database_{sequence} is queried"); + $this->assertFalse($this->reached, "a '{$type}' database must be refused before database_{sequence} is queried"); } } @@ -116,8 +120,10 @@ final class LegacyCollectionsRefusalTest extends TestCase public function testTheServedTypesStillReachTheBackend(): void { foreach ([DATABASE_TYPE_LEGACY, DATABASE_TYPE_TABLESDB] as $type) { + $this->listCollections(['type' => $type]); + $this->assertTrue( - $this->listCollections(['type' => $type])['reached'], + $this->reached, "a '{$type}' database must still be served by /v1/databases/{id}/collections", ); }