Tôi đang cố gắng thử nghiệm một hoạt động hệ thống tập tin (thực sự là đọc từ đầu vào php: //) với vfsStream nhưng thiếu tài liệu và ví dụ phong nha thực sự cản trở tôi.Đang cố gắng kiểm tra các hoạt động của hệ thống tập tin với VFSStream
Mã liên quan từ lớp tôi là thử nghiệm như sau:
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;
}
}
Quá trình cài đặt Tôi đang sử dụng trong thử nghiệm PHPUnit của tôi là như sau:
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');
}));
}
Trong một thử nghiệm thực tế (mà gọi một phương pháp mà gián tiếp gây nên getHandle()) Tôi cố gắng thiết lập VFS và chạy một xác nhận như sau:
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());
}
Điều này chỉ làm cho thử nghiệm bị treo.
Rõ ràng là tôi đang làm điều gì đó rất sai ở đây, nhưng với trạng thái của tài liệu, tôi không thể tìm ra điều tôi muốn làm. Đây có phải là một cái gì đó gây ra bởi vfsstream, hoặc là phpunit chế giễu điều tôi cần phải nhìn vào đây?
VFSStream là mảnh phức tạp không cần thiết của bộ với tất cả các lớp con và không gian tên, vv tất cả mọi thứ phải là một cách tiếp cận lớp học. Nó là một wrapper cho một wrapper. Tôi đã viết chỉ một lớp dựa trên giao diện php StreamWrapper và đó là đủ để hoàn thành công việc. Xem thêm: http://php.net/manual/en/class.streamwrapper.php – Codebeat