league/flysystem-adapter-test-utilities
Helper utilities for testing Flysystem adapters. Add as a dev dependency to reuse common adapter test cases and assertions while developing or verifying custom filesystem adapters. Part of the Flysystem project; see main docs and repo for issues/PRs.
Installation Add the package via Composer:
composer require --dev league/flysystem-adapter-test-utilities
Basic Usage
Import the League\Flysystem\Adapter\TestUtil\TestAdapter class and use it to mock a filesystem adapter for testing:
use League\Flysystem\Adapter\TestUtil\TestAdapter;
$testAdapter = new TestAdapter();
$filesystem = new League\Flysystem\Filesystem($testAdapter);
First Use Case Test file operations (e.g., upload, read, delete) without hitting a real storage system:
$filesystem->write('test.txt', 'Hello, Laravel!');
$contents = $filesystem->read('test.txt');
$this->assertEquals('Hello, Laravel!', $contents);
TestAdapter and TestFilesystem classes in the package.Storage facade can use custom adapters (e.g., Filesystem::adapter()).Unit Testing Adapters Mock a filesystem adapter for isolated tests:
public function testFileUpload()
{
$adapter = new TestAdapter();
$filesystem = new Filesystem($adapter);
$filesystem->write('file.txt', 'content');
$this->assertTrue($adapter->has('file.txt'));
}
Integration with Laravel Storage
Replace Laravel’s default adapter with TestAdapter in tests:
use Illuminate\Support\Facades\Storage;
public function testCustomAdapter()
{
Storage::extend('test', function () {
return new Filesystem(new TestAdapter());
});
Storage::disk('test')->put('file.txt', 'content');
$this->assertTrue(Storage::disk('test')->exists('file.txt'));
}
Customizing Test Data Pre-populate the adapter with test files:
$adapter = new TestAdapter();
$adapter->addFile('existing.txt', 'pre-existing content');
Storage::fake() for Laravel’s built-in fake storage, but TestAdapter is useful for custom adapters.TestAdapter to simulate edge cases (e.g., read-only mode):
class ReadOnlyTestAdapter extends TestAdapter
{
public function write($path, $contents, Config $config)
{
throw new RuntimeException('Read-only mode');
}
}
assertDatabaseHas equivalents for filesystem checks.State Management
TestAdapter retains state between tests. Reset it or use a fresh instance per test:
$adapter = new TestAdapter(); // Fresh instance per test
Laravel’s Fake Storage
Avoid mixing Storage::fake() with TestAdapter—they serve different purposes. Use one or the other.
Visibility of Test Files
Files added via addFile() are not visible to Laravel’s Storage facade unless explicitly configured.
$adapter->listContents() to debug file structure.$adapter->clear() to wipe all test data between tests.Custom Adapters
Extend TestAdapter to simulate specific behaviors (e.g., slow responses, quota limits):
class SlowTestAdapter extends TestAdapter
{
public function read($path, Config $config)
{
sleep(2); // Simulate latency
return parent::read($path, $config);
}
}
Mocking Exceptions Override methods to throw exceptions for error-case testing:
public function delete($path)
{
throw new RuntimeException('Permission denied');
}
Laravel Service Providers
Dynamically swap adapters in tests using Laravel’s Storage::extend():
Storage::extend('test', function () {
return new Filesystem(new TestAdapter());
});
write() accept a Config object (e.g., visibility, timestamp). Pass null for defaults:
$adapter->write('file.txt', 'content', null);
How can I help you explore Laravel packages today?