Skip to content

Instantly share code, notes, and snippets.

@odan
Last active July 2, 2020 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odan/0da42b9b58d5cb96acaf43488ca4a2f6 to your computer and use it in GitHub Desktop.
Save odan/0da42b9b58d5cb96acaf43488ca4a2f6 to your computer and use it in GitHub Desktop.
Creating a mocked local filesystem adapter with vfsStream

Creating a mocked local Flysystem adapter with vfsStream

Note: In this example I doesn't use the flysystem Memory or Null adpater, because the Memory adapter doesn't implement the getPathPrefix method, like the AbstractAdapter does. For this reason I use the Local adapter in LOCK_NB mode in combination with vfsStream to simulate the filesystem in-memory.

Setup

composer require mikey179/vfsstream --dev

Mocking the Local adapter

Add this method to your phpunit test trait:

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Adapter\Local;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;

// ...

protected function mockFilesystem(): Filesystem
{
    $root = vfsStream::setup();
    $rootPath = $root->url();

    // Must not be LOCK_EX, because LOCK_EX is not supported by vfsstream.
    // We are running tests in-memory, so LOCK_EX is not important here.
    $adapter = new Local($rootPath, LOCK_NB, FileStorageAdapter::DISALLOW_LINKS, [
        'file' => [
            'public' => 0777,
            'private' => 0777,
        ],
        'dir' => [
            'public' => 0777,
            'private' => 0777,
        ],
    ]);

    return new Filesystem($adapter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment