vfsStream and File System

Paul M Jones wrote earlier When Possible, Use File *Resources* Instead Of File *Names* and in comments Chris Hartjes was mentioning about vfsStream. So for @auraphp we thought of making to support vfsStream. What the hell it is? vfsStream is a PHP stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest. So without touching the file system and not bothering too much about the operating system, file permissions, disk quota, removing the files created by tests on tear down etc we can make use of vfsstream. Lets start Go and get it via composer.

1
2
3
4
5
{
    "require": {
        "mikey179/vfsStream": "1.1.0"
        }
    }

Run your composer.phar update.

1
2
3
4
5
6
require 'vendor/autoload.php';
use org\\bovigo\\vfs\\vfsStream;
use org\\bovigo\\vfs\\visitor\\vfsStreamStructureVisitor;
use org\\bovigo\\vfs\\visitor\\vfsStreamPrintVisitor;
$structure = array( 'Core' =\> array( 'AbstractFactory' =\> array( 'test.php' =\> 'some text content', 'other.php' =\> 'Some more text content', 'Invalid.csv' =\> 'Something else', ), 'AnEmptyFolder' =\> array(), 'badlocation.php' =\> 'some bad content', ) );
$root = vfsStream::setup("root", '', $structure);

So now we have created a complex structured file system. How can we check whether the structure is same?

1
vfsStream::inspect(new vfsStreamPrintVisitor());

Now you have the structure how can you get the contents of the file you have created?

1
file\_get\_contents(vfsStream::url('root/Core/badlocation.php'));

Or you can do via

1
$root->getChild('Core/badlocation.php')-\>getContent();

The question was raised by myself at https://github.com/mikey179/vfsStream/issues/48 . You can create directory and use vfsStreamPrintVisitor to see how the structure will be

1
mkdir(vfsStream::url('root/Core/AnEmptyFolder/hello')); vfsStream::inspect(new vfsStreamPrintVisitor());

There is more documentation how you can use in phpunit or have a look into the recent commits on Aura.View For more documentation have a look into https://github.com/mikey179/vfsStream/wiki

phpunit, vfsstream