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 30
30
/**
31
31
* Initialize Cloud Firestore with default project ID.
32
32
*/
33
- function setup_client_create ()
33
+ function setup_client_create (string $ projectId = null )
34
34
{
35
35
// 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
+ }
38
48
}
39
49
# [END firestore_setup_client_create]
40
50
# [END fs_initialize]
Original file line number Diff line number Diff line change 23
23
24
24
namespace Google \Cloud \Samples \Firestore ;
25
25
26
+ # TODO(craiglabenz): Remove the `firestore_setup_client_create_with_project_id`
27
+ # region tag after consolidating to `firestore_setup_client_create`
26
28
# [START fs_initialize_project_id]
27
29
# [START firestore_setup_client_create_with_project_id]
28
30
use Google \Cloud \Firestore \FirestoreClient ;
@@ -42,6 +44,8 @@ function setup_client_create_with_project_id(string $projectId): void
42
44
}
43
45
# [END firestore_setup_client_create_with_project_id]
44
46
# [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`
45
49
46
50
// The following 2 lines are only needed to run the samples
47
51
require_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
74
74
75
75
public function testInitialize ()
76
76
{
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 );
79
79
}
80
80
81
81
public function testInitializeProjectId ()
82
82
{
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 ]);
84
86
$ this ->assertStringContainsString ('Created Cloud Firestore client with project ID: ' , $ output );
85
87
}
86
88
You can’t perform that action at this time.
0 commit comments