Skip to content

Commit 8d25b48

Browse files
authored
fix(tests): phpunit xml for cloud sql tests (GoogleCloudPlatform#1489)
1 parent ebc9c26 commit 8d25b48

File tree

12 files changed

+28
-62
lines changed

12 files changed

+28
-62
lines changed

.kokoro/secrets.sh.enc

4 Bytes
Binary file not shown.

appengine/standard/getting-started/test/CloudSqlTest.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,28 @@
1717
namespace Google\Cloud\Test\GettingStarted;
1818

1919
use Google\Cloud\TestUtils\TestTrait;
20+
use Google\Cloud\TestUtils\CloudSqlProxyTrait;
2021
use Google\Cloud\Samples\AppEngine\GettingStarted\CloudSqlDataModel;
2122
use PHPUnit\Framework\TestCase;
2223
use PDO;
23-
use Symfony\Component\Process\Process;
2424

2525
class CloudSqlTest extends TestCase
2626
{
2727
use TestTrait;
28-
private static $process;
28+
use CloudSqlProxyTrait;
2929

3030
public function setUp(): void
3131
{
3232
$connection = $this->requireEnv('CLOUDSQL_CONNECTION_NAME');
33+
$socketDir = $this->requireEnv('DB_SOCKET_DIR');
34+
35+
$this->startCloudSqlProxy($connection, $socketDir);
36+
3337
$dbUser = $this->requireEnv('CLOUDSQL_USER');
3438
$dbPass = $this->requireEnv('CLOUDSQL_PASSWORD');
3539
$dbName = getenv('CLOUDSQL_DATABASE_NAME') ?: 'bookshelf';
36-
$socketDir = $this->requireEnv('DB_SOCKET_DIR');
3740
$socket = "${socketDir}/${connection}";
3841

39-
// create the directory to store the unix socket for cloud_sql_proxy
40-
if (!is_dir($socketDir)) {
41-
mkdir($socketDir, 0755, true);
42-
}
43-
44-
self::$process = new Process(['cloud_sql_proxy', '-instances=' . $connection, '-dir', $socketDir]);
45-
self::$process->start();
46-
self::$process->waitUntil(function ($type, $buffer) {
47-
return str_contains($buffer, 'Ready for new connections');
48-
});
49-
5042
if (!file_exists($socket)) {
5143
$this->markTestSkipped(
5244
"You must run 'cloud_sql_proxy -instances=${connection} -dir=${socketDir}'"
@@ -60,11 +52,6 @@ public function setUp(): void
6052
$this->model = new CloudSqlDataModel($pdo);
6153
}
6254

63-
public static function tearDownAfterClass(): void
64-
{
65-
self::$process->stop();
66-
}
67-
6855
public function testDataModel()
6956
{
7057
$model = $this->model;

cloud_sql/mysql/pdo/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit colors="true" bootstrap="../../../testing/bootstrap.php">
33
<testsuites>
44
<testsuite name="CloudSQLMySQLSample">
5-
<directory>testsdirectory>
5+
<directory>testdirectory>
66
testsuite>
77
testsuites>
88
<filter>

cloud_sql/mysql/pdo/src/DBInitializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static function initUnixDatabaseConnection(
127127
'The PHP error was %s',
128128
$e->getMessage()
129129
),
130-
$e->getCode(),
130+
(int) $e->getCode(),
131131
$e
132132
);
133133
} catch (PDOException $e) {
@@ -140,7 +140,7 @@ public static function initUnixDatabaseConnection(
140140
'https://cloud.google.com/sql/docs/mysql/connect-external-app',
141141
$e->getMessage()
142142
),
143-
$e->getCode(),
143+
(int) $e->getCode(),
144144
$e
145145
);
146146
}

cloud_sql/mysql/pdo/test/IntegrationTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,23 @@
2121
use Google\Cloud\Samples\CloudSQL\MySQL\DBInitializer;
2222
use Google\Cloud\Samples\CloudSQL\MySQL\Votes;
2323
use Google\Cloud\TestUtils\TestTrait;
24+
use Google\Cloud\TestUtils\CloudSqlProxyTrait;
2425
use PDO;
2526
use PHPUnit\Framework\TestCase;
26-
use Symfony\Component\Process\Process;
2727

2828
class IntegrationTest extends TestCase
2929
{
3030
use TestTrait;
31-
private static $process;
31+
use CloudSqlProxyTrait;
3232

3333
public static function setUpBeforeClass(): void
3434
{
3535
$connectionName = self::requireEnv('CLOUDSQL_CONNECTION_NAME_MYSQL');
3636
$socketDir = self::requireEnv('DB_SOCKET_DIR');
37-
self::$process = new Process(['cloud_sql_proxy', '-instances=' . $connectionName . '=tcp:3306,' . $connectionName, '-dir', $socketDir]);
38-
self::$process->start();
39-
self::$process->waitUntil(function ($type, $buffer) {
40-
return str_contains($buffer, 'Ready for new connections');
41-
});
42-
}
37+
// '3306' was not working on kokoro we probably just need to update cloud_sql_proxy
38+
$port = null;
4339

44-
public static function tearDownAfterClass(): void
45-
{
46-
self::$process->stop();
40+
self::startCloudSqlProxy($connectionName, $socketDir, $port);
4741
}
4842

4943
public function testUnixConnection()

cloud_sql/postgres/pdo/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit colors="true" bootstrap="../../../testing/bootstrap.php">
33
<testsuites>
44
<testsuite name="CloudSQLPostgresSample">
5-
<directory>testsdirectory>
5+
<directory>testdirectory>
66
testsuite>
77
testsuites>
88
<filter>

cloud_sql/postgres/pdo/src/DBInitializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static function initUnixDatabaseConnection(
127127
'The PHP error was %s',
128128
$e->getMessage()
129129
),
130-
$e->getCode(),
130+
(int) $e->getCode(),
131131
$e
132132
);
133133
} catch (PDOException $e) {
@@ -140,7 +140,7 @@ public static function initUnixDatabaseConnection(
140140
'https://cloud.google.com/sql/docs/postgres/connect-external-app',
141141
$e->getMessage()
142142
),
143-
$e->getCode(),
143+
(int) $e->getCode(),
144144
$e
145145
);
146146
}

cloud_sql/postgres/pdo/src/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
);
7070
} else {
7171
$connectionName = getenv('CLOUDSQL_CONNECTION_NAME');
72-
$socketDir = getenv('DB_SOCKET_DIR') ?: '/cloudsql';
72+
$socketDir = getenv('DB_SOCKET_DIR') ?: '/tmp/cloudsql';
7373
return DBInitializer::initUnixDatabaseConnection(
7474
$username,
7575
$password,

cloud_sql/postgres/pdo/test/IntegrationTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,22 @@
2121
use Google\Cloud\Samples\CloudSQL\Postgres\DBInitializer;
2222
use Google\Cloud\Samples\CloudSQL\Postgres\Votes;
2323
use Google\Cloud\TestUtils\TestTrait;
24+
use Google\Cloud\TestUtils\CloudSqlProxyTrait;
2425
use PDO;
2526
use PHPUnit\Framework\TestCase;
26-
use Symfony\Component\Process\Process;
2727

2828
class IntegrationTest extends TestCase
2929
{
3030
use TestTrait;
31-
private static $process;
31+
use CloudSqlProxyTrait;
3232

3333
public static function setUpBeforeClass(): void
3434
{
3535
$connectionName = self::requireEnv('CLOUDSQL_CONNECTION_NAME_POSTGRES');
3636
$socketDir = self::requireEnv('DB_SOCKET_DIR');
37-
self::$process = new Process(['cloud_sql_proxy', '-instances=' . $connectionName . '=tcp:5432,' . $connectionName, '-dir', $socketDir]);
38-
self::$process->start();
39-
self::$process->waitUntil(function ($type, $buffer) {
40-
return str_contains($buffer, 'Ready for new connections');
41-
});
42-
}
37+
$port = '5432';
4338

44-
public static function tearDownAfterClass(): void
45-
{
46-
self::$process->stop();
39+
self::startCloudSqlProxy($connectionName, $socketDir, $port);
4740
}
4841

4942
public function testUnixConnection()

cloud_sql/sqlserver/pdo/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit colors="true" bootstrap="../../../testing/bootstrap.php">
33
<testsuites>
44
<testsuite name="CloudSQLSQLServerSample">
5-
<directory>testsdirectory>
5+
<directory>testdirectory>
66
testsuite>
77
testsuites>
88
<filter>

cloud_sql/sqlserver/pdo/src/DBInitializer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
class DBInitializer
2727
{
28-
2928
/**
3029
* @param $username string username of the database user
3130
* @param $password string password of the database user
@@ -75,7 +74,7 @@ public static function initTcpDatabaseConnection(
7574
'https://cloud.google.com/sql/docs/sqlserver/connect-external-app',
7675
$e->getMessage()
7776
),
78-
$e->getCode(),
77+
(int) $e->getCode(),
7978
$e
8079
);
8180
}

cloud_sql/sqlserver/pdo/test/IntegrationTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,22 @@
2121
use Google\Cloud\Samples\CloudSQL\SQLServer\DBInitializer;
2222
use Google\Cloud\Samples\CloudSQL\SQLServer\Votes;
2323
use Google\Cloud\TestUtils\TestTrait;
24+
use Google\Cloud\TestUtils\CloudSqlProxyTrait;
2425
use PDO;
2526
use PHPUnit\Framework\TestCase;
26-
use Symfony\Component\Process\Process;
2727

2828
class IntegrationTest extends TestCase
2929
{
3030
use TestTrait;
31-
private static $process;
31+
use CloudSqlProxyTrait;
3232

3333
public static function setUpBeforeClass(): void
3434
{
3535
$connectionName = self::requireEnv('CLOUDSQL_CONNECTION_NAME_SQLSERVER');
3636
$socketDir = self::requireEnv('DB_SOCKET_DIR');
37-
self::$process = new Process(['cloud_sql_proxy', '-instances=' . $connectionName . '=tcp:1433,' . $connectionName, '-dir', $socketDir]);
38-
self::$process->start();
39-
self::$process->waitUntil(function ($type, $buffer) {
40-
return str_contains($buffer, 'Ready for new connections');
41-
});
42-
}
37+
$port = '1433';
4338

44-
public static function tearDownAfterClass(): void
45-
{
46-
self::$process->stop();
39+
self::startCloudSqlProxy($connectionName, $socketDir, $port);
4740
}
4841

4942
public function testTcpConnection()

0 commit comments

Comments
 (0)