I'm trying to mock a filesystem operation (well actually a read from php://input) with vfsStream but the lack of decent documentation and examples is really hampering me.
The relevant code from the class I'm testing is as follows:
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;
}
}
The setup I'm using in my PHPUnit test is as follows:
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');
}));
}
In an actual test (which calls a method which indirectly triggers getHandle()) I try to set up the VFS and run an assertion as follows:
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 ());
}
This just causes the test to hang.
Obviously I'm doing something very wrong here, but given the state of the documentation I'm unable to work out what it is I'm meant to be doing. Is this something caused by vfsstream, or is phpunit mocking the thing I need to be looking at here?