22
33namespace Tests ;
44
5- use Illuminate \Container \Container ;
6- use Illuminate \Filesystem \Filesystem ;
5+ use Mockery as m ;
76use PHPUnit \Framework \TestCase ;
87use Aws \Credentials \Credentials ;
98use Aws \S3 \S3Client ;
9+ use Aws \S3 \PostObjectV4 ;
10+ use Aws \Api \DateTimeResult ;
1011use Unisharp \S3 \Presigned \S3PresignedServiceProvider ;
1112use Unisharp \S3 \Presigned \S3Presigned ;
1213use Unisharp \S3 \Presigned \Exceptions \OptionsMissingException ;
@@ -22,6 +23,9 @@ class PresignedTest extends TestCase
2223 'version ' => 'latest ' ,
2324 'bucket ' => 'bucket ' ,
2425 'prefix ' => 'prefix/ ' ,
26+ 's3_client ' => [
27+ 'options ' => []
28+ ],
2529 'options ' => [
2630 'foo ' => 'bar '
2731 ]
@@ -32,18 +36,18 @@ protected function setUp()
3236 parent ::setUp ();
3337 }
3438
35- public function testCheckOptions ()
36- {
37- $ options = [
38- 'bucket ' => 'bucket ' ,
39- 'prefix ' => 'prefix/ ' ,
40- 'options ' => [
41- 'foo ' => 'bar '
42- ]
43- ];
44- $ this ->expectException (OptionsMissingException::class);
45- $ s3Presigned = $ this ->getS3Presigned ($ options );
46- }
39+ // public function testCheckOptions()
40+ // {
41+ // $configs = [
42+ // 'bucket' => 'bucket',
43+ // 'prefix' => 'prefix/',
44+ // 'options' => [
45+ // 'foo' => 'bar'
46+ // ]
47+ // ];
48+ // $this->expectException(OptionsMissingException::class);
49+ // $s3Presigned = $this->getS3Presigned($configs );
50+ // }
4751
4852 public function testSetPrefix ()
4953 {
@@ -85,24 +89,75 @@ public function testGetUploadForm()
8589 $ this ->assertEquals ($ result ['inputs ' ]['foo ' ], 'bar ' );
8690 }
8791
88- private function getS3Presigned ($ options = [])
92+ public function testListObjects ()
93+ {
94+ $ number = 10 ;
95+ $ url = "https:// {$ this ->configs ['bucket ' ]}.s3-ap-northeast-1.amazonaws.com/public/ " ;
96+ $ s3Client = m::mock (S3Client::class);
97+ $ s3Client ->shouldReceive ('getPaginator ' )
98+ ->once ()
99+ ->with ('ListObjects ' , m::type ('array ' ))
100+ ->andReturn ([$ this ->getMockedObjects ($ number )]);
101+
102+ $ s3Presigned = $ this ->getS3Presigned ([], $ s3Client );
103+ $ objects = $ s3Presigned ->listObjects ();
104+ $ this ->assertEquals ($ number , count ($ objects ));
105+ $ this ->assertEquals ($ url , $ objects [0 ]['Url ' ]);
106+ }
107+
108+ protected function getMockedObjects ($ number = 5 )
109+ {
110+ $ objects = m::mock (\stdObject::class);
111+ $ objects ->shouldReceive ('get ' )
112+ ->once ()
113+ ->with ('Contents ' )
114+ ->andReturn (array_fill (0 , $ number , [
115+ 'Key ' => 'public/ ' ,
116+ 'LastModified ' => DateTimeResult::fromEpoch (time ()),
117+ 'ETag ' => 'etag ' ,
118+ 'Size ' => 0 ,
119+ 'StorageClass ' => 'STANDARD ' ,
120+ 'Owner ' => [
121+ 'DisplayName ' => 'seafood ' ,
122+ 'ID ' => 'owner_id ' ,
123+ ]
124+ ]));
125+ $ objects ->shouldReceive ('get ' )
126+ ->once ()
127+ ->with ('CommonPrefixes ' )
128+ ->andReturn ([]);
129+
130+ return $ objects ;
131+ }
132+
133+ protected function getS3Presigned (array $ configs = [], S3Client $ s3Client = null )
134+ {
135+ $ configs = array_merge ($ this ->configs , $ configs );
136+ $ s3Client = $ s3Client ? $ s3Client : $ this ->getS3Client ($ configs );
137+
138+ return new S3Presigned (
139+ $ s3Client ,
140+ $ configs ['region ' ],
141+ $ configs ['bucket ' ],
142+ $ configs ['prefix ' ],
143+ $ configs ['options ' ]
144+ );
145+ }
146+
147+ protected function getS3Client (array $ configs )
89148 {
90- $ configs = $ this ->configs ;
91149 $ credentials = new Credentials (
92150 $ configs ['credentials ' ]['access_key ' ],
93151 $ configs ['credentials ' ]['secret_key ' ]
94152 );
95- $ s3Client = new S3Client ([
153+
154+ return new S3Client ([
96155 'region ' => $ configs ['region ' ],
97156 'version ' => $ configs ['version ' ],
98157 'credentials ' => $ credentials ,
99158 'options ' => [
100- $ configs ['options ' ]
159+ $ configs ['s3_client ' ][ ' options ' ]
101160 ]
102161 ]);
103-
104- $ options = $ options ?: $ configs ;
105-
106- return new S3Presigned ($ s3Client , $ options ['bucket ' ], $ options ['prefix ' ], $ options );
107162 }
108163}
0 commit comments