|
18 | 18 | namespace Google\Cloud\Samples\Spanner;
|
19 | 19 |
|
20 | 20 | use PHPUnit\Framework\TestCase;
|
| 21 | +use Google\Cloud\TestUtils\TestTrait; |
21 | 22 |
|
22 | 23 | class quickstartTest extends TestCase
|
23 | 24 | {
|
24 |
| - protected static $file; |
| 25 | + use TestTrait; |
25 | 26 |
|
26 | 27 | protected static $tempFile;
|
27 | 28 |
|
28 | 29 | public function setUp()
|
29 | 30 | {
|
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'); |
42 | 34 |
|
43 | 35 | 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'); |
47 | 36 | $contents = str_replace(
|
48 | 37 | ['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') |
51 | 40 | );
|
52 |
| - file_put_contents(self::$file, $contents); |
| 41 | + file_put_contents(self::$tempFile, $contents); |
53 | 42 | }
|
54 | 43 |
|
55 | 44 | public function testQuickstart()
|
56 | 45 | {
|
57 | 46 | // Invoke quickstart.php
|
58 |
| - $results = include self::$file; |
| 47 | + $output = $this->runSnippet(self::$tempFile); |
59 | 48 |
|
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); |
67 | 50 | }
|
68 | 51 | }
|
0 commit comments