2017-02-14 3 views
1

私はむしろPHPを初めて使っています。私は、JavaからPHPへのいくつかのテストを、クライアントが要求していることに従うように変換するように求められました。PHPはメソッド間のGuzzlerクライアントを使用します

私は基本テスト(API)から始め、GuzzlerとBehatを使って作業を簡単にすることに決めました。問題は、すべてのテストで同じクライアントを使用することができないということです。これは、PHPで何をしているのかわからないことが原因です。

私はスニペットです私がいる問題は....方法iIssueAの内側に、変数$クライアントが認識されていないことである

<?php 

use Behat\Behat\Context\Context; 
use Behat\Testwork\Hook\Scope\BeforeSuiteScope; 
use GuzzleHttp\Client; 


class FeatureContext implements Context 
{ 

/** 
* @BeforeSuite 
*/ 
public static function prepare(BeforeSuiteScope $scope) 
{ 
    // Setup of Guzzle for API calls 
    $client = new Client(['base_uri' => 'http://test.stxgrp.com.ar']); 
} 

/** 
* @Then the response status code should be :arg1 
*/ 
public function theResponseStatusCodeShouldBe($arg1) 
{ 
    //Going to make an assert 
} 

/** 
* @When /^I issue a GET request at url (.*)\/(.*)$/ 
*/ 
public function iIssueAGETRequestAtUrl1($PROVIDER_NAME, $PROVIDER_PLACE_ID) 
{ 
    $response = $client->request('GET', '$PROVIDER_NAME.$PROVIDER_PLACE_ID'); 
} 

} 

を(私はセットアップで同じクライアントを使用する必要があります。作業を取得しようとしています準備機能)。あなたはprepare方法からstaticを削除する必要が$thisを使用するために

private $client; 

/** 
* @BeforeSuite 
*/ 
public function prepare(BeforeSuiteScope $scope) 
{ 
    // Setup of Guzzle for API calls 
    $this->client = new Client(['base_uri' => 'http://test.stxgrp.com.ar']); 
} 

/** 
* @When /^I issue a GET request at url (.*)\/(.*)$/ 
*/ 
public function iIssueAGETRequestAtUrl1($PROVIDER_NAME, $PROVIDER_PLACE_ID) 
{ 
    $this->client->request('GET', '$PROVIDER_NAME.$PROVIDER_PLACE_ID'); 
} 

+0

クライアントをクラス変数として使用しようとしましたか?それを$ this-> clientと呼んでください – lauda

+0

私は[behat3を使ってBasic Auth symfony APIをテストする]と思っています(http://www.inanzzz.com/index.php/post/l41o/testing-a-basic-auth-symfony -api-with-behat3)、[behat v2でのApiリクエスト応答テストにはjson、xml、html、cliが含まれています](http://www.inanzzz.com/index.php/post/ajqn/api-request-response- testing-with-behat-v2-includes-json-xml-html-and-cli)および[Behat v1によるApiリクエスト応答テスト](http://www.inanzzz.com/index.php/post/xw1v/api -request-response-testing-with-behat-v1)が役に立ちます。 – BentCoder

答えて

2

あなたはこのようなものを持つことができます。

関連する問題