2016-04-27 30 views
0

私はWebクローラースクリプトをテストしています。私はPHPの組み込みのWebサーバーを使用してページをローカルでテストしています。Behat:テストが完了したら、Webサーバープロセスを強制終了できません。

私はサーバを起動できますが、すでに終了しているためプロセスを終了できません(例外は、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を発行すると見つかりません。行を実行した後に行)、私が出席しているようにそれを殺すために "クリーナー"ではないのですか?

答えて

0

execの呼び出しは、出力パラメータが欠落しています。

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

これが設定されていない限り$return_varが実際にコマンドの出力であるため、例外は常に(配列でない整数である)、スローされます。

関連する問題