PHP code example of hikaeme / stdin-iterator

1. Go to this page and download the library: Download hikaeme/stdin-iterator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hikaeme / stdin-iterator example snippets


use Hikaeme\StdinIterator;

$stdin = new StdinIterator();

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

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