support S3 compatible storage providers (#401)

This commit is contained in:
Saeed Vaziry
2024-12-22 23:06:36 +01:00
committed by GitHub
parent db81583884
commit ea3d64607a
20 changed files with 127 additions and 576 deletions

View File

@ -20,6 +20,7 @@ public function test_s3_connect_successful()
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::S3,
'credentials' => [
'api_url' => 'https://fake-bucket.s3.us-east-1.s3-compatible.com',
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',

View File

@ -1,104 +0,0 @@
<?php
namespace Tests\Unit\StorageProviders;
use App\Enums\StorageProvider;
use App\Models\StorageProvider as StorageProviderModel;
use App\StorageProviders\S3;
use App\StorageProviders\Wasabi;
use Aws\Command;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class WasabiTest extends TestCase
{
use RefreshDatabase;
public function test_wasabi_connect_successful()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::WASABI,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client (Wasabi uses S3-compatible API)
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new Command('listBuckets'));
// Mock the execute method
$s3ClientMock->expects($this->once())
->method('execute')
->willReturn(['Buckets' => []]);
// Mock the Wasabi class to return the mocked S3Client
$wasabi = $this->getMockBuilder(Wasabi::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$wasabi->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertTrue($wasabi->connect());
}
public function test_wasabi_connect_failure()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::WASABI,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client (Wasabi uses S3-compatible API)
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new Command('listBuckets'));
// Mock the execute method to throw an S3Exception
$s3ClientMock->expects($this->once())
->method('execute')
->willThrowException(new S3Exception('Error', new Command('ListBuckets')));
// Mock the Wasabi class to return the mocked S3Client
$wasabi = $this->getMockBuilder(Wasabi::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$wasabi->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertFalse($wasabi->connect());
}
}