|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Spanner\Tests\System; |
| 19 | + |
| 20 | +use Google\Cloud\Spanner; |
| 21 | +use Google\Cloud\Spanner\SpannerClient; |
| 22 | + |
| 23 | +trait SystemTestCaseTrait |
| 24 | +{ |
| 25 | + const TESTING_PREFIX = 'gcloud_testing_'; |
| 26 | + const INSTANCE_NAME = 'google-cloud-php-system-tests'; |
| 27 | + |
| 28 | + const TEST_TABLE_NAME = 'Users'; |
| 29 | + const TEST_INDEX_NAME = 'uniqueIndex'; |
| 30 | + |
| 31 | + const DATABASE_ROLE = 'Reader'; |
| 32 | + const RESTRICTIVE_DATABASE_ROLE = 'RestrictiveReader'; |
| 33 | + |
| 34 | + protected static $client; |
| 35 | + protected static $instance; |
| 36 | + protected static $database; |
| 37 | + protected static $database2; |
| 38 | + protected static $dbName; |
| 39 | + protected static $hasSetUp = false; |
| 40 | + |
| 41 | + protected static function getClient() |
| 42 | + { |
| 43 | + if (self::$client) { |
| 44 | + return self::$client; |
| 45 | + } |
| 46 | + |
| 47 | + $keyFilePath = getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH'); |
| 48 | + |
| 49 | + $clientConfig = [ |
| 50 | + 'keyFilePath' => $keyFilePath, |
| 51 | + 'cacheItemPool' => self::getCacheItemPool(), |
| 52 | + ]; |
| 53 | + |
| 54 | + $serviceAddress = getenv('SPANNER_SERVICE_ADDRESS'); |
| 55 | + if ($serviceAddress) { |
| 56 | + $gapicConfig = [ |
| 57 | + 'serviceAddress' => $serviceAddress |
| 58 | + ]; |
| 59 | + |
| 60 | + $clientConfig['gapicSpannerClient'] = new Spanner\V1\Client\SpannerClient($gapicConfig); |
| 61 | + $clientConfig['gapicSpannerDatabaseAdminClient'] = |
| 62 | + new Spanner\Admin\Database\V1\Client\DatabaseAdminClient($gapicConfig); |
| 63 | + $clientConfig['gapicSpannerInstanceAdminClient'] = |
| 64 | + new Spanner\Admin\Instance\V1\Client\InstanceAdminClient($gapicConfig); |
| 65 | + |
| 66 | + echo 'Using Service Address: ' . $serviceAddress . PHP_EOL; |
| 67 | + } |
| 68 | + |
| 69 | + return self::$client = new SpannerClient($clientConfig); |
| 70 | + } |
| 71 | + |
| 72 | + public static function getDatabaseInstance($dbName, $options = []) |
| 73 | + { |
| 74 | + return self::getClient()->connect(self::INSTANCE_NAME, $dbName, $options); |
| 75 | + } |
| 76 | + |
| 77 | + public static function getDatabaseFromInstance($instance, $dbName, $options = []) |
| 78 | + { |
| 79 | + $instance = self::getClient()->instance($instance); |
| 80 | + return $instance->database($dbName, $options); |
| 81 | + } |
| 82 | + |
| 83 | + public static function skipEmulatorTests() |
| 84 | + { |
| 85 | + if (self::isEmulatorUsed()) { |
| 86 | + self::markTestSkipped('This test is not supported by the emulator.'); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static function emulatorOnly() |
| 91 | + { |
| 92 | + if (!self::isEmulatorUsed()) { |
| 93 | + self::markTestSkipped('This test is only supported by the emulator.'); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static function isEmulatorUsed(): bool |
| 98 | + { |
| 99 | + return (bool) getenv('SPANNER_EMULATOR_HOST'); |
| 100 | + } |
| 101 | + |
| 102 | + public static function getDbWithReaderRole() |
| 103 | + { |
| 104 | + return self::getDatabaseFromInstance( |
| 105 | + self::INSTANCE_NAME, |
| 106 | + self::$dbName, |
| 107 | + ['databaseRole' => self::DATABASE_ROLE] |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + public static function getDbWithRestrictiveRole() |
| 112 | + { |
| 113 | + return self::getDatabaseFromInstance( |
| 114 | + self::INSTANCE_NAME, |
| 115 | + self::$dbName, |
| 116 | + ['databaseRole' => self::RESTRICTIVE_DATABASE_ROLE] |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments