Skip to content

Commit 05d39c4

Browse files
authored
Merge pull request #49 from tinect/updateTests
feat: add ability to run more tests onto live-environment + support moving folder
2 parents 19e2f84 + 141887d commit 05d39c4

File tree

5 files changed

+218
-30
lines changed

5 files changed

+218
-30
lines changed

src/BunnyCDNAdapter.php

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,17 @@ public function __construct(BunnyCDNClient $client, string $pullzone_url = '')
6868
public function copy($source, $destination, Config $config): void
6969
{
7070
try {
71-
$this->write($destination, $this->read($source), new Config());
72-
// @codeCoverageIgnoreStart
71+
/** @var array<string> $files */
72+
$files = iterator_to_array($this->getFiles($source));
73+
74+
$sourceLength = \strlen($source);
75+
76+
foreach ($files as $file) {
77+
$this->copyFile($file, $destination.\substr($file, $sourceLength), $config);
78+
}
7379
} catch (UnableToReadFile|UnableToWriteFile $exception) {
7480
throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
7581
}
76-
// @codeCoverageIgnoreEnd
7782
}
7883

7984
/**
@@ -394,13 +399,50 @@ public function fileSize(string $path): FileAttributes
394399
public function move(string $source, string $destination, Config $config): void
395400
{
396401
try {
397-
$this->write($destination, $this->read($source), new Config());
398-
$this->delete($source);
402+
/** @var array<string> $files */
403+
$files = iterator_to_array($this->getFiles($source));
404+
405+
$sourceLength = \strlen($source);
406+
407+
foreach ($files as $file) {
408+
$this->moveFile($file, $destination.\substr($file, $sourceLength), $config);
409+
}
399410
} catch (UnableToReadFile $e) {
400411
throw new UnableToMoveFile($e->getMessage());
401412
}
402413
}
403414

415+
private function getFiles(string $source): iterable
416+
{
417+
$contents = iterator_to_array($this->listContents($source, true));
418+
419+
if (\count($contents) === 0) {
420+
yield $source;
421+
422+
return;
423+
}
424+
425+
/** @var StorageAttributes $entry */
426+
foreach ($contents as $entry) {
427+
if ($entry->isFile() === false) {
428+
continue;
429+
}
430+
431+
yield $entry->path();
432+
}
433+
}
434+
435+
private function moveFile(string $source, string $destination, Config $config): void
436+
{
437+
$this->copyFile($source, $destination, $config);
438+
$this->delete($source);
439+
}
440+
441+
private function copyFile(string $source, string $destination, Config $config): void
442+
{
443+
$this->write($destination, $this->read($source), $config);
444+
}
445+
404446
/**
405447
* @param $path
406448
* @return void

tests/ClientDI_Example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php
22

3+
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;
4+
35
// $storage_zone = 'testing_storage_zone';
46
// $api_key = 'testing_api_key';
7+
// $region = BunnyCDNRegion::DEFAULT;
8+
// $public_url = '';

tests/ClientTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
class ClientTest extends TestCase
1414
{
15-
const STORAGE_ZONE = 'test_storage_zone';
16-
1715
public BunnyCDNClient $client;
1816

1917
private static function bunnyCDNClient(): BunnyCDNClient
@@ -24,9 +22,9 @@ private static function bunnyCDNClient(): BunnyCDNClient
2422

2523
if ($storage_zone !== null && $api_key !== null) {
2624
return new BunnyCDNClient($storage_zone, $api_key, $region ?? BunnyCDNRegion::DEFAULT);
27-
} else {
28-
return new MockClient(self::STORAGE_ZONE, '123');
2925
}
26+
27+
return new MockClient('test_storage_zone', '123');
3028
}
3129

3230
protected function setUp(): void

tests/FlysystemTest.php

Lines changed: 136 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use League\Flysystem\Filesystem;
88
use League\Flysystem\FilesystemAdapter;
99
use League\Flysystem\FilesystemException;
10+
use League\Flysystem\UnableToCopyFile;
11+
use League\Flysystem\UnableToMoveFile;
1012
use League\Flysystem\UnableToRetrieveMetadata;
1113
use League\Flysystem\Visibility;
1214
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
@@ -16,10 +18,21 @@
1618

1719
class FlysystemTest extends FilesystemAdapterTestCase
1820
{
19-
/**
20-
* Storage Zone
21-
*/
22-
const STORAGE_ZONE = 'test_storage_zone';
21+
public const DEMOURL = 'https://example.org.local';
22+
23+
protected static bool $isLive = false;
24+
25+
protected static string $publicUrl = self::DEMOURL;
26+
27+
public static function setUpBeforeClass(): void
28+
{
29+
global $public_url;
30+
if (isset($public_url)) {
31+
static::$publicUrl = $public_url;
32+
}
33+
34+
static::$publicUrl = rtrim(static::$publicUrl, '/');
35+
}
2336

2437
private static function bunnyCDNClient(): BunnyCDNClient
2538
{
@@ -28,15 +41,17 @@ private static function bunnyCDNClient(): BunnyCDNClient
2841
global $region;
2942

3043
if ($storage_zone !== null && $api_key !== null) {
44+
static::$isLive = true;
45+
3146
return new BunnyCDNClient($storage_zone, $api_key, $region ?? BunnyCDNRegion::DEFAULT);
32-
} else {
33-
return new MockClient(self::STORAGE_ZONE, '123');
3447
}
48+
49+
return new MockClient('test_storage_zone', '123');
3550
}
3651

3752
public static function createFilesystemAdapter(): FilesystemAdapter
3853
{
39-
return new BunnyCDNAdapter(self::bunnyCDNClient(), 'https://example.org.local/assets/');
54+
return new BunnyCDNAdapter(self::bunnyCDNClient(), static::$publicUrl);
4055
}
4156

4257
/**
@@ -52,22 +67,132 @@ public function generating_a_temporary_url(): void
5267
$this->markTestSkipped('No temporary URL support is provided for BunnyCDN');
5368
}
5469

70+
/**
71+
* @test
72+
*/
73+
public function moving_a_folder(): void
74+
{
75+
$this->runScenario(function () {
76+
$adapter = $this->adapter();
77+
$adapter->write(
78+
'test/text.txt',
79+
'contents to be copied',
80+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
81+
);
82+
$adapter->write(
83+
'test/2/text.txt',
84+
'contents to be copied',
85+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
86+
);
87+
$adapter->move('test', 'destination', new Config());
88+
$this->assertFalse(
89+
$adapter->fileExists('test/text.txt'),
90+
'After moving a file should no longer exist in the original location.'
91+
);
92+
$this->assertFalse(
93+
$adapter->fileExists('test/2/text.txt'),
94+
'After moving a file should no longer exist in the original location.'
95+
);
96+
$this->assertTrue(
97+
$adapter->fileExists('destination/text.txt'),
98+
'After moving, a file should be present at the new location.'
99+
);
100+
$this->assertTrue(
101+
$adapter->fileExists('destination/2/text.txt'),
102+
'After moving, a file should be present at the new location.'
103+
);
104+
$this->assertEquals('contents to be copied', $adapter->read('destination/text.txt'));
105+
$this->assertEquals('contents to be copied', $adapter->read('destination/2/text.txt'));
106+
});
107+
}
108+
109+
/**
110+
* @test
111+
*/
112+
public function moving_a_not_existing_folder(): void
113+
{
114+
$this->runScenario(function () {
115+
$adapter = $this->adapter();
116+
117+
$this->expectException(UnableToMoveFile::class);
118+
$adapter->move('not_existing_file', 'destination', new Config());
119+
});
120+
}
121+
122+
/**
123+
* @test
124+
*/
125+
public function copying_a_folder(): void
126+
{
127+
$this->runScenario(function () {
128+
$adapter = $this->adapter();
129+
$adapter->write(
130+
'test/text.txt',
131+
'contents to be copied',
132+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
133+
);
134+
$adapter->write(
135+
'test/2/text.txt',
136+
'contents to be copied',
137+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
138+
);
139+
$adapter->copy('test', 'destination', new Config());
140+
$this->assertTrue(
141+
$adapter->fileExists('test/text.txt'),
142+
'After copying a file should exist in the original location.'
143+
);
144+
$this->assertTrue(
145+
$adapter->fileExists('test/2/text.txt'),
146+
'After copying a file should exist in the original location.'
147+
);
148+
$this->assertTrue(
149+
$adapter->fileExists('destination/text.txt'),
150+
'After copying, a file should be present at the new location.'
151+
);
152+
$this->assertTrue(
153+
$adapter->fileExists('destination/2/text.txt'),
154+
'After copying, a file should be present at the new location.'
155+
);
156+
$this->assertEquals('contents to be copied', $adapter->read('destination/text.txt'));
157+
$this->assertEquals('contents to be copied', $adapter->read('destination/2/text.txt'));
158+
});
159+
}
160+
161+
/**
162+
* @test
163+
*/
164+
public function copying_a_not_existing_folder(): void
165+
{
166+
$this->runScenario(function () {
167+
$adapter = $this->adapter();
168+
169+
$this->expectException(UnableToCopyFile::class);
170+
$adapter->copy('not_existing_file', 'destination', new Config());
171+
});
172+
}
173+
55174
/**
56175
* We overwrite the test, because the original tries accessing the url
57176
*
58177
* @test
59178
*/
60179
public function generating_a_public_url(): void
61180
{
181+
if (self::$isLive && ! \str_starts_with(static::$publicUrl, self::DEMOURL)) {
182+
parent::generating_a_public_url();
183+
184+
return;
185+
}
186+
62187
$url = $this->adapter()->publicUrl('/path.txt', new Config());
63188

64-
self::assertEquals('https://example.org.local/assets/path.txt', $url);
189+
self::assertEquals(static::$publicUrl.'/path.txt', $url);
65190
}
66191

67192
public function test_without_pullzone_url_error_thrown_accessing_url(): void
68193
{
69-
self::expectException(\RuntimeException::class);
70-
self::expectExceptionMessage('In order to get a visible URL for a BunnyCDN object, you must pass the "pullzone_url" parameter to the BunnyCDNAdapter.');
194+
$this->expectException(\RuntimeException::class);
195+
$this->expectExceptionMessage('In order to get a visible URL for a BunnyCDN object, you must pass the "pullzone_url" parameter to the BunnyCDNAdapter.');
71196
$myAdapter = new BunnyCDNAdapter(static::bunnyCDNClient());
72197
$myAdapter->publicUrl('/path.txt', new Config());
73198
}
@@ -144,7 +269,7 @@ public function test_regression_pr_20()
144269
*/
145270
public function test_regression_issue_29()
146271
{
147-
$client = new MockClient(self::STORAGE_ZONE, 'api-key');
272+
$client = self::bunnyCDNClient();
148273
$client->make_directory('/example_folder');
149274

150275
$adapter = new Filesystem(new BunnyCDNAdapter($client));

tests/PrefixTest.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616

1717
class PrefixTest extends FilesystemAdapterTestCase
1818
{
19-
/**
20-
* Storage Zone
21-
*/
22-
const STORAGE_ZONE = 'test_storage_zone';
23-
2419
/**
2520
* Path Prefix
2621
*/
27-
const PREFIX_PATH = 'path_prefix_12345';
22+
public const PREFIX_PATH = 'path_prefix_12345';
2823

2924
private static function bunnyCDNClient(): BunnyCDNClient
3025
{
@@ -33,9 +28,9 @@ private static function bunnyCDNClient(): BunnyCDNClient
3328

3429
if ($storage_zone !== null && $api_key !== null) {
3530
return new BunnyCDNClient($storage_zone, $api_key);
36-
} else {
37-
return new MockClient(self::STORAGE_ZONE, '123');
3831
}
32+
33+
return new MockClient('test_storage_zone', '123');
3934
}
4035

4136
private static function bunnyCDNAdapter(): BunnyCDNAdapter
@@ -125,8 +120,8 @@ public function get_checksum(): void
125120

126121
public function test_construct_throws_error(): void
127122
{
128-
self::expectException(\RuntimeException::class);
129-
self::expectExceptionMessage('PrefixPath is no longer supported directly. Use PathPrefixedAdapter instead: https://flysystem.thephpleague.com/docs/adapter/path-prefixing/');
123+
$this->expectException(\RuntimeException::class);
124+
$this->expectExceptionMessage('PrefixPath is no longer supported directly. Use PathPrefixedAdapter instead: https://flysystem.thephpleague.com/docs/adapter/path-prefixing/');
130125
new BunnyCDNAdapter(self::bunnyCDNClient(), 'https://example.org.local/assets/', 'thisisauselessarg');
131126
}
132127

@@ -235,6 +230,30 @@ public function prefix_path(): void
235230
self::assertFalse($prefixPathAdapter->fileExists(
236231
'source.file.svg'
237232
));
233+
234+
$prefixPathAdapter->write(
235+
'subfolder/subfolder2/source.file.svg',
236+
$content,
237+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
238+
);
239+
240+
self::assertTrue($regularAdapter->fileExists(
241+
self::PREFIX_PATH.'/subfolder/subfolder2/source.file.svg'
242+
));
243+
244+
$prefixPathAdapter->move(
245+
'subfolder',
246+
'newsubfolder',
247+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
248+
);
249+
250+
self::assertFalse($regularAdapter->fileExists(
251+
self::PREFIX_PATH.'/subfolder/subfolder2/source.file.svg'
252+
));
253+
254+
self::assertTrue($prefixPathAdapter->fileExists(
255+
'newsubfolder/subfolder2/source.file.svg'
256+
));
238257
});
239258
}
240259

0 commit comments

Comments
 (0)