From e1655c0c25b3b17113ed538dd840268bcecd2edc Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Tue, 16 Jun 2026 06:21:02 +0000 Subject: [PATCH] feat(storage): update compose sample to support deleteSourceObjects option Update compose_file snippet to support deleteSourceObjects option. Add corresponding integration tests. [Generated-by: AI] --- storage/src/compose_file.php | 17 +++++++++++++---- storage/test/ObjectsTest.php | 28 +++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/storage/src/compose_file.php b/storage/src/compose_file.php index c355f2d584..a61b7ed269 100644 --- a/storage/src/compose_file.php +++ b/storage/src/compose_file.php @@ -37,9 +37,15 @@ * (e.g. 'my-object-2') * @param string $targetObjectName The name of the object to be created. * (e.g. 'composed-my-object-1-my-object-2') + * @param bool $deleteSourceObjects Whether to delete the source objects after composing. */ -function compose_file(string $bucketName, string $firstObjectName, string $secondObjectName, string $targetObjectName): void -{ +function compose_file( + string $bucketName, + string $firstObjectName, + string $secondObjectName, + string $targetObjectName, + bool $deleteSourceObjects = false +): void { $storage = new StorageClient(); $bucket = $storage->bucket($bucketName); @@ -48,17 +54,20 @@ function compose_file(string $bucketName, string $firstObjectName, string $secon $objectsToCompose = [$firstObjectName, $secondObjectName]; $targetObject = $bucket->compose($objectsToCompose, $targetObjectName, [ + 'deleteSourceObjects' => $deleteSourceObjects, 'destination' => [ 'contentType' => 'application/octet-stream' ] ]); if ($targetObject->exists()) { + $deletionMessage = $deleteSourceObjects ? ' and the source objects were deleted' : ''; printf( - 'New composite object %s was created by combining %s and %s', + 'New composite object %s was created by combining %s and %s%s', $targetObject->name(), $firstObjectName, - $secondObjectName + $secondObjectName, + $deletionMessage ); } } diff --git a/storage/test/ObjectsTest.php b/storage/test/ObjectsTest.php index e52ff81f5a..a6ce65f47f 100644 --- a/storage/test/ObjectsTest.php +++ b/storage/test/ObjectsTest.php @@ -260,7 +260,10 @@ public function provideMoveObject() return [[true], [false]]; } - public function testCompose() + /** + * @dataProvider provideCompose + */ + public function testCompose($deleteSourceObjects) { $bucket = self::$storage->bucket(self::$bucketName); $object1Name = uniqid('compose-object1-'); @@ -274,23 +277,38 @@ public function testCompose() $object1Name, $object2Name, $targetName, + $deleteSourceObjects ]); + $deletionMessage = $deleteSourceObjects ? ' and the source objects were deleted' : ''; $this->assertEquals( sprintf( - 'New composite object %s was created by combining %s and %s', + 'New composite object %s was created by combining %s and %s%s', $targetName, $object1Name, - $object2Name + $object2Name, + $deletionMessage ), $output ); - $bucket->object($object1Name)->delete(); - $bucket->object($object2Name)->delete(); + if ($deleteSourceObjects) { + $this->assertFalse($bucket->object($object1Name)->exists()); + $this->assertFalse($bucket->object($object2Name)->exists()); + } else { + $this->assertTrue($bucket->object($object1Name)->exists()); + $this->assertTrue($bucket->object($object2Name)->exists()); + $bucket->object($object1Name)->delete(); + $bucket->object($object2Name)->delete(); + } $bucket->object($targetName)->delete(); } + public function provideCompose() + { + return [[true], [false]]; + } + public function testUploadAndDownloadObjectFromMemory() { $objectName = 'test-object-' . time();