Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Vfsstream Laravel Package

mikey179/vfsstream

vfsStream provides a PHP stream wrapper for an in-memory virtual file system, ideal for unit tests. Mock files, directories, permissions, and content without touching disk, and use it with PHPUnit, SimpleTest, or any test framework.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require --dev mikey179/vfsstream. Start by replacing direct filesystem operations in your tests with vfsStream::url('root/some/file.txt') and set up your virtual filesystem using vfsStream::setup('root', $structure). The most common first use case: mocking file reads/writes in a service class during unit tests—e.g., test a config reader without touching real files.

Implementation Patterns

  • Use vfsStream::setup() at the beginning of a test (e.g., in setUp() or inline) to define directory/file hierarchy with optional content or custom file content types (vfsStreamContent, vfsStreamDirectory).
  • Use vfsStream::url('root') as the base stream URL for all simulated I/O. Replace file_get_contents('/path/to/config.yml') with file_get_contents(vfsStream::url('root/config.yml')).
  • For custom behavior, implement org\bovigo\vfs\content\Content or extend vfsStreamFile to simulate special file types (e.g., empty but writable file, or one that throws on write).
  • Combine with PHPUnit’s ::setUpBeforeClass() or ::assertFileEquals() to validate expected outputs.
  • For stream context simulation (e.g., stream_context_create()), set it via vfsStreamWrapper::setContext() before usage—especially useful for testing wrapper options or vfsStreamWrapper::register()/::unregister() in isolation tests.

Gotchas and Tips

  • vfsStream::path() (deprecated) and vfsStream::url() differ: url() returns the full stream wrapper URL (vfs://root/file.txt), while path() returns a normalized path relative to root—but avoid path() as it's deprecated.
  • File/directory names in structure arrays must be scalar strings; using integers (e.g., 0) as keys or names requires care—v1.6.3+ fixes some edge cases here.
  • PHP 8.4 requires explicit nullable type hints; ensure your custom content implementations match exact signatures (write() must return int|false, not just int).
  • Avoid relying on hard-coded php://stream aliases—use vfsStreamWrapper::register()/::unregister() explicitly when testing stream wrapper registrations.
  • When mocking real-world scenarios like fopen() with 'w+', ensure the target file or parent directory exists (create via vfsStream::create() or full structure), as vfsStream emulates real filesystem path existence checks.
  • Debug tip: enable verbose output in tests with var_dump(vfsStream::root()->children()) to inspect the virtual structure after operations.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4