I'm rather new to PHP . I was asked to convert some of my tests from Java to PHP to comply with what a client is asking.
So I started with the basic tests (API), and decided to use Guzzler and Behat to make things easier. The problem is that I can't seem to use the same client for all my tests, which is most likely due to the fact that I have no idea what I'm doing in PHP
Here's a snippet that I'm trying to get working:
<?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');
}
}
The issue I'm having is that inside the method iIssueA.... , the variable $client is not being recognized (I need to use the same client that is setup in the prepare function).
You can have something like 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');
}
in order to use $this
you need to remove static
from prepare
method.