2016-04-27 2 views
0

Я тестирую скрипт веб-искателя. Я использую встроенный веб-сервер php для тестирования на страницах локально.Behat: Не удается убить процесс веб-сервера после завершения тестирования

Я могу запустить сервер, но я не могу убить процесс, потому что он уже убит (я получаю исключение, которое я установил Could not kill the testing web server).

Вот моя попытка:

<?php 

use Behat\Behat\Tester\Exception\PendingException; 
use Behat\Behat\Context\Context; 
use Behat\Behat\Context\SnippetAcceptingContext; 
use Behat\Gherkin\Node\PyStringNode; 
use Behat\Gherkin\Node\TableNode; 

use Behat\Behat\Hook\Scope\BeforeScenarioScope; 
use Behat\Behat\Hook\Scope\AfterScenarioScope; 

/** 
* Defines application features from the specific context. 
*/ 
class FeatureContext implements Context, SnippetAcceptingContext 
{ 

    const TESTING_BASE_URL = 'http://127.0.0.1:6666'; 
    const TESTING_DIR = '/tmp/testDirectory'; 

    private $pid; 

    /** 
    * Initializes context. 
    * 
    * Every scenario gets its own context instance. 
    * You can also pass arbitrary arguments to the 
    * context constructor through behat.yml. 
    */ 
    public function __construct() 
    { 
    } 

    /** 
    * @BeforeScenario 
    */ 
    public function before(BeforeScenarioScope $scope) 
    { 
     // Create testing directory holding our pages 
     if (!is_dir(self::TESTING_DIR)) { 
      if (!mkdir(self::TESTING_DIR)) { 
       throw new \Exception('Cannot create the directory for testing'); 
      } 
     } 

     // Start the testing server 
     $command = sprintf(
      'php -S %s -t $%s >/dev/null 2>&1 & echo $!', 
      escapeshellarg(self::TESTING_BASE_URL), 
      escapeshellarg(self::TESTING_DIR) 
     ); 

     $output = []; 
     exec($command, $output, $return_var); 

     if ($return_var !== 0) { 
      throw new \Exception('Cannot start the testing web server'); 
     } 

     $this->pid = (int)$output[0]; 
     echo sprintf(
      'Testing web server started on %s with PID %s %s %s', 
      self::TESTING_BASE_URL, 
      (string)$this->pid, 
      PHP_EOL, 
      PHP_EOL 
     ); 

    } 

    /** 
    * @AfterScenario 
    */ 
    public function after(AfterScenarioScope $scope) 
    { 
      // ... kill the web server 
      $output = []; 
      exec('kill ' . (string) $this->pid, $return_var); 

      if ($return_var !== 0) { 
       throw new \Exception('Could not kill the testing web server (PID ' . (string) $this->pid . ')'); 
      } 

      echo 'Testing web server killed (PID ', (string) $this->pid, ')', PHP_EOL, PHP_EOL; 

      // ... remove the test directory 
      $o = []; 
      exec('rm -rf ' . escapeshellarg(self::TESTING_DIR), $o, $returnVar); 

      if ($returnVar !== 0) { 
       throw new \Exception('Cannot remove the testing directory'); 
      } 
    } 


    // ... 
} 

Я также пробовал различные вещи, как положить все это в конструкторе, используя register_shutdown_function, без какого-либо успеха.

Что мне не хватает? Любая идея о том, как я могу это решить?

Вместо того, чтобы просто «не заботиться о том, чтобы убить серверный процесс» (потому что для меня это похоже, что оно исчезло, когда я пытаюсь убить процесс, следовательно, ошибка, я не могу найти его, когда выдаю ps aux | grep php по команде line after running behat), разве это не «чище», чтобы убить его, пока я посещаю?

ответ

0

ехеса вызов отсутствует выходной параметр:

exec('kill ' . (string) $this->pid, $output, $return_var); 

Если только это установлено, исключение всегда будет отброшено, потому что $return_var на самом деле вывод команды (который представляет собой массив не является целым числом) ,

Смежные вопросы