Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion storage/src/compose_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
* (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 composition.
* (e.g. false)
*/
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);
Expand All @@ -60,6 +62,14 @@ function compose_file(string $bucketName, string $firstObjectName, string $secon
$firstObjectName,
$secondObjectName
);

if ($deleteSourceObjects) {
foreach ($objectsToCompose as $sourceObject) {
$bucket->object($sourceObject)->delete();
}
printf(' and source objects were deleted');
}
printf(PHP_EOL);
}
}
# [END storage_compose_file]
Expand Down
51 changes: 38 additions & 13 deletions storage/test/ObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ public function provideMoveObject()
return [[true], [false]];
}

public function testCompose()
/**
* @dataProvider provideCompose
*/
public function testCompose(bool $deleteSourceObjects)
{
$bucket = self::$storage->bucket(self::$bucketName);
$object1Name = uniqid('compose-object1-');
Expand All @@ -269,28 +272,50 @@ public function testCompose()
$bucket->upload('content', ['name' => $object2Name]);

$targetName = uniqid('compose-object-target-');
$output = self::runFunctionSnippet('compose_file', [

$args = [
self::$bucketName,
$object1Name,
$object2Name,
$targetName,
]);
];

$this->assertEquals(
sprintf(
'New composite object %s was created by combining %s and %s',
$targetName,
$object1Name,
$object2Name
),
$output
if ($deleteSourceObjects) {
$args[] = true;
}

$output = self::runFunctionSnippet('compose_file', $args);

$expectedOutput = sprintf(
'New composite object %s was created by combining %s and %s',
$targetName,
$object1Name,
$object2Name
);

$bucket->object($object1Name)->delete();
$bucket->object($object2Name)->delete();
if ($deleteSourceObjects) {
$expectedOutput .= ' and source objects were deleted';
}
$expectedOutput .= PHP_EOL;

$this->assertEquals($expectedOutput, $output);

$this->assertEquals(!$deleteSourceObjects, $bucket->object($object1Name)->exists());
$this->assertEquals(!$deleteSourceObjects, $bucket->object($object2Name)->exists());
Comment on lines +303 to +304

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

Using assertEquals with a boolean value is a PHPUnit anti-pattern. It is more idiomatic and readable to use assertTrue or assertFalse directly, which also aligns with the assertion style used elsewhere in this test file.

        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());
        }


if (!$deleteSourceObjects) {
$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();
Expand Down