Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/firestore/override database #209

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
'tenant_id' => env('FIREBASE_AUTH_TENANT_ID'),
],

/*
* ------------------------------------------------------------------------
* Firestore Component
* ------------------------------------------------------------------------
*/

'firestore' => [
'database' => null,
],

/*
* ------------------------------------------------------------------------
* Firebase Realtime Database
Expand Down
6 changes: 5 additions & 1 deletion src/FirebaseProjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(Container $app)
$this->app = $app;
}

public function project(string $name = null): FirebaseProject
public function project(?string $name = null): FirebaseProject
{
$name = $name ?? $this->getDefaultProject();

Expand Down Expand Up @@ -64,6 +64,10 @@ protected function configure(string $name): FirebaseProject

$config = $this->configuration($name);

$factory = $factory->withFirestoreDatabase(
$config['firestore']['database'] ?? '(default)'
);

if ($tenantId = $config['auth']['tenant_id'] ?? null) {
$factory = $factory->withTenantId($tenantId);
}
Expand Down
30 changes: 29 additions & 1 deletion tests/FirebaseProjectManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,34 @@ public function it_uses_the_laravel_cache_as_verifier_cache(): void
$this->assertInstanceOf(CacheItemPoolInterface::class, $property->getValue($factory));
}

/**
* @test
*/
public function it_use_the_default_firestore_database(): void
{
config(['firebase.projects.app.firestore.database' => null]);
$projectName = $this->app->config->get('firebase.default');
$factory = $this->factoryForProject($projectName);

$property = $this->getAccessibleProperty($factory, 'firestoreClientConfig');

$this->assertEquals('(default)', $property->getValue($factory)['database']);
}

/**
* @test
*/
public function it_overrides_the_default_firestore_database(): void
{
config(['firebase.projects.app.firestore.database' => 'override-database']);
$projectName = $this->app->config->get('firebase.default');
$factory = $this->factoryForProject($projectName);

$property = $this->getAccessibleProperty($factory, 'firestoreClientConfig');

$this->assertEquals('override-database', $property->getValue($factory)['database']);
}

/**
* @test
*/
Expand All @@ -289,7 +317,7 @@ public function it_uses_the_laravel_cache_as_auth_token_cache(): void
$this->assertInstanceOf(CacheItemPoolInterface::class, $property->getValue($factory));
}

private function factoryForProject(string $project = null): Factory
private function factoryForProject(?string $project = null): Factory
{
$project = $this->app->make(FirebaseProjectManager::class)->project($project);

Expand Down