Created
July 28, 2018 19:28
-
-
Save QWp6t/44d77d6929854fc2b76e843ef57cfd11 to your computer and use it in GitHub Desktop.
Trait for using mikey179/vfsStream with phpunit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]] | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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