feat(storage): add Object Contexts samples and system tests#2200
feat(storage): add Object Contexts samples and system tests#2200salilg-eng wants to merge 8 commits intoGoogleCloudPlatform:mainfrom
Conversation
|
Here is the summary of changes. You are about to add 3 region tags.
This comment is generated by snippet-bot.
|
|
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. |
There was a problem hiding this comment.
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.
| public function testObjectContexts() | ||
| { | ||
| $objectContextsBucket = self::$storage->createBucket( | ||
| sprintf('%s-test-bucket-%s', self::$projectId, time()) |
There was a problem hiding this comment.
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())There was a problem hiding this comment.
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()
| $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)); | ||
| } |
There was a problem hiding this comment.
Yes both object and bucket deleted
| self::$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'; | ||
| } | ||
|
|
||
| public function testObjectContexts() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That is my bad above description mentioned storageTest.php actually made changes in ObjectTest.php file.
| ]); | ||
| $objectName = $object->info()['name']; |
| printf(' - Key: %s, Value: %s' . PHP_EOL, $key, $data['value']); | ||
| printf(' - Created: %s' . PHP_EOL, $data['createTime']); | ||
| printf(' - Updated: %s' . PHP_EOL, $data['updateTime']); |
There was a problem hiding this comment.
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'] ?? '');
Uh oh!
There was an error while loading. Please reload this page.