https://github.com/christeredvartsen/testfs
Virtual filesystem for PHP that can be used for testing
https://github.com/christeredvartsen/testfs
php php-library stream-wrapper testfs unit-test
Last synced: 4 months ago
JSON representation
Virtual filesystem for PHP that can be used for testing
- Host: GitHub
- URL: https://github.com/christeredvartsen/testfs
- Owner: christeredvartsen
- License: mit
- Created: 2020-01-21T19:24:46.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2026-01-02T17:46:52.000Z (5 months ago)
- Last Synced: 2026-01-08T22:00:04.895Z (5 months ago)
- Topics: php, php-library, stream-wrapper, testfs, unit-test
- Language: PHP
- Size: 224 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# TestFs
Virtual filesystem for PHP for use with testing, implemented using a [stream wrapper](https://www.php.net/manual/en/class.streamwrapper.php).
## Requirements
This library requires PHP >= 8.3.
## Installation
Install using [Composer](https://getcomposer.org):
```
composer require --dev christeredvartsen/testfs
```
## Usage
### Enable the stream wrapper
The stream wrapper is enabled once you register it:
```php
require 'vendor/autoload.php';
use TestFs\StreamWrapper;
StreamWrapper::register();
```
When it is registered it will pick up usage of the `tfs://` protocol used with filesystem functions, for instance `fopen()`, `file_get_contents()` and `mkdir()`.
### Mirror a local directory into the virtual filesystem
If you want to mirror a local directory into the virtual filesystem you can do this:
```php
require 'vendor/autoload.php';
use TestFs\StreamWrapper;
StreamWrapper::register();
$device = StreamWrapper::getDevice();
$device->buildFromDirectory('./src');
echo $device->tree() . PHP_EOL;
```
If the above code would be executed from a PHP file in the root of this project you would get something like this:
```
/
├── Asset.php
├── Device.php
├── Directory.php
├── Exception
│ ├── DuplicateAssetException.php
│ ├── DuplicateGroupException.php
│ ├── DuplicateUserException.php
│ ├── InsufficientStorageException.php
│ ├── InvalidAssetNameException.php
│ ├── InvalidFopenModeException.php
│ ├── InvalidUrlException.php
│ ├── InvalidWhenceException.php
│ ├── ProtocolAlreadyRegisteredException.php
│ ├── RuntimeException.php
│ ├── UnknownAssetException.php
│ ├── UnknownGroupException.php
│ └── UnknownUserException.php
├── File.php
├── FopenMode.php
├── RootDirectory.php
├── StreamWrapper.php
└── functions.php
1 directory, 21 files
```
### Converting paths to URLs
To convert regular file paths to URLs that will be picked up by TestFs you can use the `TestFs::url(string $path)` function:
```php