Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trait for using mikey179/vfsStream with phpunit
<?php
namespace QWp6t\Tests;
/** composer require mikey179/vfsStream --dev */
use org\bovigo\vfs\vfsStream;
trait VirtualFileSystem
{
protected $filesystem;
public function setUp()
{
$this->filesystem = new class ($this->fixtures()) {
/** @var \org\bovigo\vfs\vfsStreamDirectory */
protected $stream;
public function __construct($fixtures = [])
{
$this->stream = vfsStream::setup('__fixtures__', null, $fixtures);
}
public function __call($name, $arguments)
{
return $this->stream->{$name}(...$arguments);
}
public function __get($name)
{
return $this->stream->{$name};
}
public function __toString()
{
return $this->stream->url();
}
};
}
protected function write($file, $contents)
{
$file = str_replace('\\', '/', $file);
$file = ltrim($file, '/');
$file = "{$this->filesystem}/{$file}";
file_put_contents($file, $contents);
return $file;
}
protected function writeDump($file, $data)
{
$contents = var_export($data, true);
return $this->write($file, $contents);
}
protected function fixtures()
{
$filesystem = [];
foreach ($this->fixtures ?? [] as $file => $content) {
$limbs = array_reverse(array_filter(explode('/', $file)));
$filesystem = array_merge_recursive(array_reduce($limbs, function ($leaf, $limb) {
return [$limb => $leaf];
}, $content), $filesystem);
}
return $filesystem;
}
}
<?php
namespace QWp6t\Tests\Unit;
use PHPUnit\Framework\TestCase;
use QWp6t\Tests\VirtualFileSystem;
/**
* NOTE the use of `$fixtures` property
*/
class Z1ExampleUsageTest extends TestCase
{
use VirtualFileSystem;
protected $fixtures = [
'foo/bar.txt' => 'foobar',
'biz/baz.log' => 'bizbaz',
];
/** @test */
public function it_should_read_contents_of_files()
{
$foobar = file_get_contents("{$this->filesystem}/foo/bar.txt");
$bizbaz = file_get_contents("{$this->filesystem}/biz/baz.log");
$this->assertEquals($foobar, 'foobar');
$this->assertEquals($bizbaz, 'bizbaz');
}
}
<?php
namespace QWp6t\Tests\Unit;
use PHPUnit\Framework\TestCase;
use QWp6t\Tests\VirtualFileSystem;
/**
* NOTE the use of `fixtures()` method
*/
class Z2ExampleUsageTest extends TestCase
{
use VirtualFileSystem;
/** @test */
public function it_should_read_contents_of_files()
{
$foobar = file_get_contents("{$this->filesystem}/foo/bar.txt");
$bizbaz = file_get_contents("{$this->filesystem}/biz/baz.log");
$this->assertEquals($foobar, 'foobar');
$this->assertEquals($bizbaz, 'bizbaz');
}
protected function fixtures()
{
$foobar = 'foobar';
$bizbaz = 'bizbaz';
return [
['foo' => ['bar' => $foobar]],
['biz' => ['baz' => $bizbaz]]
];
}
}
<?php
namespace QWp6t\Tests\Unit;
use PHPUnit\Framework\TestCase;
use QWp6t\Tests\VirtualFileSystem;
/**
* NOTE the use of `$fixtures` property, `setUp()` override, and `write()` method
*/
class Z3ExampleUsageTest extends TestCase
{
use VirtualFileSystem {
setUp as vfsSetup;
}
protected $fixtures = [
'foo/bar.txt' => 'foobar'
];
public function setUp()
{
$this->vfsSetup();
$bizbaz = 'bizbaz';
$this->write('/biz/baz.log', $bizbaz);
}
/** @test */
public function it_should_read_contents_of_files()
{
$foobar = file_get_contents("{$this->filesystem}/foo/bar.txt");
$bizbaz = file_get_contents("{$this->filesystem}/biz/baz.log");
$this->assertEquals($foobar, 'foobar');
$this->assertEquals($bizbaz, 'bizbaz');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.