Skip to content

Commit 4269b48

Browse files
authored
fixes quickstart test (GoogleCloudPlatform#930)
1 parent f0360bc commit 4269b48

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

spanner/test/quickstartTest.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,34 @@
1818
namespace Google\Cloud\Samples\Spanner;
1919

2020
use PHPUnit\Framework\TestCase;
21+
use Google\Cloud\TestUtils\TestTrait;
2122

2223
class quickstartTest extends TestCase
2324
{
24-
protected static $file;
25+
use TestTrait;
2526

2627
protected static $tempFile;
2728

2829
public function setUp()
2930
{
30-
if (!extension_loaded('grpc')) {
31-
self::markTestSkipped('Must enable grpc extension.');
32-
}
33-
if (!$projectId = getenv('GOOGLE_PROJECT_ID')) {
34-
$this->markTestSkipped('GOOGLE_PROJECT_ID must be set.');
35-
}
36-
if (!$instanceId = getenv('GOOGLE_SPANNER_INSTANCE_ID')) {
37-
$this->markTestSkipped('GOOGLE_SPANNER_INSTANCE_ID must be set.');
38-
}
39-
if (!$databaseId = getenv('GOOGLE_SPANNER_DATABASE_ID')) {
40-
$this->markTestSkipped('GOOGLE_SPANNER_DATABASE_ID must be set.');
41-
}
31+
$this->requireGrpc();
32+
$instanceId = $this->requireEnv('GOOGLE_SPANNER_INSTANCE_ID');
33+
$databaseId = $this->requireEnv('GOOGLE_SPANNER_DATABASE_ID');
4234

4335
self::$tempFile = sys_get_temp_dir() . '/spanner_quickstart.php';
44-
self::$file = __DIR__ . '/../quickstart.php';
45-
copy(self::$file, self::$tempFile);
46-
$contents = file_get_contents(__DIR__ . '/../quickstart.php');
4736
$contents = str_replace(
4837
['YOUR_PROJECT_ID', 'your-instance-id', 'your-database-id', '__DIR__'],
49-
[$projectId, $instanceId, $databaseId, sprintf('"%s/.."', __DIR__)],
50-
$contents
38+
[self::$projectId, $instanceId, $databaseId, sprintf('"%s/.."', __DIR__)],
39+
file_get_contents(__DIR__ . '/../quickstart.php')
5140
);
52-
file_put_contents(self::$file, $contents);
41+
file_put_contents(self::$tempFile, $contents);
5342
}
5443

5544
public function testQuickstart()
5645
{
5746
// Invoke quickstart.php
58-
$results = include self::$file;
47+
$output = $this->runSnippet(self::$tempFile);
5948

60-
$this->expectOutputString('Hello World' . PHP_EOL);
61-
}
62-
63-
public function tearDown()
64-
{
65-
copy(self::$tempFile, self::$file);
66-
unlink(self::$tempFile);
49+
$this->assertContains('Hello World', $output);
6750
}
6851
}

0 commit comments

Comments
 (0)