Skip to content

Commit

Permalink
Add helper to reset the test Firebase project
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed May 28, 2024
1 parent 48fedd7 commit 1ef4b1e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
"rector-fix": [
"vendor/bin/rector"
],
"reset-project": [
"tests/bin/reset-project"
],
"test": [
"@analyze",
"@test-dependencies",
Expand Down
40 changes: 40 additions & 0 deletions tests/bin/reset-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

require __DIR__ . '/../../vendor/autoload.php';

use Kreait\Firebase\Auth\UserRecord;
use Kreait\Firebase\Database\RuleSet;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Util;

// Unsafe is needed because google/auth uses getenv/putenv to determine the Application Credentials
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__.'/..');
$dotenv->safeLoad();
$dotenv->required('GOOGLE_APPLICATION_CREDENTIALS');

$factory = (new Factory())->withDatabaseUri(Util::getenv('TEST_FIREBASE_RTDB_URI'));

$auth = $factory->createAuth();
$db = $factory->createDatabase();

echo 'Resetting database rules... ';
$factory->createDatabase()->updateRules(RuleSet::private());
echo 'DONE'.PHP_EOL;

echo 'Purging database... ';
$factory->createDatabase()->getReference('/')->remove();
echo 'DONE'.PHP_EOL;

echo 'Purging auth users... ';
do {
$users = iterator_to_array($auth->listUsers());
$uids = array_map(fn(UserRecord $record) => $record->uid, $users);

if (count($uids) > 0) {
$auth->deleteUsers($uids, true);
}
} while (count($uids) > 0);
echo 'DONE'.PHP_EOL;

0 comments on commit 1ef4b1e

Please sign in to comment.