Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:38:31+00:00 2026-06-09T11:38:31+00:00

I’m trying to mock a filesystem operation (well actually a read from php://input) with

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:38:32+00:00Added an answer on June 9, 2026 at 11:38 am

    So … how to test with streams? All vfsStream does is provide a custom stream wrapper for file system operations. You don’t need the full-blown vfsStream library just to mock the behavior of a single stream argument — it’s not the correct solution. Instead, you need to write and register your own one-off stream wrapper because you aren’t trying to mock file system operations.

    Say you have the following simple class to test:

    class ClassThatNeedsStream {
        private $bodyStream;
        public function __construct($bodyStream) {
            $this->bodyStream = $bodyStream;
        }
        public function doSomethingWithStream() {
            return stream_get_contents($this->bodyStream);
        }
    }
    

    In real life you do:

    $phpInput = fopen('php://input', 'r');
    new ClassThatNeedsStream($phpInput);
    

    So to test it, we create our own stream wrapper that will allow us to control the behavior of the stream we pass in. I can’t go into too much detail because custom stream wrappers are a large topic. But basically the process goes like this:

    1. Create custom stream wrapper
    2. Register that stream wrapper with PHP
    3. Open a resource stream using the registered stream wrapper scheme

    So your custom stream looks something like:

    class TestingStreamStub {
    
        public $context;
        public static $position = 0;
        public static $body = '';
    
        public function stream_open($path, $mode, $options, &$opened_path) {
            return true;
        }
    
        public function stream_read($bytes) {
            $chunk = substr(static::$body, static::$position, $bytes);
            static::$position += strlen($chunk);
            return $chunk;
        }
    
        public function stream_write($data) {
            return strlen($data);
        }
    
        public function stream_eof() {
            return static::$position >= strlen(static::$body);
        }
    
        public function stream_tell() {
            return static::$position;
        }
    
        public function stream_close() {
            return null;
        }
    }
    

    Then in your test case you would do this:

    public function testSomething() {
        stream_wrapper_register('streamTest', 'TestingStreamStub');
        TestingStreamStub::$body = 'my custom stream contents';
        $stubStream = fopen('streamTest://whatever', 'r+');
    
        $myClass = new ClassThatNeedsStream($stubStream);
        $this->assertEquals(
            'my custom stream contents',
            $myClass->doSomethingWithStream()
        );
    
        stream_wrapper_unregister('streamTest');
    }
    

    Then, you can simply change the static properties I’ve defined in the stream wrapper to change what data comes back from reading the stream. Or, extend your base stream wrapper class and register it instead to provide different scenarios for tests.

    This is a very basic intro, but the point is this: don’t use vfsStream unless you’re mocking actual filesystem operations — that’s what it’s designed for. Otherwise, write a custom stream wrapper for testing.

    PHP provides a prototype stream wrapper class to get you started: http://www.php.net/manual/en/class.streamwrapper.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.