From 6683454e03a419f96e601b12282a6bf5d88b3ff8 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 15 May 2023 17:21:16 -0700 Subject: [PATCH] feat: support new CloudSqlProxy --- src/TestUtils/CloudSqlProxyTrait.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/TestUtils/CloudSqlProxyTrait.php b/src/TestUtils/CloudSqlProxyTrait.php index f98014e..ce3da4b 100644 --- a/src/TestUtils/CloudSqlProxyTrait.php +++ b/src/TestUtils/CloudSqlProxyTrait.php @@ -32,21 +32,35 @@ trait CloudSqlProxyTrait public static function startCloudSqlProxy($connectionName, $socketDir, $port = null) { + if (self::$cloudSqlProxyProcess) { + if (self::$cloudSqlProxyProcess->isRunning()) { + return self::$cloudSqlProxyProcess; + } + } + // create the directory to store the unix socket for cloud_sql_proxy if (!is_dir($socketDir) && !@mkdir($socketDir, 0755, true)) { throw new Exception('Unable to create socket dir ' . $socketDir); } - $instances = sprintf('-instances=%s', $connectionName); + $cmd = [ + 'cloud-sql-proxy', + '--unix-socket', + $socketDir, + ]; + if ($port) { - $instances = sprintf('%s=tcp:%s,%s', $instances, $port, $connectionName); + $cmd[] = '--port'; + $cmd[] = $port; } - $process = new Process(['cloud_sql_proxy', $instances, '-dir', $socketDir]); + $cmd[] = $connectionName; + + $process = new Process($cmd); $process->start(); $process->waitUntil(function ($type, $buffer) { print($buffer); - return str_contains($buffer, 'Ready for new connections'); + return str_contains($buffer, 'ready for new connections'); }); if (!$process->isRunning()) { if ($output = $process->getOutput()) {