If I comment out one of the tests bellow the test passes. However, running both together, the last one will fail (even if I change tests order):
Production code:
<?php
class View
{
private $filename;
private $data;
public function __construct($filename)
{
$this->filename = $filename;
$this->data = [];
}
public function __set($key, $value)
{
$this->data[$key] = $value;
}
public function render()
{
extract($this->data);
ob_start();
require_once $this->filename;
return ob_get_clean();
}
public function __toString()
{
return $this->render();
}
}
Test class:
require_once 'vendor/autoload.php';
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamWrapper;
/**
* @outputBuffering enabled
*/
class ViewTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
vfsStream::setup();
}
/**
* @outputBuffering enabled
*/
public function testRenderSimpleView()
{
$fileContent = 'index file';
vfsStreamWrapper::getRoot()->addChild(
vfsStream::newFile('index.php')->withContent($fileContent)
);
$view = new View(vfsStream::url('index.php'));
echo $view->render();
$this->expectOutputString($fileContent);
}
/**
* @outputBuffering enabled
*/
public function testRenderViewWithData()
{
$filename = 'index.php';
$fileContent = '<?php echo $a; ?>';
vfsStreamWrapper::getRoot()->addChild(
vfsStream::newFile($filename)->withContent($fileContent)
);
$view = new View(vfsStream::url($filename));
$view->a = 1;
echo $view;
$this->expectOutputString('1');
}
}
test output:
PHPUnit 3.7.10 by Sebastian Bergmann.
.F
Time: 0 seconds, Memory: 3.75Mb
There was 1 failure:
1) ViewTest::testRenderViewWithData
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'1'
+''
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
It doesn't make any sense to me. What am I missing?