VfsStream ile bir dosya sistemi işlemi (aslında php: // girişten okunan bir dosya) ile uğraşmaya çalışıyorum ancak iyi bir dokümantasyon ve örnek olmaması beni gerçekten rahatsız ediyor. aşağıdaki gibiVFSStream ile dosya sistemi işlemlerini test etmeye çalışmak
Ben test olduğum sınıftan ilgili kodudur:
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;
}
}
şöyle benim PHPUnit testinde kullanıyorum kurgusunda:
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');
}));
}
gerçek testte I VFS kurmak ve aşağıdaki gibi bir onaylama çalıştırmayı (ki dolaylı getHandle() tetikleyen bir yöntemini çağırdığında):
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());
}
Bu sadece testin askıda kalmasına neden oluyor.
Açıkçası, burada çok yanlış bir şey yapıyorum, ancak belgelerin durumu göz önünde bulundurulduğunda, ne yapmak istediğimi anlayamıyorum. Bu vfsstream'in neden olduğu bir şey mi, yoksa buraya bakmaya ihtiyacım olan şeyle dalga geçen phpunit mi?
VFSStream kullanırken
orijinal kodundan Unutulmaması gereken bir diğer şey,
vfsStreamWrapper::register();
aramaya gerek kalmamasıdır sınıf yaklaşımı Bir sarıcı için bir sarıcıdır. StreamWrapper php arayüzüne dayalı bir sınıf yazdım ve işi bitirmek için bu yeterli. Ayrıca bakınız: http://php.net/manual/en/class.streamwrapper.php – Codebeat