10

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?

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
  • execute th script with a command line – Cole Tobin Apr 11 '13 at 19:47
  • 2
    There's a filesystem mocking extension for PHPUnit called vfsStream, which I thought might help but the docs say nothing, so perhaps not. Maybe worth a dig though? It's on GitHub. Alternatively here's a related QA that might be worth reading: http://stackoverflow.com/questions/9158155/how-to-write-unit-tests-for-interactive-console-app – Darragh Enright Apr 11 '13 at 19:55

1 Answers1

1

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.

M8R-1jmw5r
  • 4,896
  • 2
  • 18
  • 26