私はファイルシステム操作(よく実際にはphp://入力からの読み込み)をvfsStreamでモックしようとしていますが、まともな文書や例がないと本当に邪魔です。次のように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');
}));
}
を実際の試験では私はVFSを設定し、次のように主張を実行しよう(これは間接的にgetHandle()をトリガーするメソッドを呼び出します):
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に基づいて1つだけのクラスを書いており、それは仕事を完了するのに十分です。以下も参照してください:http://php.net/manual/en/class.streamwrapper.php – Codebeat