我试图用vfsStream来模拟文件系统操作(实际上是从php://输入读取),但缺乏体面的文档和示例实际上阻碍了我。试图用VFSStream测试文件系统操作
从类我测试的有关代码如下:
class RequestBody implements iface\request\RequestBody
{
const
REQ_PATH = 'php://input',
protected
$requestHandle = false;
/**
* Obtain a handle to the request body
*
* @return resource a file pointer resource on success, or <b>FALSE</b> on error.
*/
protected function getHandle()
{
if (empty ($this -> requestHandle))
{
$this -> requestHandle = fopen (static::REQ_PATH, 'rb');
}
return $this -> requestHandle;
}
}
我用我的PHPUnit的测试设置如下:
protected function configureMock()
{
$mock = $this -> getMockBuilder ('\gordian\reefknot\http\request\RequestBody');
$mock -> setConstructorArgs (array ($this -> getMock ('\gordian\reefknot\http\iface\Request')))
-> setMethods (array ('getHandle'));
return $mock;
}
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
\vfsStreamWrapper::register();
\vfsStream::setup ('testReqBody');
$mock = $this -> configureMock();
$this -> object = $mock -> getMock();
$this -> object -> expects ($this -> any())
-> method ('getHandle')
-> will ($this -> returnCallback (function() {
return fopen ('vfs://testReqBody/data', 'rb');
}));
}
在实际测试(它调用间接触发getHandle()的方法)我尝试设置VFS并按如下方式运行断言:
public function testBodyParsedParsedTrue()
{
// Set up virtual data
$fh = fopen ('vfs://testReqBody/data', 'w');
fwrite ($fh, 'test write 42');
fclose ($fh);
// Make assertion
$this -> object -> methodThatTriggersGetHandle();
$this -> assertTrue ($this -> object -> methodToBeTested());
}
这只是导致测试挂起。
显然我在这里做的事情非常错误,但鉴于文档的状态,我无法弄清楚我打算做什么。这是由vfsstream引起的,还是phpunit嘲笑我需要在这里看的东西?
VFSStream当你不需要调用
vfsStreamWrapper::register();
是所有子类和命名空间等一切都必须是一个不必要的复杂的工具包片一流的方法。它是一个包装的包装。我只写了一个基于php接口StreamWrapper的类,这足以完成工作。另请参阅:http://php.net/manual/en/class.streamwrapper.php – Codebeat 2013-08-02 23:01:45