Skip to content

Commit 8563ba4

Browse files
authored
fix: prevent fatal error when using emulator without grpc (#7588)
1 parent 80addd3 commit 8563ba4

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

.github/emulator/start-emulator.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ CONTAINER=`docker run \
1818
$IMAGE gcloud beta emulators $1 start --host-port=0.0.0.0:8085 --project=emulator-project`
1919
sleep 10
2020
docker logs $CONTAINER
21+

Core/src/EmulatorTrait.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ private function emulatorGapicConfig($emulatorHost)
3636
$emulatorHost = str_replace($search, '', $emulatorHost);
3737
}
3838

39-
return [
39+
$config = [
4040
'apiEndpoint' => $emulatorHost,
41-
'transportConfig' => [
41+
'credentials' => new InsecureCredentialsWrapper(),
42+
];
43+
if (class_exists('Grpc\ChannelCredentials')) {
44+
$config['transportConfig'] = [
4245
'grpc' => [
4346
'stubOpts' => [
4447
'credentials' => \Grpc\ChannelCredentials::createInsecure()
4548
]
4649
]
47-
],
48-
'credentials' => new InsecureCredentialsWrapper(),
49-
];
50+
];
51+
}
52+
53+
return $config;
5054
}
5155
/**
5256
* Retrieve a valid base uri for a service emulator.

Core/tests/Unit/EmulatorTraitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ public function testEmulatorGapicConfig($hostname, $expected = null)
5050
$this->assertNull($res['transportConfig']['grpc']['stubOpts']['credentials']);
5151
}
5252

53+
54+
/**
55+
* @dataProvider hostnames
56+
*/
57+
public function testEmulatorGapicConfigWithNoGrpc($hostname, $expected = null)
58+
{
59+
if (\extension_loaded('grpc')) {
60+
$this->markTestSkipped("Cannot run test with grpc extension loaded");
61+
}
62+
63+
$expected = $expected ?: $hostname;
64+
65+
$res = $this->impl->call('emulatorGapicConfig', [$hostname]);
66+
$this->assertEquals($expected, $res['apiEndpoint']);
67+
$this->assertArrayNotHasKey('transportConfig', $res);
68+
}
69+
5370
public function hostnames()
5471
{
5572
return [

0 commit comments

Comments
 (0)