Skip to content

Commit 1adfbcd

Browse files
committed
feat[behat]: first working scenario
1 parent ae39fa2 commit 1adfbcd

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

.env.test

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ KERNEL_CLASS='App\Kernel'
33
APP_SECRET='$ecretf0rt3st'
44
SYMFONY_DEPRECATIONS_HELPER=999999
55
PANTHER_APP_ENV=panther
6+
7+
BEHAT_BASE_URL=http://127.0.0.1:8000/index.php

behat.yml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ default:
22
suites:
33
default:
44
contexts:
5+
- App\Tests\Behat\RestContext
56
- App\Tests\Behat\DemoContext
67

78
extensions:

config/services_test.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ services:
55

66
App\Tests\Behat\:
77
resource: '../tests/Behat/*'
8+
9+
App\Tests\Behat\RestContext:
10+
public: true
11+
arguments:
12+
# - "%kernel.environment%"
13+
$base_url: "%env(BEHAT_BASE_URL)%"

features/book.feature

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Book
2+
Crud (REST) to /v1/books
3+
4+
Scenario: GET /v1/books
5+
# Given there is a "Sith Lord Lightsaber", which costs £5
6+
When I run GET on "v1/books"
7+
Then the status code is 200
8+
And the body is json
9+

tests/Behat/RestContext.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Behat;
6+
7+
use Behat\Behat\Context\Context;
8+
use Behat\Behat\Tester\Exception\PendingException;
9+
use GuzzleHttp\Client;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\Exception\TransferException;
12+
use Psr\Http\Message\ResponseInterface;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\HttpKernel\KernelInterface;
16+
17+
/**
18+
* This context class contains the definitions of the steps used by the demo
19+
* feature file. Learn how to get started with Behat and BDD on Behat's website.
20+
*
21+
* @see http://behat.org/en/latest/quick_start.html
22+
*/
23+
final class RestContext implements Context
24+
{
25+
/** @var KernelInterface */
26+
private $kernel;
27+
28+
/** @var \GuzzleHttp\Psr7\Response */
29+
private $_response;
30+
private $_bodyDecoded;
31+
32+
/** @var Client */
33+
private $_client;
34+
35+
36+
37+
public function __construct(KernelInterface $kernel, string $base_url)
38+
{
39+
$this->kernel = $kernel;
40+
$this->_client = new Client(['base_uri' => $base_url]);
41+
// throw new \Exception("nenne: ".$base_url);
42+
}
43+
/**
44+
* @When I run GET on :arg1
45+
*/
46+
public function iRunGetOn($arg1)
47+
{
48+
try {
49+
$this->_response = $this->_client->get($arg1);
50+
51+
} catch (RequestException $e) {
52+
53+
if ($e->hasResponse()) {
54+
$this->_response = $e->getResponse();
55+
} else {
56+
throw $e;
57+
}
58+
} catch (\Exception $e) {
59+
60+
throw $e;
61+
}
62+
}
63+
64+
/**
65+
* @Then the status code is :arg1
66+
*/
67+
public function theStatusCodeIs($arg1)
68+
{
69+
if ((string) $this->_response->getStatusCode() !== $arg1) {
70+
throw new \Exception('HTTP code does not match ' . $arg1 .
71+
' (actual: ' . $this->_response->getStatusCode() . ')' . substr($this->_response->getBody(),0,10000));
72+
}
73+
}
74+
75+
/**
76+
* @Then the body is json
77+
*/
78+
public function theBodyIsJson()
79+
{
80+
try {
81+
$body=json_decode($this->_response->getBody()->getContents(), true);
82+
$this->_bodyDecoded=$body;
83+
}catch (\Exception $e ){
84+
if($body=$this->_response->getBody()){
85+
print "$body\n";
86+
throw $e;
87+
}
88+
}
89+
}
90+
91+
}

0 commit comments

Comments
 (0)