Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 724
Piotr Olaszewski Avatar asked Apr 11 '13 19:04

Piotr Olaszewski


1 Answers

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.

like image 57
M8R-1jmw5r Avatar answered Oct 06 '22 09:10

M8R-1jmw5r