1717namespace Google \Cloud \Test \GettingStarted ;
1818
1919use Google \Cloud \TestUtils \TestTrait ;
20+ use Google \Cloud \TestUtils \CloudSqlProxyTrait ;
2021use Google \Cloud \Samples \AppEngine \GettingStarted \CloudSqlDataModel ;
2122use PHPUnit \Framework \TestCase ;
2223use PDO ;
23- use Symfony \Component \Process \Process ;
2424
2525class 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 ;
0 commit comments