|
| 1 | +# Laravel Storage Virtual File System Adapter |
| 2 | +Sometimes, when we are testing, it would be very nice to just easily swap out the various `Storage:disk()` calls with a |
| 3 | +virtual files system like `mikey179/vfsStream`. |
| 4 | + |
| 5 | +## Requirements |
| 6 | +This package has been tested against Laravel/Lumen versions 5.4. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +``` |
| 11 | +composer require stechstudio/laravel-vfs-adapter |
| 12 | +``` |
| 13 | +Then register the provider in your `App\Providers\AppServiceProvider` like so: |
| 14 | +```php |
| 15 | +public function register() |
| 16 | +{ |
| 17 | + if ($this->app->environment() === 'testing') { |
| 18 | + if (class_exists(VfsFilesystemServiceProvider::class)) { |
| 19 | + $this->app->register(VfsFilesystemServiceProvider::class); |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | +This assumes you stick with the default `APP_ENV=testing` settings that Laravel comes with out of the box. Of course, |
| 25 | +you may also register the provider in the more traditional manner if you want the virtual file system adapter available |
| 26 | +in other environments, of it you just don't like the way we do it. |
| 27 | + |
| 28 | +## Configuration |
| 29 | +All configuration can be done in a combination of your `.env`, `phpunit.xml`, and `config/filesystems.php`. |
| 30 | + |
| 31 | +Suppose you had a `config/filesystems.php` that looked something like this: |
| 32 | + |
| 33 | +```php |
| 34 | +return [ |
| 35 | + .... |
| 36 | + |
| 37 | + 'disks' => [ |
| 38 | + 'data' => [ |
| 39 | + 'driver' => 'local', |
| 40 | + 'root' => env('STORAGE_DATA_DIR'), |
| 41 | + ], |
| 42 | + |
| 43 | + 'archive' => [ |
| 44 | + 'driver' => 's3' |
| 45 | + 'key' => env('S3_KEY'), |
| 46 | + 'secret' => env('S3_SECRET'), |
| 47 | + 'region' => env('S3_REGION'), |
| 48 | + 'bucket' => env('S3_DEVELOP_BUCKET') |
| 49 | + ] |
| 50 | + ], |
| 51 | +]; |
| 52 | +``` |
| 53 | +Simply make a few modifications: |
| 54 | +```php |
| 55 | +return [ |
| 56 | + .... |
| 57 | + |
| 58 | + 'disks' => [ |
| 59 | + |
| 60 | + 'data' => [ |
| 61 | + 'driver' => env('STORAGE_DATA_DRIVER', 'local'), |
| 62 | + 'root' => env('STORAGE_DATA_DIR'), |
| 63 | + 'dir_name' => 'data' |
| 64 | + ], |
| 65 | + |
| 66 | + 'archive' => [ |
| 67 | + 'driver' => env('S3_ARCHIVE_DRIVER', 's3') |
| 68 | + 'key' => env('S3_KEY'), |
| 69 | + 'secret' => env('S3_SECRET'), |
| 70 | + 'region' => env('S3_REGION'), |
| 71 | + 'bucket' => env('S3_DEVELOP_BUCKET'), |
| 72 | + 'dir_name' => 'archive' |
| 73 | + ] |
| 74 | + ], |
| 75 | +]; |
| 76 | + |
| 77 | +``` |
| 78 | +The `dir_name` determines the root directory of the vfsStream for that particular `Storage::disk()`, I like to name it |
| 79 | +something relative to the actual `disk` name. Setting up the driver with `env()` allows us to default to our standard drivers |
| 80 | +or allow us to override that in `phpunit.xml` to switch over to the virtual filesystem driver. |
| 81 | + |
| 82 | +Now, in your `phpunit.xml` add: |
| 83 | + |
| 84 | +```xml |
| 85 | +<env name="STORAGE_DATA_DRIVER" value="vfs"/> |
| 86 | +<env name="S3_ARCHIVE_DRIVER" value="vfs"/> |
| 87 | +``` |
| 88 | +That is all there is to it, those drives will now use the virtual filesystem adapter. |
| 89 | + |
0 commit comments