PHPackages                             christeredvartsen/testfs - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. christeredvartsen/testfs

ActiveLibrary[Testing &amp; Quality](/categories/testing)

christeredvartsen/testfs
========================

Virtual filesystem that can be used for testing

v0.8.0(3mo ago)13.6k[1 issues](https://github.com/christeredvartsen/testfs/issues)1MITPHPPHP &gt;=8.3CI passing

Since Jan 22Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/christeredvartsen/testfs)[ Packagist](https://packagist.org/packages/christeredvartsen/testfs)[ Docs](https://github.com/christeredvartsen/testfs/)[ RSS](/packages/christeredvartsen-testfs/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (7)Versions (13)Used By (1)

TestFs
======

[](#testfs)

Virtual filesystem for PHP for use with testing, implemented using a [stream wrapper](https://www.php.net/manual/en/class.streamwrapper.php).

Requirements
------------

[](#requirements)

This library requires PHP &gt;= 8.3.

Installation
------------

[](#installation)

Install using [Composer](https://getcomposer.org):

```
composer require --dev christeredvartsen/testfs

```

Usage
-----

[](#usage)

### Enable the stream wrapper

[](#enable-the-stream-wrapper)

The stream wrapper is enabled once you register it:

```
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

[](#mirror-a-local-directory-into-the-virtual-filesystem)

If you want to mirror a local directory into the virtual filesystem you can do this:

```
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

[](#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:

```
