Skip to content

Commit 9f9ebbb

Browse files
authored
Merge pull request #52 from PlatformCommunity/feature/flytests-to-live
Test Cleanup (Path Prefixing, Rewriting Mock Client)
2 parents 2f201af + 0a79a26 commit 9f9ebbb

10 files changed

+524
-593
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ report/
99
clover.xml
1010
./tests/BunnyCDNAdapterTest_Production.php
1111
.phpunit.result.cache
12-
1312
.DS_Store
13+
tests/ClientDI.php

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "@stable",
19-
"mockery/mockery": "^1.3",
2019
"league/flysystem-adapter-test-utilities": "^3",
2120
"league/flysystem-memory": "^3.0",
2221
"league/flysystem-path-prefixing": "^3.3",

src/BunnyCDNAdapter.php

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,20 @@ public function listContents(string $path, bool $deep): iterable
140140
*/
141141
protected function normalizeObject(array $bunny_file_array): StorageAttributes
142142
{
143+
$normalised_path = Util::normalizePath(
144+
Util::replaceFirst(
145+
$bunny_file_array['StorageZoneName'].'/',
146+
'/',
147+
$bunny_file_array['Path'].$bunny_file_array['ObjectName']
148+
)
149+
);
150+
143151
return match ($bunny_file_array['IsDirectory']) {
144152
true => new DirectoryAttributes(
145-
Util::normalizePath(
146-
$this->replaceFirst(
147-
$bunny_file_array['StorageZoneName'].'/',
148-
'/',
149-
$bunny_file_array['Path'].$bunny_file_array['ObjectName']
150-
)
151-
)
153+
$normalised_path
152154
),
153155
false => new FileAttributes(
154-
Util::normalizePath(
155-
$this->replaceFirst(
156-
$bunny_file_array['StorageZoneName'].'/',
157-
'/',
158-
$bunny_file_array['Path'].$bunny_file_array['ObjectName']
159-
)
160-
),
156+
$normalised_path,
161157
$bunny_file_array['Length'],
162158
Visibility::PUBLIC,
163159
self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
@@ -448,6 +444,16 @@ public function fileExists(string $path): bool
448444
return (bool) count($count);
449445
}
450446

447+
/**
448+
* @param string $path
449+
* @param Config $config
450+
* @return string
451+
*/
452+
public function checksum(string $path, Config $config): string
453+
{
454+
return $this->calculateChecksumFromStream($path, $config);
455+
}
456+
451457
/**
452458
* @deprecated use publicUrl instead
453459
*
@@ -479,20 +485,4 @@ private static function parse_bunny_timestamp(string $timestamp): int
479485
{
480486
return (date_create_from_format('Y-m-d\TH:i:s.u', $timestamp) ?: date_create_from_format('Y-m-d\TH:i:s', $timestamp))->getTimestamp();
481487
}
482-
483-
private function replaceFirst(string $search, string $replace, string $subject): string
484-
{
485-
$position = strpos($subject, $search);
486-
487-
if ($position !== false) {
488-
return (string) substr_replace($subject, $replace, $position, strlen($search));
489-
}
490-
491-
return $subject;
492-
}
493-
494-
public function checksum(string $path, Config $config): string
495-
{
496-
return $this->calculateChecksumFromStream($path, $config);
497-
}
498488
}

src/Util.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,15 @@ public static function endsWith($haystack, $needle): bool
7878

7979
return substr($haystack, -$length) === $needle;
8080
}
81+
82+
public static function replaceFirst(string $search, string $replace, string $subject): string
83+
{
84+
$position = strpos($subject, $search);
85+
86+
if ($position !== false) {
87+
return (string) substr_replace($subject, $replace, $position, strlen($search));
88+
}
89+
90+
return $subject;
91+
}
8192
}

tests/ClientDI_Example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// $storage_zone = 'testing_storage_zone';
4+
// $api_key = 'testing_api_key';

tests/ClientTest.php

Lines changed: 59 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace PlatformCommunity\Flysystem\BunnyCDN\Tests;
44

5-
use GuzzleHttp\Psr7\Response;
5+
use League\Flysystem\FilesystemException;
6+
use function PHPUnit\Framework\assertEmpty;
67
use PHPUnit\Framework\TestCase;
78
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
89
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\BunnyCDNException;
@@ -11,13 +12,46 @@
1112

1213
class ClientTest extends TestCase
1314
{
14-
const STORAGE_ZONE = 'example_storage_zone';
15+
const STORAGE_ZONE = 'test_storage_zone';
1516

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+
}
1730

1831
protected function setUp(): void
1932
{
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();
2155
}
2256

2357
/**
@@ -28,16 +62,9 @@ protected function setUp(): void
2862
*/
2963
public function test_listing_directory()
3064
{
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');
4168

4269
$response = $this->client->list('/');
4370

@@ -53,35 +80,27 @@ public function test_listing_directory()
5380
*/
5481
public function test_listing_subdirectory()
5582
{
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');
6585

86+
// Act
6687
$response = $this->client->list('/subfolder');
6788

89+
// Assert
6890
$this->assertIsArray($response);
6991
$this->assertCount(1, $response);
7092
}
7193

7294
/**
7395
* @return void
7496
*
75-
* @throws NotFoundException
7697
* @throws BunnyCDNException
98+
* @throws FilesystemException
99+
* @throws NotFoundException
77100
*/
78101
public function test_download_file()
79102
{
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');
85104

86105
$response = $this->client->download('/test.png');
87106

@@ -91,16 +110,13 @@ public function test_download_file()
91110
/**
92111
* @return void
93112
*
94-
* @throws NotFoundException
95113
* @throws BunnyCDNException
114+
* @throws FilesystemException
115+
* @throws NotFoundException
96116
*/
97117
public function test_streaming()
98118
{
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));
104120

105121
$stream = $this->client->stream('/test.png');
106122

@@ -120,18 +136,10 @@ public function test_streaming()
120136
*/
121137
public function test_upload()
122138
{
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-
132139
$response = $this->client->upload('/test_contents.txt', 'testing_contents');
133140

134141
$this->assertIsArray($response);
142+
135143
$this->assertEquals([
136144
'HttpCode' => 201,
137145
'Message' => 'File uploaded.',
@@ -145,15 +153,6 @@ public function test_upload()
145153
*/
146154
public function test_make_directory()
147155
{
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-
157156
$response = $this->client->make_directory('/test_dir/');
158157

159158
$this->assertIsArray($response);
@@ -166,22 +165,16 @@ public function test_make_directory()
166165
/**
167166
* @return void
168167
*
169-
* @throws NotFoundException
170168
* @throws BunnyCDNException
171169
* @throws DirectoryNotEmptyException
170+
* @throws FilesystemException
171+
* @throws NotFoundException
172172
*/
173173
public function test_delete_file()
174174
{
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');
183176

184-
$response = $this->client->delete('/testing.txt');
177+
$response = $this->client->delete('/test_file.txt');
185178

186179
$this->assertIsArray($response);
187180
$this->assertEquals([
@@ -193,68 +186,14 @@ public function test_delete_file()
193186
/**
194187
* @return void
195188
*
196-
* @throws NotFoundException
197189
* @throws BunnyCDNException
198190
* @throws DirectoryNotEmptyException
191+
* @throws NotFoundException
192+
* @throws FilesystemException
199193
*/
200194
public function test_delete_file_not_found()
201195
{
202196
$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');
214198
}
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-
// }
260199
}

0 commit comments

Comments
 (0)