Skip to content

Commit

Permalink
feat: add testcase for writeBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Oct 23, 2024
1 parent cd1a5a2 commit 271056d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/BunnyCDNAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ public function writeBatch(array $writeBatches, Config $config): void
}
};

$pool = new Pool($this->client->client, $requests(), [
$pool = new Pool($this->client->guzzleClient, $requests(), [
'concurrency' => $concurrency,
'rejected' => function (RequestException $reason, int $index) {
'rejected' => function (RequestException|RuntimeException $reason, int $index) {
throw UnableToWriteFile::atLocation($index, $reason->getMessage());
},
]);
Expand Down
8 changes: 4 additions & 4 deletions src/BunnyCDNClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

class BunnyCDNClient
{
public Guzzle $client;
public Guzzle $guzzleClient;

public function __construct(
public string $storage_zone_name,
private string $api_key,
private string $region = BunnyCDNRegion::FALKENSTEIN
) {
$this->client = new Guzzle();
$this->guzzleClient = new Guzzle();
}

private static function get_base_url($region): string
Expand Down Expand Up @@ -54,7 +54,7 @@ public function createRequest(string $path, string $method = 'GET', array $heade
*/
private function request(Request $request, array $options = []): mixed
{
$contents = $this->client->send($request, $options)->getBody()->getContents();
$contents = $this->guzzleClient->send($request, $options)->getBody()->getContents();

return json_decode($contents, true) ?? $contents;
}
Expand Down Expand Up @@ -125,7 +125,7 @@ public function download(string $path): string
public function stream(string $path)
{
try {
return $this->client->send($this->createRequest($path), ['stream' => true])->getBody()->detach();
return $this->guzzleClient->send($this->createRequest($path), ['stream' => true])->getBody()->detach();
// @codeCoverageIgnoreStart
} catch (GuzzleException $e) {
throw match ($e->getCode()) {
Expand Down
96 changes: 94 additions & 2 deletions tests/FlysystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace PlatformCommunity\Flysystem\BunnyCDN\Tests;

use Faker\Factory;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
use League\Flysystem\Config;
use League\Flysystem\Filesystem;
Expand All @@ -13,11 +16,13 @@
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToProvideChecksum;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\Visibility;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;
use PlatformCommunity\Flysystem\BunnyCDN\Util;
use PlatformCommunity\Flysystem\BunnyCDN\WriteBatchFile;
use Throwable;

if (\is_file(__DIR__.'/ClientDI.php')) {
Expand Down Expand Up @@ -54,7 +59,34 @@ private static function bunnyCDNClient(): BunnyCDNClient
return new BunnyCDNClient($storage_zone, $api_key, $region ?? BunnyCDNRegion::DEFAULT);
}

return new MockClient('test_storage_zone', '123');
$mockedClient = new MockClient('test_storage_zone', '123');

$mockedClient->guzzleClient = new Guzzle([
'handler' => function (Request $request) use ($mockedClient) {
$path = $request->getUri()->getPath();
$method = $request->getMethod();

if ($method === 'PUT' && $path === 'destination.txt') {
$mockedClient->filesystem->write('destination.txt', 'text');

return new Response(200);
}

if ($method === 'PUT' && $path === 'destination2.txt') {
$mockedClient->filesystem->write('destination2.txt', 'text2');

return new Response(200);
}

if ($method === 'PUT' && \in_array($path, ['failing.txt', 'failing2.txt'])) {
throw new \RuntimeException('Failed to write file');
}

throw new \RuntimeException('Unexpected request: '.$method.' '.$path);
},
]);

return $mockedClient;
}

public static function createFilesystemAdapter(): FilesystemAdapter
Expand Down Expand Up @@ -474,11 +506,71 @@ public function test_regression_issue_39()
$this->runScenario(function () {
$adapter = $this->adapter();

$this->adapter()->write('test.json', json_encode(['test' => 123]), new Config([]));
$adapter->write('test.json', json_encode(['test' => 123]), new Config([]));

$response = $adapter->read('/test.json');

$this->assertIsString($response);
});
}

public function test_write_batch(): void
{
$this->runScenario(function () {
$firstTmpFile = \tmpfile();
fwrite($firstTmpFile, 'text');
$firstTmpPath = stream_get_meta_data($firstTmpFile)['uri'];

$secondTmpFile = \tmpfile();
fwrite($secondTmpFile, 'text2');
$secondTmpPath = stream_get_meta_data($secondTmpFile)['uri'];

$adapter = $this->adapter();

$adapter->writeBatch(
[
new WriteBatchFile($firstTmpPath, 'destination.txt'),
new WriteBatchFile($secondTmpPath, 'destination2.txt'),
],
new Config()
);

\fclose($firstTmpFile);
\fclose($secondTmpFile);

$this->assertSame('text', $adapter->read('destination.txt'));
$this->assertSame('text2', $adapter->read('destination2.txt'));
});
}

public function test_failing_write_batch(): void
{
if (self::$isLive) {
$this->markTestSkipped('This test is not applicable in live mode');
}

$this->runScenario(function () {
$firstTmpFile = \tmpfile();
fwrite($firstTmpFile, 'text');
$firstTmpPath = stream_get_meta_data($firstTmpFile)['uri'];

$secondTmpFile = \tmpfile();
fwrite($secondTmpFile, 'text2');
$secondTmpPath = stream_get_meta_data($firstTmpFile)['uri'];

$adapter = $this->adapter();

$this->expectException(UnableToWriteFile::class);
$adapter->writeBatch(
[
new WriteBatchFile($firstTmpPath, 'failing.txt'),
new WriteBatchFile($secondTmpPath, 'failing2.txt'),
],
new Config()
);

\fclose($firstTmpFile);
\fclose($secondTmpFile);
});
}
}
25 changes: 20 additions & 5 deletions tests/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PlatformCommunity\Flysystem\BunnyCDN\Tests;

use Faker\Factory;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Psr7\Request;
use League\Flysystem\FileAttributes;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
Expand All @@ -20,6 +22,8 @@ class MockClient extends BunnyCDNClient
*/
public Filesystem $filesystem;

public Guzzle $guzzleClient;

public function __construct(string $storage_zone_name, string $api_key, string $region = '')
{
parent::__construct($storage_zone_name, $api_key, $region);
Expand All @@ -41,8 +45,10 @@ public function list(string $path): array
'Checksum' => hash('sha256', $this->filesystem->read($file->path())),
]);
})->toArray();
} catch (FilesystemException $exception) {
} catch (FilesystemException) {
}

return [];
}

/**
Expand Down Expand Up @@ -81,8 +87,10 @@ public function upload(string $path, $contents): array
'HttpCode' => 201,
'Message' => 'File uploaded.',
];
} catch (FilesystemException $exception) {
} catch (FilesystemException) {
}

return [];
}

/**
Expand All @@ -98,8 +106,10 @@ public function make_directory(string $path): array
'HttpCode' => 201,
'Message' => 'Directory created.',
];
} catch (FilesystemException $exception) {
} catch (FilesystemException) {
}

return [];
}

/**
Expand All @@ -121,16 +131,21 @@ public function delete(string $path): array
'HttpCode' => 200,
'Message' => 'File deleted successfuly.', // ಠ_ಠ Spelling @bunny.net
];
} catch (NotFoundException $e) {
} catch (NotFoundException) {
throw new NotFoundException('404');
} catch (\Exception $e) {
} catch (\Exception) {
return [
'HttpCode' => 404,
'Message' => 'File deleted successfuly.', // ಠ_ಠ Spelling @bunny.net
];
}
}

public function getUploadRequest(string $path, $contents): Request
{
return new Request('PUT', $path, [], $contents);
}

private static function example_file($path = '/directory/test.png', $storage_zone = 'storage_zone', $override = []): array
{
['file' => $file, 'dir' => $dir] = Util::splitPathIntoDirectoryAndFile($path);
Expand Down

0 comments on commit 271056d

Please sign in to comment.