简体   繁体   中英

How test STDIN in PHPUnit

I have reader class which read from stdin and return readed value.

class Reader 
{
    const STREAM_READ = 'php://stdin';

    private $_streamHandle;

    public function __construct($stream = self::STREAM_READ)
    {
        $this->_streamHandle = fopen($stream, 'r');
    }

    public function getReadedValue()
    {
        $value = trim(fgets($this->_streamHandle));

        return $value;
    }

    public function __destruct()
    {
        fclose($this->_streamHandle);
    }
}

Now is my question, how I can test this class, reading something from stdin and return readed value by getReadedValue() function?

You test the Reader , not if STDIN is working or not.

Because you test the unit (the Reader ) it is not important what that filename is as it is only optional. You can inject something different, for example the filename of a temporary file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM