Skip to content

Commit 9f19055

Browse files
authored
docs: consolidated client creation snippets (GoogleCloudPlatform#1447)
1 parent 5b7cfee commit 9f19055

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

firestore/src/setup_client_create.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@
3030
/**
3131
* Initialize Cloud Firestore with default project ID.
3232
*/
33-
function setup_client_create()
33+
function setup_client_create(string $projectId = null)
3434
{
3535
// Create the Cloud Firestore client
36-
$db = new FirestoreClient();
37-
printf('Created Cloud Firestore client with default project ID.' . PHP_EOL);
36+
if (empty($projectId)) {
37+
// The `projectId` parameter is optional and represents which project the
38+
// client will act on behalf of. If not supplied, the client falls back to
39+
// the default project inferred from the environment.
40+
$db = new FirestoreClient();
41+
printf('Created Cloud Firestore client with default project ID.' . PHP_EOL);
42+
} else {
43+
$db = new FirestoreClient([
44+
'projectId' => $projectId,
45+
]);
46+
printf('Created Cloud Firestore client with project ID: %s' . PHP_EOL, $projectId);
47+
}
3848
}
3949
# [END firestore_setup_client_create]
4050
# [END fs_initialize]

firestore/src/setup_client_create_with_project_id.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
namespace Google\Cloud\Samples\Firestore;
2525

26+
# TODO(craiglabenz): Remove the `firestore_setup_client_create_with_project_id`
27+
# region tag after consolidating to `firestore_setup_client_create`
2628
# [START fs_initialize_project_id]
2729
# [START firestore_setup_client_create_with_project_id]
2830
use Google\Cloud\Firestore\FirestoreClient;
@@ -42,6 +44,8 @@ function setup_client_create_with_project_id(string $projectId): void
4244
}
4345
# [END firestore_setup_client_create_with_project_id]
4446
# [END fs_initialize_project_id]
47+
# TODO(craiglabenz): Remove the `firestore_setup_client_create_with_project_id`
48+
# region tag after consolidating to `firestore_setup_client_create`
4549

4650
// The following 2 lines are only needed to run the samples
4751
require_once __DIR__ . '/../../testing/sample_helpers.php';

firestore/test/firestoreTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ public static function tearDownAfterClass(): void
7474

7575
public function testInitialize()
7676
{
77-
$output = $this->runFirestoreSnippet('setup_client_create', []);
78-
$this->assertStringContainsString('Created Cloud Firestore client with default project ID.', $output);
77+
$output = self::runFunctionSnippet('setup_client_create', [self::$projectId]);
78+
$this->assertStringContainsString('Created Cloud Firestore client with project ID: ', $output);
7979
}
8080

8181
public function testInitializeProjectId()
8282
{
83-
$output = $this->runFirestoreSnippet('setup_client_create_with_project_id');
83+
# The lack of a second parameter implies that a non-empty projectId is
84+
# supplied to the snippet's function.
85+
$output = $this->runFirestoreSnippet('setup_client_create', [self::$projectId]);
8486
$this->assertStringContainsString('Created Cloud Firestore client with project ID:', $output);
8587
}
8688

0 commit comments

Comments
 (0)