2017-02-14 4 views
1

저는 PHP가 처음입니다. 나는 클라이언트에서 요구하는 것을 준수하기 위해 Java에서 PHP로 내 테스트 중 일부를 변환하도록 요청 받았다.PHP는 방법 들간에 Guzzler 클라이언트를 사용합니다.

그래서 기본 테스트 (API)부터 시작하여 더 쉽게 Guzzler와 Behat을 사용하기로 결정했습니다. 문제는 모든 테스트에 동일한 클라이언트를 사용할 수 없다는 것입니다. 이는 PHP에서 내가 무엇을하고 있는지 전혀 모른다는 사실 때문에 가장 가능성이 높습니다.

여기에 제가있는 스 니펫이 있습니다. 작업을 얻으려고 노력 :

<?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'); 
} 

} 

난 데 문제는 방법 iIssueA 내부 ..., 변수 $ 클라이언트가 내가 설치가에있는 동일한 클라이언트를 사용해야합니다 (인식되지 않는 것입니다 준비 기능). 당신은 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

[기본 인증 심포니 API를 behat3로 테스트] (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

당신이 뭔가를 할 수 있습니다.