Spawning interactive CLI command with Expect exits with 0 early, when run as Symfony Process in Behat context Spawning interactive CLI command with Expect exits with 0 early, when run as Symfony Process in Behat context symfony symfony

Spawning interactive CLI command with Expect exits with 0 early, when run as Symfony Process in Behat context


As per the answer to Expect Script wait Command Hangs you need to wait for EOF instead of using the interact command:

set timeout 180spawn ./bin/albumgrabexpect "Please enter the name of the directory"send "/tmp/php-london\n"expect "Please enter the URL to the first image"send "https://www.facebook.com/PeeHPLondon/photos/pb.7119218495.-2207520000.1430669248./10153559172718496/\n"expect EOF

Here's a full script I've used to test it on an OS X:

<?phprequire_once __DIR__.'/vendor/autoload.php';use Symfony\Component\Process\Process;$expect = <<<BASHexec expect -c '    set timeout 180    spawn ./bin/albumgrab    expect "Please enter the name of the directory"    send "/tmp/php-london\n"    expect "Please enter the URL to the first image"    send "https://www.facebook.com/PeeHPLondon/photos/pb.7119218495.-2207520000.1430669248./10153559172718496/?type=3&src=https%3A%2F%2Ffbcdn-sphotos-g-a.akamaihd.net%2Fhphotos-ak-xfp1%2Fv%2Ft1.0-9%2F10986697_10153559172718496_5727444485530442900_n.jpg%3Foh%3Dc47770f4cd15fecc6888bcd504899087%26oe%3D55DA9CB0%26__gda__%3D1439174101_7c78a93bf247dbad6c56681b6db5309c&size=960%2C959&fbid=10153559172718496\\n"    expect EOF'BASH;$process = new Process($expect);$process->setPty(true);$process->start();$process->wait(function ($type, $buffer) {    if (Process::ERR === $type) {        echo 'ERR > '.$buffer;    } else {        echo 'OUT > '.$buffer;    }});if (!$process->isSuccessful()) {    throw new \RuntimeException($process->getErrorOutput());}


In order to be able receive an answer to a question - you definitely need a Terminal (TTY) or a Pseudo-Terminal (PTY) to obtain any user-input.

That's why - without $process->setTty(true) or setPty(true) - the QuestionHelper silently falls back to the default value and the command succeeds with exit code 0.

Now - to test your command with example user input - you should make use of the symfony's console helper components instead of using expect.

Symfony\Component\Console\Helper\HelperSet Symfony\Component\Console\Tester\CommandTester

How to use these helpers is described in the cookbook chapter Testing a Command that expects input.