2020use Google \Cloud \TestUtils \TestTrait ;
2121use Google \Cloud \TestUtils \ExecuteCommandTrait ;
2222use Google \Cloud \Storage \StorageClient ;
23+ use GuzzleHttp \Client ;
2324use PHPUnit \Framework \TestCase ;
2425
2526/**
@@ -30,17 +31,19 @@ class ObjectSignedUrlTest extends TestCase
3031 use TestTrait;
3132 use ExecuteCommandTrait;
3233
34+ private static $ storage ;
3335 private static $ bucketName ;
3436 private static $ objectName ;
3537 private static $ commandFile = __DIR__ . '/../storage.php ' ;
3638
3739 /** @beforeClass */
3840 public static function setUpObject ()
3941 {
40- $ storage = new StorageClient ();
42+ self :: $ storage = new StorageClient ();
4143 self ::$ bucketName = self ::requireEnv ('GOOGLE_STORAGE_BUCKET ' );
4244 self ::$ objectName = sprintf ('test-object-%s ' , time ());
43- $ storage
45+ // Pre-upload an object for testing GET signed urls
46+ self ::$ storage
4447 ->bucket (self ::$ bucketName )
4548 ->upload ("test file content " , [
4649 'name ' => self ::$ objectName
@@ -56,4 +59,48 @@ public function testGetV2SignedUrl()
5659
5760 $ this ->assertContains ("The signed url for " . self ::$ objectName . " is " , $ output );
5861 }
62+
63+ public function testGetV4SignedUrl ()
64+ {
65+ $ output = $ this ->runCommand ('get-object-v4-signed-url ' , [
66+ 'bucket ' => self ::$ bucketName ,
67+ 'object ' => self ::$ objectName ,
68+ ]);
69+
70+ $ this ->assertContains ('Generated GET signed URL: ' , $ output );
71+ }
72+
73+ public function testGetV4UploadSignedUrl ()
74+ {
75+ $ uploadObjectName = sprintf ('test-upload-object-%s ' , time ());
76+
77+ $ output = $ this ->runCommand ('get-object-v4-upload-signed-url ' , [
78+ 'bucket ' => self ::$ bucketName ,
79+ 'object ' => $ uploadObjectName ,
80+ ]);
81+
82+ $ this ->assertContains ('Generated PUT signed URL: ' , $ output );
83+
84+ // Extract the signed URL from command output.
85+ preg_match_all ('/URL:\n([^\s]+)/ ' , $ output , $ matches );
86+ $ url = $ matches [1 ][0 ];
87+
88+ // Make a PUT request using the signed URL.
89+ $ client = new Client ();
90+ $ res = $ client ->request ('PUT ' , $ url , [
91+ 'headers ' => [
92+ 'Content-Type ' => 'application/octet-stream ' ,
93+ ],
94+ 'body ' => 'upload content '
95+ ]);
96+
97+ $ this ->assertEquals (200 , $ res ->getStatusCode ());
98+
99+ // Assert file is correctly uploaded to the bucket.
100+ $ content = self ::$ storage
101+ ->bucket (self ::$ bucketName )
102+ ->object ($ uploadObjectName )
103+ ->downloadAsString ();
104+ $ this ->assertEquals ('upload content ' , $ content );
105+ }
59106}
0 commit comments