File tree Expand file tree Collapse file tree 3 files changed +22
-6
lines changed
Expand file tree Collapse file tree 3 files changed +22
-6
lines changed Original file line number Diff line number Diff line change 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]
Original file line number Diff line number Diff line change 2323
2424namespace 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]
2830use 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
4751require_once __DIR__ . '/../../testing/sample_helpers.php ' ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments