2017-01-02 15 views
2

私のコードをテストしていますが、ヘッダに問題があります。各APIの私はそれを得るためにPHPUnit - getallheadersが機能しない

$headers = getallheaders(); 

を使用して、と私はアプリやcrhome郵便配達の拡張子をテストするとき、これは正常に動作します。 私はそのテスト・トークン(私は、アプリケーションで使用しますないトークン)を持つユーザーでそのQRコードでカードを撃っしようと、私はコールを参照してください。この

$client = $this->createClient(); 
    $client->request('GET', '/api/shotcard', 
     ['qrcode'=>'D0m1c173'], [], 
     ['HTTP_API_TOKEN' => 'abc123'] 
    ); 

    $this->assertEquals(200, $client->getResponse()->getStatusCode()); 

のように、私のテストを出せここのように:https://stackoverflow.com/a/11681422/5475228そう

If you use Nginx, PHP-FPM or any other FastCGI method of running PHP you’ve probably noticed that the function getallheaders() does not exist. There are many creative workarounds in the wild, but PHP offers two very nice features to ease your pain.

if (!function_exists('getallheaders')) { 
    function getallheaders() { 
    $headers = []; 
    foreach ($_SERVER as $name => $value) { 
     if (substr($name, 0, 5) == 'HTTP_') { 
      $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
     } 
    } 
    return $headers; 
    } 
} 
+0

doc:この関数は、apache_request_headers()の別名です。この関数の動作の詳細については、apache_request_headers()のドキュメントを参照してください。 http://php.net/manual/en/function.apache-request-headers.php – Matteo

+0

どのバージョンのPHPを使用していますか? PHP 5.5.7以降でCLIから利用できるようにする必要があります。 – Matteo

答えて

1

私はそのように(https://stackoverflow.com/a/11681422/5475228のおかげで)解決

private function request_headers($type, Request $request) 
{ 
    if(function_exists("getallheaders")) 
    { 
     if($header = getallheaders()[$type]) 
     { 
      return $header; 
     } 
    } 

    return $request->headers->get($type); 
} 

通常のリクエスト: テストがこの方法で失敗:

PHP Fatal error: Call to undefined function AppBackendBundle\Controller\getallheaders() in /var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php on line 42

5

this記事からgetallheaders()でapp getヘッダーから、PHPUnitからのリクエストはそれを取得します要求オブジェクト。なぜ(誰かが説明することができれば)わかりませんが、私は働いています。

+0

私はこの解決方法も使います。しかし、PHPUnitでは、ヘッダーを印刷しようとしましたが、PHPUnitは空の値を表示します。私はPHP 5.5.9を使用しています – NicolaPez

関連する問題