PHP在方法之间使用Guzzler客户端

时间:2017-02-14 17:49:55

标签: php guzzle behat

我对PHP很新。我被要求将我的一些测试从Java转换为PHP,以符合客户的要求。

所以我开始使用基本测试(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 ....中,变量$ client未被识别(我需要使用在prepare函数中设置的相同客户端)。

1 个答案:

答案 0 :(得分:1)

你可以这样:

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

要使用$this,您需要从static方法移除prepare