Skip to content

fjyuu/php-stdin-iterator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stdin Iterator

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);
    }
}

About

Iterator to read STDIN line by line

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages