Search code examples
phpunit-testingoopphpunitvfs-stream

Insert a file into specific directory/node using vfsStream


The use case of vfsStream is as follows:

$directories = explode('/', 'path/to/some/dir');

$structure = [];
$reference =& $structure;

foreach ($directories as $directory) {
    $reference[$directory] = [];
    $reference =& $reference[$directory];
}

vfsStream::setup();
$root = vfsStream::create($structure);
$file = vfsStream::newFile('file')
    ->at($root) //should changes be introduced here?
    ->setContent($content = 'Some content here');

The output of vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure() is

Array
(
    [root] => Array
    (
        [path] => Array
        (
            [to] => Array
            (
                [some] => Array
                (
                    [dir] => Array
                    (
                    )
                )
            )
        )

        [file] => Some content here
    )
)

Is it possible to insert a file into specific directory, for example, under dir directory?


Solution

  • The answer was given on github; thus instead of

    ->at($root)
    

    one should use

    ->at($root->getChild('path/to/some/dir')).