public function testQueueCompleteEventHandler()
 {
     $files = ['task401', 'task402'];
     $testChain = new ReportingChain();
     foreach ($files as $f) {
         if (file_exists("/tmp/{$f}")) {
             unlink("/tmp/{$f}");
         }
         $testTask = new ReportingTask();
         $testTask->cliCommand('touch', ["/tmp/{$f}"]);
         $testChain->addTask($testTask);
     }
     $firstTaskId = $testChain->registerChain();
     DeferredHelper::runImmediateTask($firstTaskId);
     sleep(2);
     $this->assertTrue(file_exists('/tmp/task401'));
     /** @var DeferredQueue $queue */
     $queue = DeferredQueue::findOne(['id' => $firstTaskId]);
     $process = new Process('pwd > /dev/null');
     $process->run();
     $queue->setProcess($process);
     $event = new DeferredQueueCompleteEvent($queue);
     QueueCompleteEventHandler::handleEvent($event);
     sleep(2);
     $this->assertTrue(file_exists('/tmp/task402'));
 }
 /**
  * Builds ReportingTask and places it into certain group. Also if group is not exists yet, it will be created
  * with necessary parameters, such as group_notifications=0.
  *
  * @param array $command
  * @param string $groupName
  * @return ReportingTask
  */
 public static function buildTask($command, $groupName)
 {
     $groupConfig = ['email_notification' => 0, 'allow_parallel_run' => 0, 'group_notifications' => 0, 'run_last_command_only' => 0];
     if (null === ($group = DeferredGroup::findOne(['name' => $groupName]))) {
         $group = new DeferredGroup();
         $group->loadDefaultValues();
         $group->setAttributes($groupConfig);
         $group->name = $groupName;
         $group->save();
     }
     if ((int) $group->group_notifications !== 0) {
         // otherwise DeferredController 'deferred-queue-complete' event will not trigger
         // and we'll unable to write config
         $group->setAttributes($groupConfig);
         $group->save(array_keys($groupConfig));
     }
     $task = new ReportingTask();
     $task->model()->deferred_group_id = $group->id;
     $task->cliCommand(DeferredHelper::getPhpBinary(), $command);
     return $task;
 }