Source: blog.deleu.dev

Laravel Fake S3 with Minio
Most of the time when I'm writing test code in Laravel I take advantage of the great Storage::fake() provided by Laravel Test Suite. Here's how simple it is for me to write test code with it.

My Test Code will look like this: $minio = new Minio(); $minio->disk('my-bucket', function (S3Client $client, string $bucket) { $this->post('/my/endpoint/that/interacts/with/s3', []) ->assertSuccessful(); $object = $client->getObject([ 'Bucket' => $bucket, 'Key' => "/my/expected/s3/key"]); $content = $object['Body']->getContents(); $this->assertStringContainsString('partial-file-content', $content); }); When instantiating the Minio class, I have the opportunity to overwrite some defaults.

$this->port; $client = new S3Client([ 'region' => 'local', 'version' => '2006-03-01', 'endpoint' => $endpoint, 'use_path_style_endpoint' => true, 'credentials' => [ 'key' => $this->key, 'secret' => $this->secret, ], ]); $bucket = "$disk-bucket"; $this->config->set("filesystems.disks.$disk", [ 'driver' => 's3', 'region' => 'local', 'bucket' => $bucket, 'endpoint' => $endpoint, 'use_path_style_endpoint' => true, 'key' => $this->key, 'secret' => $this->secret, ]); try { $client->createBucket(['Bucket' => $bucket]); } catch (Throwable $e) { } try { $callback($client, $bucket); } finally { $iterator = $client->getIterator('ListObjects', ['Bucket' => $bucket]); foreach ($iterator as $object) { $client->deleteObject([ 'Bucket' => $bucket, 'Key' => $object['Key']]); } $client->deleteBucket(['Bucket' => $bucket]); }} }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners