diff --git a/CHANGELOG.md b/CHANGELOG.md index 13c5aebc..50b3aeb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Added - Added `Connection::runDdlBatch` which runs DDLs in batch synchronously. (#86) +- Added emulator support for `Connection::listSessions`. (#88) Fixed - Fixed bug where running `Connection::statement` with DDLs was not logging and was not triggering events. (#86) diff --git a/src/Concerns/ManagesSessionPool.php b/src/Concerns/ManagesSessionPool.php index 120ab3f3..a3362bb1 100644 --- a/src/Concerns/ManagesSessionPool.php +++ b/src/Concerns/ManagesSessionPool.php @@ -20,6 +20,7 @@ use Colopl\Spanner\Session\DeleteOperation; use Colopl\Spanner\Session\SessionInfo; use Google\Auth\FetchAuthTokenInterface; +use Google\Cloud\Core\EmulatorTrait; use Google\Cloud\Spanner\Connection\ConnectionInterface; use Google\Cloud\Spanner\Connection\Grpc; use Google\Cloud\Spanner\Database; @@ -36,6 +37,8 @@ */ trait ManagesSessionPool { + use EmulatorTrait; + /** * @return void */ @@ -82,7 +85,14 @@ public function warmupSessionPool(): int public function listSessions(): Collection { $databaseName = $this->getSpannerDatabase()->name(); - $response = (new ProtobufSpannerClient())->listSessions($databaseName); + + $emulatorHost = getenv('SPANNER_EMULATOR_HOST'); + $config = $emulatorHost + ? $this->emulatorGapicConfig($emulatorHost) + : []; + + $response = (new ProtobufSpannerClient($config))->listSessions($databaseName); + return collect($response->iterateAllElements())->map(function ($session) { assert($session instanceof ProtobufSpannerSession); return new SessionInfo($session); diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index fbf1e02a..db315fcb 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -287,10 +287,6 @@ public function test_clearSessionPool(): void public function test_listSessions(): void { - if (getenv('SPANNER_EMULATOR_HOST')) { - $this->markTestSkipped('Cannot list sessions on emulator'); - } - $conn = $this->getDefaultConnection(); $conn->select('SELECT 1'); diff --git a/tests/Console/SessionsCommandTest.php b/tests/Console/SessionsCommandTest.php index 03c02881..e3dea730 100644 --- a/tests/Console/SessionsCommandTest.php +++ b/tests/Console/SessionsCommandTest.php @@ -65,10 +65,6 @@ public function test_no_args(): void public function test_with_args(): void { - if (getenv('SPANNER_EMULATOR_HOST')) { - $this->markTestSkipped('Cannot list sessions on emulator'); - } - foreach (['main', 'alternative'] as $name) { $conn = $this->getConnection($name); assert($conn instanceof Connection); @@ -89,10 +85,6 @@ public function test_with_args(): void public function test_no_sessions_shows_no_table(): void { - if (getenv('SPANNER_EMULATOR_HOST')) { - $this->markTestSkipped('Cannot list sessions on emulator'); - } - $this->artisan('spanner:sessions', ['connections' => 'main']) ->expectsOutputToContain('main contains 0 session(s).') ->doesntExpectOutputToContain('Name') @@ -102,10 +94,6 @@ public function test_no_sessions_shows_no_table(): void public function test_sort(): void { - if (getenv('SPANNER_EMULATOR_HOST')) { - $this->markTestSkipped('Cannot list sessions on emulator'); - } - $conn = $this->getDefaultConnection(); $this->setUpDatabaseOnce($conn); $this->createSessions($conn, 2); @@ -127,10 +115,6 @@ public function test_sort(): void public function test_sort_order(): void { - if (getenv('SPANNER_EMULATOR_HOST')) { - $this->markTestSkipped('Cannot list sessions on emulator'); - } - $conn = $this->getDefaultConnection(); $this->setUpDatabaseOnce($conn); $this->createSessions($conn, 2); @@ -152,10 +136,6 @@ public function test_sort_order(): void public function test_sort_patterns(): void { - if (getenv('SPANNER_EMULATOR_HOST')) { - $this->markTestSkipped('Cannot list sessions on emulator'); - } - $conn = $this->getDefaultConnection(); $this->setUpDatabaseOnce($conn); $this->createSessions($conn, 1);