diff --git a/bigtable/composer.json b/bigtable/composer.json index 85296a4722..64f1b30cfa 100644 --- a/bigtable/composer.json +++ b/bigtable/composer.json @@ -2,5 +2,10 @@ "require": { "google/cloud-bigtable": "^1.0.0", "psr/cache": "^1.0" + }, + "autoload-dev": { + "psr-4": { + "Google\\Cloud\\Samples\\Bigtable\\Tests\\": "test" + } } } diff --git a/bigtable/test/BigtableTestTrait.php b/bigtable/test/BigtableTestTrait.php new file mode 100644 index 0000000000..ae8729bf07 --- /dev/null +++ b/bigtable/test/BigtableTestTrait.php @@ -0,0 +1,98 @@ + self::$projectId, + ]); + } + + public static function createDevInstance($instanceIdPrefix) + { + $instanceId = uniqid($instanceIdPrefix); + $output = self::runSnippet('create_dev_instance', [ + self::$projectId, + $instanceId, + $instanceId, + ]); + + // Verify the instance was created successfully + if (false !== strpos($output, 'Error: ')) { + throw new Exception('Error creating instance: ' . $output); + } + + return $instanceId; + } + + public static function createTable($tableIdPrefix, $columns = []) + { + $tableId = uniqid($tableIdPrefix); + + $formattedParent = self::$tableAdminClient + ->instanceName(self::$projectId, self::$instanceId); + + $columns = $columns ?: ['stats_summary']; + $table = (new Table())->setColumnFamilies(array_combine( + $columns, + array_fill(0, count($columns), new ColumnFamily) + )); + + self::$tableAdminClient->createtable( + $formattedParent, + $tableId, + $table + ); + + return $tableId; + } + + public static function deleteBigtableInstance() + { + $instanceName = self::$instanceAdminClient->instanceName( + self::$projectId, + self::$instanceId + ); + self::$instanceAdminClient->deleteInstance($instanceName); + } +} diff --git a/bigtable/test/bigtableTest.php b/bigtable/test/bigtableTest.php index 3c709edcd6..33e6cbeeff 100644 --- a/bigtable/test/bigtableTest.php +++ b/bigtable/test/bigtableTest.php @@ -2,42 +2,23 @@ namespace Google\Cloud\Samples\Bigtable\Tests; -use Google\Cloud\Bigtable\BigtableClient; -use PHPUnit\Framework\TestCase; - -use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient; -use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient; -use Google\Cloud\TestUtils\ExponentialBackoffTrait; -use Google\Cloud\Bigtable\Admin\V2\Table\View; -use Google\Cloud\TestUtils\TestTrait; use Google\ApiCore\ApiException; +use Google\Cloud\Bigtable\Admin\V2\Table\View; +use PHPUnit\Framework\TestCase; final class BigtableTest extends TestCase { - use TestTrait, ExponentialBackoffTrait; + use BigtableTestTrait; const INSTANCE_ID_PREFIX = 'php-instance-'; const CLUSTER_ID_PREFIX = 'php-cluster-'; const TABLE_ID_PREFIX = 'php-table-'; - private static $instanceAdminClient; - private static $tableAdminClient; - private static $bigtableClient; - private static $instanceId; + private static $clusterId; public static function setUpBeforeClass() { - self::checkProjectEnvVarBeforeClass(); - self::$instanceAdminClient = new BigtableInstanceAdminClient(); - self::$tableAdminClient = new BigtableTableAdminClient(); - self::$bigtableClient = new BigtableClient([ - 'projectId' => self::$projectId, - ]); - - self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX); - self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX); - - self::create_production_instance(self::$projectId, self::$instanceId, self::$clusterId); + self::setUpBigtableVars(); } public function setUp() @@ -45,8 +26,31 @@ public function setUp() $this->useResourceExhaustedBackoff(); } + public function testCreateProductionInstance() + { + self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX); + self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX); + + $content = self::runSnippet('create_production_instance', [ + self::$projectId, + self::$instanceId, + self::$clusterId + ]); + + $instanceName = self::$instanceAdminClient->instanceName( + self::$projectId, + self::$instanceId + ); + + $this->checkInstance($instanceName); + } + + /** + * @depends testCreateProductionInstance + */ public function testCreateAndDeleteCluster() { + // Create a new cluster as last cluster in an instance cannot be deleted $clusterId = uniqid(self::CLUSTER_ID_PREFIX); $content = self::runSnippet('create_cluster', [ @@ -57,7 +61,11 @@ public function testCreateAndDeleteCluster() ]); $array = explode(PHP_EOL, $content); - $clusterName = self::$instanceAdminClient->clusterName(self::$projectId, self::$instanceId, $clusterId); + $clusterName = self::$instanceAdminClient->clusterName( + self::$projectId, + self::$instanceId, + $clusterId + ); $this->checkCluster($clusterName); @@ -95,23 +103,9 @@ public function testCreateDevInstance() $this->cleanInstance(self::$projectId, $instanceId); } - public function testCreateProductionInstance() - { - $instanceId = uniqid(self::INSTANCE_ID_PREFIX); - $clusterId = uniqid(self::CLUSTER_ID_PREFIX); - - $content = self::runSnippet('create_production_instance', [ - self::$projectId, - $instanceId, - $clusterId - ]); - - $instanceName = self::$instanceAdminClient->instanceName(self::$projectId, $instanceId); - - $this->checkInstance($instanceName); - $this->cleanInstance(self::$projectId, $instanceId); - } - + /** + * @depends testCreateProductionInstance + */ public function testListInstances() { $content = self::runSnippet('list_instance', [ @@ -125,6 +119,9 @@ public function testListInstances() $this->assertContains(self::$instanceId, $array); } + /** + * @depends testCreateProductionInstance + */ public function testListTable() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -141,6 +138,9 @@ public function testListTable() $this->assertContains('projects/' . self::$projectId . '/instances/' . self::$instanceId . '/tables/' . $tableId, $array); } + /** + * @depends testCreateProductionInstance + */ public function testListColumnFamilies() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -166,6 +166,9 @@ public function testListColumnFamilies() $this->assertContains('{"gcRule":{"union":{"rules":[{"maxNumVersions":2},{"maxAge":"432000s"}]}}}', $array); } + /** + * @depends testCreateProductionInstance + */ public function testListInstanceClusters() { $content = self::runSnippet('list_instance_clusters', [ @@ -179,7 +182,10 @@ public function testListInstanceClusters() $this->assertContains('projects/' . self::$projectId . '/instances/' . self::$instanceId . '/clusters/' . self::$clusterId, $array); } - public function testcreate_table() + /** + * @depends testCreateProductionInstance + */ + public function testCreateTable() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -194,6 +200,9 @@ public function testcreate_table() $this->checkTable($tableName); } + /** + * @depends testCreateProductionInstance + */ public function testCreateFamilyGcUnion() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -226,6 +235,9 @@ public function testCreateFamilyGcUnion() $this->checkRule($tableName, 'cf3', $gcRuleCompare); } + /** + * @depends testCreateProductionInstance + */ public function testCreateFamilyGcNested() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -267,6 +279,9 @@ public function testCreateFamilyGcNested() $this->checkRule($tableName, 'cf5', $gcRuleCompare); } + /** + * @depends testCreateProductionInstance + */ public function testCreateFamilyGcMaxVersions() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -290,6 +305,9 @@ public function testCreateFamilyGcMaxVersions() $this->checkRule($tableName, 'cf2', $gcRuleCompare); } + /** + * @depends testCreateProductionInstance + */ public function testCreateFamilyGcMaxAge() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -313,6 +331,9 @@ public function testCreateFamilyGcMaxAge() $this->checkRule($tableName, 'cf1', $gcRuleCompare); } + /** + * @depends testCreateProductionInstance + */ public function testCreateFamilyGcIntersection() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -345,6 +366,9 @@ public function testCreateFamilyGcIntersection() $this->checkRule($tableName, 'cf4', $gcRuleCompare); } + /** + * @depends testCreateProductionInstance + */ public function testDeleteTable() { $tableId = uniqid(self::TABLE_ID_PREFIX); @@ -369,6 +393,9 @@ public function testDeleteTable() } } + /** + * @depends testCreateProductionInstance + */ public function testHelloWorld() { $this->requireGrpc(); @@ -395,15 +422,9 @@ public function testHelloWorld() $this->assertContains(sprintf('Deleted %s table.', $tableId), $array); } - private static function create_production_instance($projectId, $instanceId, $clusterId) - { - $content = self::runSnippet('create_production_instance', [ - $projectId, - $instanceId, - $clusterId - ]); - } - + /** + * @depends testCreateProductionInstance + */ public function testDeleteInstance() { $instanceName = self::$instanceAdminClient->instanceName(self::$projectId, self::$instanceId); diff --git a/bigtable/test/filterTest.php b/bigtable/test/filterTest.php index eb0663bfd8..d51e7550ab 100644 --- a/bigtable/test/filterTest.php +++ b/bigtable/test/filterTest.php @@ -18,69 +18,32 @@ namespace Google\Cloud\Samples\Bigtable\Tests; -use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient; -use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient; -use Google\Cloud\Bigtable\Admin\V2\ColumnFamily; -use Google\Cloud\Bigtable\Admin\V2\Table; -use Google\Cloud\Bigtable\BigtableClient; use Google\Cloud\Bigtable\Mutations; -use Google\Cloud\TestUtils\ExponentialBackoffTrait; -use Google\Cloud\TestUtils\TestTrait; use PHPUnit\Framework\TestCase; final class FilterTest extends TestCase { - use TestTrait; - use ExponentialBackoffTrait; + use BigtableTestTrait; const INSTANCE_ID_PREFIX = 'phpunit-test-'; const TABLE_ID_PREFIX = 'mobile-time-series-'; - private static $bigtableInstanceAdminClient; - private static $bigtableTableAdminClient; - private static $instanceId; - private static $tableId; private static $timestampMicros; private static $timestampMicrosMinusHr; public static function setUpBeforeClass(): void { self::requireGrpc(); - self::checkProjectEnvVarBeforeClass(); - - self::$bigtableInstanceAdminClient = new BigtableInstanceAdminClient(); - self::$bigtableTableAdminClient = new BigtableTableAdminClient(); - self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX); - self::runSnippet('create_dev_instance', [ - self::$projectId, - self::$instanceId, - self::$instanceId, + self::setUpBigtableVars(); + self::$instanceId = self::createDevInstance(self::INSTANCE_ID_PREFIX); + self::$tableId = self::createTable(self::TABLE_ID_PREFIX, [ + 'cell_plan', + 'stats_summary', ]); - self::$tableId = uniqid(self::TABLE_ID_PREFIX); - - $formattedParent = self::$bigtableTableAdminClient - ->instanceName(self::$projectId, self::$instanceId); - $table = (new Table()) - ->setColumnFamilies([ - "stats_summary" => new ColumnFamily(), - "cell_plan" => new ColumnFamily() - ]); - self::$bigtableTableAdminClient->createtable( - $formattedParent, - self::$tableId, - $table - ); - - $dataClient = new BigtableClient([ - 'projectId' => self::$projectId, - ]); - - $table = $dataClient->table(self::$instanceId, self::$tableId); - self::$timestampMicros = time() * 1000 * 1000; self::$timestampMicrosMinusHr = (time() - 60 * 60) * 1000 * 1000; - $table->mutateRows([ + self::$bigtableClient->table(self::$instanceId, self::$tableId)->mutateRows([ "phone#4c410523#20190501" => (new Mutations()) ->upsert('cell_plan', "data_plan_01gb", true, self::$timestampMicrosMinusHr) ->upsert('cell_plan', "data_plan_01gb", false, self::$timestampMicros) @@ -118,8 +81,7 @@ public function setUp(): void public static function tearDownAfterClass(): void { - $instanceName = self::$bigtableInstanceAdminClient->instanceName(self::$projectId, self::$instanceId); - self::$bigtableInstanceAdminClient->deleteInstance($instanceName); + self::deleteBigtableInstance(); } /** diff --git a/bigtable/test/readTest.php b/bigtable/test/readTest.php index 47484316bb..df9c911b16 100644 --- a/bigtable/test/readTest.php +++ b/bigtable/test/readTest.php @@ -18,63 +18,27 @@ namespace Google\Cloud\Samples\Bigtable\Tests; -use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient; -use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient; -use Google\Cloud\Bigtable\Admin\V2\ColumnFamily; -use Google\Cloud\Bigtable\Admin\V2\Table; -use Google\Cloud\Bigtable\BigtableClient; use Google\Cloud\Bigtable\Mutations; -use Google\Cloud\TestUtils\ExponentialBackoffTrait; -use Google\Cloud\TestUtils\TestTrait; use PHPUnit\Framework\TestCase; final class ReadTest extends TestCase { - use TestTrait; - use ExponentialBackoffTrait; + use BigtableTestTrait; const INSTANCE_ID_PREFIX = 'phpunit-test-'; const TABLE_ID_PREFIX = 'mobile-time-series-'; - private static $bigtableInstanceAdminClient; - private static $bigtableTableAdminClient; - private static $instanceId; - private static $tableId; private static $timestampMicros; public static function setUpBeforeClass(): void { self::requireGrpc(); - self::checkProjectEnvVarBeforeClass(); - - self::$bigtableInstanceAdminClient = new BigtableInstanceAdminClient(); - self::$bigtableTableAdminClient = new BigtableTableAdminClient(); - self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX); - self::runSnippet('create_dev_instance', [ - self::$projectId, - self::$instanceId, - self::$instanceId, - ]); - - self::$tableId = uniqid(self::TABLE_ID_PREFIX); - - $formattedParent = self::$bigtableTableAdminClient - ->instanceName(self::$projectId, self::$instanceId); - $table = (new Table())->setColumnFamilies(["stats_summary" => new ColumnFamily()]); - self::$bigtableTableAdminClient->createtable( - $formattedParent, - self::$tableId, - $table - ); - - $dataClient = new BigtableClient([ - 'projectId' => self::$projectId, - ]); - - $table = $dataClient->table(self::$instanceId, self::$tableId); + self::setUpBigtableVars(); + self::$instanceId = self::createDevInstance(self::INSTANCE_ID_PREFIX); + self::$tableId = self::createTable(self::TABLE_ID_PREFIX); self::$timestampMicros = time() * 1000 * 1000; - $table->mutateRows([ + self::$bigtableClient->table(self::$instanceId, self::$tableId)->mutateRows([ "phone#4c410523#20190501" => (new Mutations()) ->upsert('stats_summary', "connected_cell", 1, self::$timestampMicros) ->upsert('stats_summary', "connected_wifi", 1, self::$timestampMicros) @@ -105,8 +69,7 @@ public function setUp(): void public static function tearDownAfterClass(): void { - $instanceName = self::$bigtableInstanceAdminClient->instanceName(self::$projectId, self::$instanceId); - self::$bigtableInstanceAdminClient->deleteInstance($instanceName); + self::deleteBigtableInstance(); } /** diff --git a/bigtable/test/writeTest.php b/bigtable/test/writeTest.php index 0266b20875..67eaa5ef4a 100644 --- a/bigtable/test/writeTest.php +++ b/bigtable/test/writeTest.php @@ -18,50 +18,20 @@ namespace Google\Cloud\Samples\Bigtable\Tests; -use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient; -use Google\Cloud\Bigtable\Admin\V2\ColumnFamily; use PHPUnit\Framework\TestCase; -use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient; -use Google\Cloud\Bigtable\Admin\V2\Table; -use Google\Cloud\TestUtils\TestTrait; -use Google\Cloud\TestUtils\ExponentialBackoffTrait; - final class WriteTest extends TestCase { - use TestTrait; - use ExponentialBackoffTrait; + use BigtableTestTrait; const INSTANCE_ID_PREFIX = 'phpunit-test-'; const TABLE_ID_PREFIX = 'mobile-time-series-'; - private static $bigtableInstanceAdminClient; - private static $bigtableTableAdminClient; - private static $instanceId; - private static $tableId; public static function setUpBeforeClass(): void { - self::checkProjectEnvVarBeforeClass(); - - self::$bigtableInstanceAdminClient = new BigtableInstanceAdminClient(); - self::$bigtableTableAdminClient = new BigtableTableAdminClient(); - self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX); - self::runSnippet('create_dev_instance', [ - self::$projectId, - self::$instanceId, - self::$instanceId, - ]); - - self::$tableId = uniqid(self::TABLE_ID_PREFIX); - - $formattedParent = self::$bigtableTableAdminClient - ->instanceName(self::$projectId, self::$instanceId); - $table = (new Table())->setColumnFamilies(["stats_summary" => new ColumnFamily()]); - self::$bigtableTableAdminClient->createtable( - $formattedParent, - self::$tableId, - $table - ); + self::setUpBigtableVars(); + self::$instanceId = self::createDevInstance(self::INSTANCE_ID_PREFIX); + self::$tableId = self::createTable(self::TABLE_ID_PREFIX); } public function setUp(): void @@ -71,8 +41,7 @@ public function setUp(): void public static function tearDownAfterClass(): void { - $instanceName = self::$bigtableInstanceAdminClient->instanceName(self::$projectId, self::$instanceId); - self::$bigtableInstanceAdminClient->deleteInstance($instanceName); + self::deleteBigtableInstance(); } public function testWriteSimple()