Skip to content

Commit

Permalink
fix: prevent fatal error when using emulator without grpc (#7588)
Browse files Browse the repository at this point in the history
  • Loading branch information
kynx authored Aug 9, 2024
1 parent 80addd3 commit 8563ba4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/emulator/start-emulator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ CONTAINER=`docker run \
$IMAGE gcloud beta emulators $1 start --host-port=0.0.0.0:8085 --project=emulator-project`
sleep 10
docker logs $CONTAINER

14 changes: 9 additions & 5 deletions Core/src/EmulatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ private function emulatorGapicConfig($emulatorHost)
$emulatorHost = str_replace($search, '', $emulatorHost);
}

return [
$config = [
'apiEndpoint' => $emulatorHost,
'transportConfig' => [
'credentials' => new InsecureCredentialsWrapper(),
];
if (class_exists('Grpc\ChannelCredentials')) {
$config['transportConfig'] = [
'grpc' => [
'stubOpts' => [
'credentials' => \Grpc\ChannelCredentials::createInsecure()
]
]
],
'credentials' => new InsecureCredentialsWrapper(),
];
];
}

return $config;
}
/**
* Retrieve a valid base uri for a service emulator.
Expand Down
17 changes: 17 additions & 0 deletions Core/tests/Unit/EmulatorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ public function testEmulatorGapicConfig($hostname, $expected = null)
$this->assertNull($res['transportConfig']['grpc']['stubOpts']['credentials']);
}


/**
* @dataProvider hostnames
*/
public function testEmulatorGapicConfigWithNoGrpc($hostname, $expected = null)
{
if (\extension_loaded('grpc')) {
$this->markTestSkipped("Cannot run test with grpc extension loaded");
}

$expected = $expected ?: $hostname;

$res = $this->impl->call('emulatorGapicConfig', [$hostname]);
$this->assertEquals($expected, $res['apiEndpoint']);
$this->assertArrayNotHasKey('transportConfig', $res);
}

public function hostnames()
{
return [
Expand Down

0 comments on commit 8563ba4

Please sign in to comment.