Really can't in queued method. But for testing you can override your env setting and use queue sync (or what ever the default is). Set it as you would a testing database as in some of the videos here.
Apr 17, 2017
4
Level 26
How to test Symfony Process inside a queued job?
Hello there,
I am relatively new to testing and so... I have this inside a queued job:
public function handle()
{
...
$output .= $this->runCommand("ls -la");
...
}
/**
* Runs a command
*
* @param $cmd
* @param string $cwd
*
* @return string
*/
private function runCommand($cmd, $cwd = '')
{
$process = new Process(trim($cmd), $cwd);
$process->setTimeout(300);
$process->run();
$process->wait();
if ( ! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return trim($process->getOutput());
}
How can I test this without actually running the command?
Is it ok if I mock the Proccess class inside my code?
Level 26
For this test I made a mock of Process inside the job
private function getProccess($cmd,$cwd)
{
if (app()->environment() != 'testing'){
return new Process(trim($cmd), $cwd);
}
$process = \Mockery::mock(Process::class);
$process->shouldReceive(
'setTimeout', 'run', 'wait', 'stop', 'getCommandLine',
'getExitCode', 'getExitCodeText', 'getWorkingDirectory',
'isOutputDisabled', 'getErrorOutput'
);
$process->shouldReceive('isSuccessful')->andReturn(config('command-should-succeed'));
$process->shouldReceive('getOutput')->andReturn('sent for execution');
return $process;
}
and my runCommand method calls for
$process = $this->getProcess($cmd, $cwd);
then in my test I set a config parameter
config(['command-should-succeed' => true]);
not sure if it is the best solution but works perfectly (:
Please or to participate in this conversation.