hikaeme/stdin-iterator

Iterator to read STDIN line by line

1.1.0 2017-06-03 10:34 UTC

This package is auto-updated.

Last update: 2024-10-17 01:47:01 UTC


README

Usage

use Hikaeme\StdinIterator;

$stdin = new StdinIterator();

foreach ($stdin as $line) {
    echo $line;
}

Installation

composer require hikaeme/stdin-iterator

Testable STDIN

class SampleCommand
{
    private $stdin;

    public function __construct(StdinIterator $stdin = null)
    {
        $this->stdin = $stdin ?: new StdinIterator();
    }

    public function run()
    {
        foreach ($this->stdin as $line) {
            echo $line;
        }
    }
}
class SampleCommandTest extends \PHPUnit_Framework_TestCase
{
    public function test()
    {
        $stub = new StdinIteratorStub();
        $stub->setStdin("1\n2\n3\n");

        $command = new \SampleCommand($stub);
        ob_start();
        $command->run();
        $result = ob_get_clean();

        $this->assertSame("1\n2\n3\n", $result);
    }
}