2
2
3
3
namespace PlatformCommunity \Flysystem \BunnyCDN \Tests ;
4
4
5
- use GuzzleHttp \Psr7 \Response ;
5
+ use League \Flysystem \FilesystemException ;
6
+ use function PHPUnit \Framework \assertEmpty ;
6
7
use PHPUnit \Framework \TestCase ;
7
8
use PlatformCommunity \Flysystem \BunnyCDN \BunnyCDNClient ;
8
9
use PlatformCommunity \Flysystem \BunnyCDN \Exceptions \BunnyCDNException ;
11
12
12
13
class ClientTest extends TestCase
13
14
{
14
- const STORAGE_ZONE = 'example_storage_zone ' ;
15
+ const STORAGE_ZONE = 'test_storage_zone ' ;
15
16
16
- public BunnyCDNClient |MockClient $ client ;
17
+ public BunnyCDNClient $ client ;
18
+
19
+ private static function bunnyCDNClient (): BunnyCDNClient
20
+ {
21
+ global $ storage_zone ;
22
+ global $ api_key ;
23
+
24
+ if ($ storage_zone !== null && $ api_key !== null ) {
25
+ return new BunnyCDNClient ($ storage_zone , $ api_key );
26
+ } else {
27
+ return new MockClient (self ::STORAGE_ZONE , '123 ' );
28
+ }
29
+ }
17
30
18
31
protected function setUp (): void
19
32
{
20
- $ this ->client = new MockClient (self ::STORAGE_ZONE , 'b0e98a1b-d62d-4c31-aae0df94bbf6-1592-4f66 ' );
33
+ $ this ->client = self ::bunnyCDNClient ();
34
+ $ this ->clearStorage ();
35
+ }
36
+
37
+ private function clearStorage ()
38
+ {
39
+ foreach ($ this ->client ->list ('/ ' ) as $ item ) {
40
+ try {
41
+ $ this ->client ->delete ($ item ['IsDirectory ' ] ? $ item ['ObjectName ' ].'/ ' : $ item ['ObjectName ' ]);
42
+ } catch (\Exception $ exception ) {
43
+ } // Try our best effort at removing everything from the filesystem
44
+ }
45
+
46
+ assertEmpty (
47
+ $ this ->client ->list ('/ ' ),
48
+ 'Warning! Bunny Client not emptied out prior to next test. This can be problematic when running the test against production clients '
49
+ );
50
+ }
51
+
52
+ protected function tearDown (): void
53
+ {
54
+ $ this ->clearStorage ();
21
55
}
22
56
23
57
/**
@@ -28,16 +62,9 @@ protected function setUp(): void
28
62
*/
29
63
public function test_listing_directory ()
30
64
{
31
- if ($ this ->client instanceof MockClient) {
32
- $ this ->client ->add_response (
33
- new Response (200 , [], json_encode (
34
- [
35
- $ this ->client ::example_folder ('subfolder ' , self ::STORAGE_ZONE ),
36
- $ this ->client ::example_file ('example_image.png ' , self ::STORAGE_ZONE ),
37
- ]
38
- ))
39
- );
40
- }
65
+ // Arrange
66
+ $ this ->client ->make_directory ('subfolder ' );
67
+ $ this ->client ->upload ('example_image.png ' , 'test ' );
41
68
42
69
$ response = $ this ->client ->list ('/ ' );
43
70
@@ -53,35 +80,27 @@ public function test_listing_directory()
53
80
*/
54
81
public function test_listing_subdirectory ()
55
82
{
56
- if ($ this ->client instanceof MockClient) {
57
- $ this ->client ->add_response (
58
- new Response (200 , [], json_encode (
59
- [
60
- $ this ->client ::example_file ('/subfolder/example_image.png ' , self ::STORAGE_ZONE ),
61
- ]
62
- ))
63
- );
64
- }
83
+ // Arrange
84
+ $ this ->client ->upload ('/subfolder/example_image.png ' , 'test ' );
65
85
86
+ // Act
66
87
$ response = $ this ->client ->list ('/subfolder ' );
67
88
89
+ // Assert
68
90
$ this ->assertIsArray ($ response );
69
91
$ this ->assertCount (1 , $ response );
70
92
}
71
93
72
94
/**
73
95
* @return void
74
96
*
75
- * @throws NotFoundException
76
97
* @throws BunnyCDNException
98
+ * @throws FilesystemException
99
+ * @throws NotFoundException
77
100
*/
78
101
public function test_download_file ()
79
102
{
80
- if ($ this ->client instanceof MockClient) {
81
- $ this ->client ->add_response (
82
- new Response (200 , [], 'example_image_contents ' )
83
- );
84
- }
103
+ $ this ->client ->upload ('/test.png ' , 'test ' );
85
104
86
105
$ response = $ this ->client ->download ('/test.png ' );
87
106
@@ -91,16 +110,13 @@ public function test_download_file()
91
110
/**
92
111
* @return void
93
112
*
94
- * @throws NotFoundException
95
113
* @throws BunnyCDNException
114
+ * @throws FilesystemException
115
+ * @throws NotFoundException
96
116
*/
97
117
public function test_streaming ()
98
118
{
99
- if ($ this ->client instanceof MockClient) {
100
- $ this ->client ->add_response (
101
- new Response (200 , [], str_repeat ('example_image_contents ' , 1024000 )),
102
- );
103
- }
119
+ $ this ->client ->upload ('/test.png ' , str_repeat ('example_image_contents ' , 1024 ));
104
120
105
121
$ stream = $ this ->client ->stream ('/test.png ' );
106
122
@@ -120,18 +136,10 @@ public function test_streaming()
120
136
*/
121
137
public function test_upload ()
122
138
{
123
- if ($ this ->client instanceof MockClient) {
124
- $ this ->client ->add_response (
125
- new Response (201 , [], json_encode ([
126
- 'HttpCode ' => 201 ,
127
- 'Message ' => 'File uploaded. ' ,
128
- ]))
129
- );
130
- }
131
-
132
139
$ response = $ this ->client ->upload ('/test_contents.txt ' , 'testing_contents ' );
133
140
134
141
$ this ->assertIsArray ($ response );
142
+
135
143
$ this ->assertEquals ([
136
144
'HttpCode ' => 201 ,
137
145
'Message ' => 'File uploaded. ' ,
@@ -145,15 +153,6 @@ public function test_upload()
145
153
*/
146
154
public function test_make_directory ()
147
155
{
148
- if ($ this ->client instanceof MockClient) {
149
- $ this ->client ->add_response (
150
- new Response (201 , [], json_encode ([
151
- 'HttpCode ' => 201 ,
152
- 'Message ' => 'Directory created. ' ,
153
- ]))
154
- );
155
- }
156
-
157
156
$ response = $ this ->client ->make_directory ('/test_dir/ ' );
158
157
159
158
$ this ->assertIsArray ($ response );
@@ -166,22 +165,16 @@ public function test_make_directory()
166
165
/**
167
166
* @return void
168
167
*
169
- * @throws NotFoundException
170
168
* @throws BunnyCDNException
171
169
* @throws DirectoryNotEmptyException
170
+ * @throws FilesystemException
171
+ * @throws NotFoundException
172
172
*/
173
173
public function test_delete_file ()
174
174
{
175
- if ($ this ->client instanceof MockClient) {
176
- $ this ->client ->add_response (
177
- new Response (200 , [], json_encode ([
178
- 'HttpCode ' => 200 ,
179
- 'Message ' => 'File deleted successfuly. ' , // ಠ_ಠ Spelling @bunny.net
180
- ]))
181
- );
182
- }
175
+ $ this ->client ->upload ('test_file.txt ' , '123 ' );
183
176
184
- $ response = $ this ->client ->delete ('/testing .txt ' );
177
+ $ response = $ this ->client ->delete ('/test_file .txt ' );
185
178
186
179
$ this ->assertIsArray ($ response );
187
180
$ this ->assertEquals ([
@@ -193,68 +186,14 @@ public function test_delete_file()
193
186
/**
194
187
* @return void
195
188
*
196
- * @throws NotFoundException
197
189
* @throws BunnyCDNException
198
190
* @throws DirectoryNotEmptyException
191
+ * @throws NotFoundException
192
+ * @throws FilesystemException
199
193
*/
200
194
public function test_delete_file_not_found ()
201
195
{
202
196
$ this ->expectException (NotFoundException::class);
203
-
204
- if ($ this ->client instanceof MockClient) {
205
- $ this ->client ->add_response (
206
- new Response (404 , [], json_encode ([
207
- 'HttpCode ' => 404 ,
208
- 'Message ' => 'Object Not Found. ' ,
209
- ]))
210
- );
211
- }
212
-
213
- $ this ->client ->delete ('/file_not_found.txt ' );
197
+ $ this ->client ->delete ('file_not_found.txt ' );
214
198
}
215
-
216
- /**
217
- * Utility Classes
218
- */
219
-
220
- // /**
221
- // * @param $path
222
- // * @param $contents
223
- // * @return void
224
- // * @throws BunnyCDNException
225
- // */
226
- // private function givenWeHaveAnExistingFile($path = '/example_file', $contents = ''): void
227
- // {
228
- // if($this->client instanceof MockClient) {
229
- // $this->client->add_response(
230
- // new Response(200, [], json_encode(
231
- // [
232
- // $this->client::example_file($path, self::STORAGE_ZONE)
233
- // ]
234
- // ))
235
- // );
236
- // } else {
237
- // $this->client->upload($path, $contents);
238
- // }
239
- // }
240
- //
241
- // /**
242
- // * @param $path
243
- // * @return void
244
- // * @throws BunnyCDNException
245
- // */
246
- // public function givenWeHaveAnExistingFolder($path = '/example_folder'): void
247
- // {
248
- // if($this->client instanceof MockClient) {
249
- // $this->client->add_response(
250
- // new Response(200, [], json_encode(
251
- // [
252
- // $this->client::example_folder($path, self::STORAGE_ZONE)
253
- // ]
254
- // ))
255
- // );
256
- // } else {
257
- // $this->client->make_directory($path);
258
- // }
259
- // }
260
199
}
0 commit comments