BDD API Development with Symfony and Behat You may have built an API in Symfony before. You may have even written some browser tests in Beta. Did you ever consider using Behat to write integration tests for your API? If not, you definitely should. The portability and reusability of Behat steps make it the perfect platform for API integration tests. The Symfony kernel integration for Behat and absence of JavaScript in an API makes this match made in heaven. Pull up a cloud and let me show you the pure awesomeness that is BDD API Development with Symfony and Behat.
30. @adam_englander
You are dealing with this:
HTTP/1.1 200 OK
Date: Thu, 19 Oct 2017 06:28:02 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 1009
Server: GitHub.com
Status: 200 OK
Vary: Accept-Encoding
X-GitHub-Request-Id: CD6C:D7DE:E24F3B2:123F37BB:59E845F1
{
"login": "symfony",
"id": 143937,
"url": "https://api.github.com/orgs/symfony",
. . .
31. @adam_englander
Feature: All pages require login
As a user
In order to view the home page
I must login to the website
Scenario: Not logged in redirects to login page
When I go to the home page
Then I am redirected to the login page
Scenario: Logged sees page
Given I am logged in
When I go to the home page
Then I see hello Adam
Web
32. @adam_englander
Feature: All endpoints require OAuth token
As an API consumer
In order to access an endpoint
I must be authenticated
Scenario: Not logged in shows 401
When I access the status endpoint
Then the HTTPS Status is 401 Unauthorized
And the WWW-Authenticate header is Bearer
realm=“API Realm”
Scenario: Authorized returns endpoint response
Given a valid OAuth Token
When I access the status endpoint
Then the HTTP Status is 200 OK
Device to
Device
43. @adam_englander
Symfony Kernel Context
class SymfonyKernelFeatureContext
extends AbstractFeatureContext
implements KernelAwareContext
public function __construct() {
$this->psr7Factory = new DiactorosFactory();
}
public function setKernel(KernelInterface $kernel) {
$this->client = $kernel
->getContainer()->get(‘test.client');
}