Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

I'm not going to go into to much details by writing what does what but you can study the code below. Also make sure you visit the links below for more info.


Behat.yml


default:
formatter:
name: pretty
parameters:
output_styles:
comment: [ magenta ]
context:
class: Application\BackendBundle\Features\Context\FeatureContext
parameters:
base_url: 'http://football.local/app_test.php/backend/'
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
kernel:
env: test
debug: true
Behat\MinkExtension\Extension:
base_url: 'http://football.local/app_test.php/backend/'
javascript_session: symfony2
goutte: ~
selenium2: ~
paths:
bootstrap: %behat.paths.features%/Context
features: %behat.paths.base%/src/Application/BackendBundle/Features

Feature


Feature: Test feature

Scenario: I can handle dynamic response content
Given I am on "/"
Then the response status code should be 200
And I should see "Welcome to Application backend!"
Then I send a "GET" request to "user"
And the response status code should be 200
And the response type should be "application/json"
And the response should contain "Hello"

FeatureContext.php


You should print_r(get_class_methods($client)); and print_r(get_class_methods($this->getSession())); to see what's available for you to use.



namespace Application\BackendBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Exception;

class FeatureContext extends MinkContext
{
private $baseUrl;
private $response;

public function __construct(array $parameters)
{
$this->baseUrl = $parameters['base_url'];
}

/**
* @When /^I send a "([^"]*)" request to "([^"]*)"$/
*
* @param $method
* @param $uri
*/
public function iSendARequestTo($method, $uri)
{
$client = $this->getSession()->getDriver()->getClient();
$this->response = $client->request($method, $this->baseUrl.$uri);
}

/**
* @Then /^the response type should be "([^"]*)"$/
*
* @param $type
* @throws Exception
*/
public function theResponseTypeShouldBeJson($type)
{
$headers = $this->getSession()->getResponseHeaders();

if ($headers['Content-Type'][0] != $type) {
throw new Exception('Invalid response type');
}
}
}

References


For more examples, you can check links below.