1919
2020use Google \Cloud \Bigtable \BigtableClient ;
2121use Google \Cloud \Bigtable \Table ;
22+ use Google \Cloud \Bigtable \V2 \Client \BigtableClient as BigtableGapicClient ;
23+ use Google \Cloud \Bigtable \V2 \PingAndWarmRequest ;
24+ use Google \Cloud \Bigtable \V2 \PingAndWarmResponse ;
2225use Google \Cloud \Core \InsecureCredentialsWrapper ;
2326use Google \Cloud \Core \Testing \TestHelpers ;
2427use PHPUnit \Framework \TestCase ;
28+ use Prophecy \Argument ;
29+ use Prophecy \PhpUnit \ProphecyTrait ;
2530
2631/**
2732 * @group bigtable
2833 * @group bigtabledata
2934 */
3035class BigtableClientTest extends TestCase
3136{
37+ use ProphecyTrait;
38+
3239 private $ client ;
3340
3441 public function setUp (): void
@@ -44,4 +51,61 @@ public function testTable()
4451
4552 $ this ->assertInstanceOf (Table::class, $ table );
4653 }
54+
55+ public function testPingAndWarm ()
56+ {
57+ $ gapicClient = $ this ->prophesize (BigtableGapicClient::class);
58+ $ gapicClient ->pingAndWarm (Argument::any ())
59+ ->shouldNotBeCalled ();
60+
61+ $ bigtable = new BigtableClient ([
62+ 'projectId ' => 'my-project ' ,
63+ 'credentials ' => new InsecureCredentialsWrapper (),
64+ ]);
65+ (fn () => $ this ->gapicClient = $ gapicClient ->reveal ())->call ($ bigtable );
66+
67+ // calling "::table" without the option does not pingandwarm
68+ $ bigtable ->table ('my-instance ' , 'my-table ' );
69+
70+ // creating the client with the option does not pingandwarm
71+ $ bigtable = new BigtableClient ([
72+ 'projectId ' => 'my-project ' ,
73+ 'credentials ' => new InsecureCredentialsWrapper (),
74+ 'pingAndWarm ' => true , // this would throw an auth error if it was called
75+ ]);
76+
77+ $ pingAndWarmResponse = new PingAndWarmResponse ();
78+ $ gapicClient = $ this ->prophesize (BigtableGapicClient::class);
79+ $ gapicClient ->pingAndWarm (
80+ Argument::that (function (PingAndWarmRequest $ request ) {
81+ return $ request ->getName () === 'projects/my-project/instances/my-instance ' ;
82+ }),
83+ []
84+ )
85+ ->shouldBeCalledOnce ()
86+ ->willReturn ($ pingAndWarmResponse );
87+ $ gapicClient ->pingAndWarm (
88+ Argument::that (function (PingAndWarmRequest $ request ) {
89+ return $ request ->getName () === 'projects/my-project/instances/my-instance-2 ' ;
90+ }),
91+ []
92+ )
93+ ->shouldBeCalledOnce ()
94+ ->willReturn ($ pingAndWarmResponse );
95+
96+ // calling "::table" with the option should call pingandwarm
97+ $ bigtable = new BigtableClient ([
98+ 'projectId ' => 'my-project ' ,
99+ 'credentials ' => new InsecureCredentialsWrapper (),
100+ 'gapicClient ' => $ gapicClient ->reveal (),
101+ 'pingAndWarm ' => true ,
102+ ]);
103+ (fn () => $ this ->gapicClient = $ gapicClient ->reveal ())->call ($ bigtable );
104+ $ bigtable ->table ('my-instance ' , 'my-table ' );
105+
106+ // "PingAndWarm" will only be called once per instance
107+ $ bigtable ->table ('my-instance ' , 'my-table ' ); // not called
108+ $ bigtable ->table ('my-instance ' , 'my-table-2 ' ); // still not called
109+ $ bigtable ->table ('my-instance-2 ' , 'my-table ' ); // called
110+ }
47111}
0 commit comments