Skip to content

feat(storage): add Object Contexts samples and system tests#2200

Open
salilg-eng wants to merge 8 commits intoGoogleCloudPlatform:mainfrom
salilg-eng:feat/sample-object-contexts
Open

feat(storage): add Object Contexts samples and system tests#2200
salilg-eng wants to merge 8 commits intoGoogleCloudPlatform:mainfrom
salilg-eng:feat/sample-object-contexts

Conversation

@salilg-eng
Copy link
Copy Markdown

@salilg-eng salilg-eng commented Apr 20, 2026

  • Add set_object_contexts.php to demonstrate upload contexts.
  • Add get_object_contexts.php to show retrieval of structured metadata.
  • Add list_object_contexts.js to demonstrate server-side filtering.
  • Implement comprehensive system tests in ObjectTest.php covering presence, absence, and existence filter operators.
  • Ensure samples use the correct 'contexts' field with 'custom' map structure.

@salilg-eng salilg-eng requested review from a team as code owners April 20, 2026 13:32
@snippet-bot
Copy link
Copy Markdown

snippet-bot bot commented Apr 20, 2026

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. labels Apr 20, 2026
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 20, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces three new PHP samples for managing Cloud Storage object contexts: get_object_contexts, list_object_contexts, and set_object_contexts. It also includes a comprehensive test case in ObjectsTest.php to verify these functionalities. Feedback highlights several issues in the test implementation, including the use of an uninitialized project ID variable which results in invalid bucket names, a resource leak due to buckets not being deleted after tests, and redundant variable assignments. Additionally, it is recommended to use null coalescing operators in the samples for better robustness and to include missing filter operator test cases mentioned in the PR description.

Comment thread storage/test/ObjectsTest.php Outdated
public function testObjectContexts()
{
$objectContextsBucket = self::$storage->createBucket(
sprintf('%s-test-bucket-%s', self::$projectId, time())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The variable self::$projectId is uninitialized in this test class, which will cause the bucket name to start with a hyphen (e.g., -test-bucket-...). This is an invalid bucket name in Cloud Storage and will cause the test to fail. Additionally, using uniqid() is preferred over time() to avoid collisions during parallel test runs.

            sprintf('%s-test-bucket-%s', self::$storage->projectId(), uniqid())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are using the testTrait file and have the $projectId defined, I believe this approach is correct. However, following your feedback, I have updated time() to uniqid()

Comment on lines +47 to +104
$objectContextsBucket = self::$storage->createBucket(
sprintf('%s-test-bucket-%s', self::$projectId, time())
);
$bucketName = $objectContextsBucket->name();
$objectName = 'object-contexts-'.uniqid();
$object = $objectContextsBucket->upload('test', [
'name' => $objectName,
]);
$objectName = $object->info()['name'];

// Set Object Contexts
$setOutput = $this->runFunctionSnippet('set_object_contexts', [
$bucketName,
$objectName,
]);

$this->assertStringContainsString("Contexts for object $objectName were updated", $setOutput);

// Get Object Contexts
$getOutput = $this->runFunctionSnippet('get_object_contexts', [
$bucketName,
$objectName
]);

$this->assertStringContainsString('Key: department, Value: finance', $getOutput);
$this->assertStringContainsString('Key: priority, Value: high', $getOutput);

// List Object Contexts using filter
$listOutput = $this->runFunctionSnippet('list_object_contexts', [
$bucketName
]);
$this->assertStringContainsString("Found object: $objectName", $listOutput);

// Clear all contexts on the object.
$object->update([
'contexts' => [
'custom' => null,
],
]);
$object->reload();

$clearedGetOutput = $this->runFunctionSnippet('get_object_contexts', [
$bucketName,
$objectName
]);

$this->assertStringNotContainsString('Key: department, Value: finance', $clearedGetOutput);
$this->assertStringNotContainsString('Key: priority, Value: high', $clearedGetOutput);

$clearedInfo = $object->info();
$this->assertTrue(empty($clearedInfo['contexts']['custom'] ?? []));

// List again with filter after clearing contexts.
$clearedListOutput = $this->runFunctionSnippet('list_object_contexts', [
$bucketName
]);
$this->assertEmpty(trim($clearedListOutput));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The bucket created for this test is never deleted, leading to resource leaks in the test project. Ensure the bucket is deleted at the end of the test, or consider using the shared bucket self::$bucketName if isolation is not strictly required.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes both object and bucket deleted

self::$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
}

public function testObjectContexts()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The PR description mentions implementing comprehensive system tests covering presence, absence (-), and existence (:) filter operators in storageTest.php. However, these tests are missing from the current changes in ObjectsTest.php. Please include the promised test cases to ensure full coverage of the filtering logic.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is my bad above description mentioned storageTest.php actually made changes in ObjectTest.php file.

Comment thread storage/test/ObjectsTest.php Outdated
Comment on lines +54 to +55
]);
$objectName = $object->info()['name'];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This assignment is redundant as $objectName is already defined on line 51 and its value remains unchanged after the upload. Removing this line improves code clarity and avoids an unnecessary call to the info() method.

        ]);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name changed.

Comment thread storage/src/get_object_contexts.php Outdated
Comment on lines +47 to +49
printf(' - Key: %s, Value: %s' . PHP_EOL, $key, $data['value']);
printf(' - Created: %s' . PHP_EOL, $data['createTime']);
printf(' - Updated: %s' . PHP_EOL, $data['updateTime']);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider using the null coalescing operator (??) when accessing context fields to make the sample more robust against unexpected API response structures or missing metadata fields.

            printf(' - Key: %s, Value: %s' . PHP_EOL, $key, $data['value'] ?? '');
            printf(' - Created: %s' . PHP_EOL, $data['createTime'] ?? '');
            printf(' - Updated: %s' . PHP_EOL, $data['updateTime'] ?? '');

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant