如果我注释掉下面的测试之一,则测试通过。但是,同时运行两者,最后一个将失败(即使我更改测试顺序):
生产代码:
<?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();
}
}
测试类:
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');
}
}
测试输出:
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.
这对我来说没有任何意义。我错过了什么?