Skip to content

Instantly share code, notes, and snippets.

@QWp6t
Created July 28, 2018 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QWp6t/44d77d6929854fc2b76e843ef57cfd11 to your computer and use it in GitHub Desktop.
Save QWp6t/44d77d6929854fc2b76e843ef57cfd11 to your computer and use it in GitHub Desktop.
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