Skip to content

Commit 0fc02e2

Browse files
committed
👮 Add behat tests
1 parent 671c6c2 commit 0fc02e2

10 files changed

+206
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
22
composer.lock
3+
cache

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ php:
66
- 7.0
77

88
before_script:
9+
- echo 'always_populate_raw_post_data = -1' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
910
- phpenv config-rm xdebug.ini
1011
- composer install --prefer-dist --optimize-autoloader
12+
- php -S 127.0.0.1:4224 -t "$TRAVIS_BUILD_DIR/testapp" &> /dev/null &
1113

1214
script:
1315
- vendor/bin/atoum -ulr
16+
- vendor/bin/behat -f progress

behat.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
default:
2+
suites:
3+
web:
4+
contexts:
5+
- Rezzza\RestApiBehatExtension\RestApiContext
6+
- Rezzza\RestApiBehatExtension\Json\JsonContext
7+
8+
extensions:
9+
Rezzza\RestApiBehatExtension\Extension:
10+
rest:
11+
base_url: http://localhost:4224
12+
store_response: true

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
"fzaninotto/faker": "^1.5",
2828
"league/tactician-bundle": "^0.4.1",
2929
"symfony/serializer": "^3.0",
30-
"symfony/validator": "^3.0"
30+
"symfony/validator": "^3.0",
31+
"symfony/framework-bundle": "^3.0",
32+
"rezzza/rest-api-behat-extension": "^5.0",
33+
"php-http/curl-client": "^1.5",
34+
"guzzlehttp/psr7": "^1.3"
3135
},
3236
"suggest": {
3337
"league/tactician-bundle": "Support for ValidatorMiddleware of TacticianBundle",

features/handle_exception.feature

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Feature: Handle exception
2+
3+
In order to let our application throw specific exception
4+
As a developper
5+
I map these exceptions to http status code
6+
7+
Scenario: Throw mapped exception
8+
When I send a POST request to "/exception?supported"
9+
Then the response status code should be 409
10+
And the JSON node "errors.message" should be equal to "This is my app message"
11+
12+
Scenario: Throw unmapped exception
13+
When I send a POST request to "/exception"
14+
Then the response status code should be 500
15+
And the JSON node "exception[0].message" should be equal to "Something wrong"

features/handle_payload.feature

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Feature: Handle json payload
2+
3+
In order to benefit of complex json payload
4+
As a developper
5+
I dump json payload into request attributes
6+
7+
Scenario: Send valid json
8+
Given I set "Content-Type" header equal to "application/json"
9+
When I send a POST request to "/echo" with body:
10+
"""
11+
{
12+
"name": "Bond"
13+
}
14+
"""
15+
Then the response should be in JSON
16+
And the JSON node "name" should be equal to "Bond"
17+
18+
Scenario: Send invalid json
19+
Given I set "Content-Type" header equal to "application/json"
20+
When I send a POST request to "/echo" with body:
21+
"""
22+
{
23+
"name": "Bond
24+
}
25+
"""
26+
Then the response should be in JSON
27+
And the response status code should be 400
28+
29+
Scenario: Send unsupported content-type
30+
Given I set "Content-Type" header equal to "text/html"
31+
When I send a POST request to "/echo" with body:
32+
"""
33+
{
34+
"name": "Bond"
35+
}
36+
"""
37+
Then the JSON node "name" should not exist
38+
39+
Scenario: Send invalid data
40+
Given I set "Content-Type" header equal to "application/json"
41+
When I send a POST request to "/echo" with body:
42+
"""
43+
{
44+
"firstname": "James"
45+
}
46+
"""
47+
Then the response status code should be 400
48+
And the JSON node "errors[0].parameter" should be equal to "name"
49+

testapp/config.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
show_exception_token: s3s4m3
3+
exception_http_code_map:
4+
'Rezzza\SymfonyRestApiJson\Tests\Fixtures\MyOtherException': 409

testapp/index.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
4+
use Symfony\Component\Config\Loader\LoaderInterface;
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\HttpFoundation\JsonResponse;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpKernel\Kernel;
9+
use Symfony\Component\Routing\RouteCollectionBuilder;
10+
11+
// require Composer's autoloader
12+
require __DIR__.'/../vendor/autoload.php';
13+
14+
class AppKernel extends Kernel
15+
{
16+
use MicroKernelTrait;
17+
18+
public function registerBundles()
19+
{
20+
return [
21+
new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
22+
];
23+
}
24+
25+
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
26+
{
27+
$c->loadFromExtension('framework', ['secret' => 'S0ME_SECRET']);
28+
$loader->load(__DIR__.'/config.yml');
29+
$loader->load(__DIR__.'/services.xml');
30+
}
31+
32+
protected function configureRoutes(RouteCollectionBuilder $routes)
33+
{
34+
// kernel is a service that points to this class
35+
// optional 3rd argument is the route name
36+
$routes->add('/echo', 'kernel:echoAction')->setDefault('_jsonSchema', ['request' => 'schema.json']);
37+
$routes->add('/exception', 'kernel:exceptionAction');
38+
}
39+
40+
public function echoAction(Request $request)
41+
{
42+
return new JsonResponse($request->request->all() + $request->attributes->all());
43+
}
44+
45+
public function exceptionAction(Request $request)
46+
{
47+
if ($request->query->has('supported')) {
48+
throw new \Rezzza\SymfonyRestApiJson\Tests\Fixtures\MyOtherException('This is my app message');
49+
}
50+
51+
throw new \RuntimeException('Something wrong');
52+
}
53+
}
54+
55+
$kernel = new AppKernel('dev', true);
56+
$request = Request::createFromGlobals();
57+
$response = $kernel->handle($request);
58+
$response->send();
59+
$kernel->terminate($request, $response);

testapp/schema.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {
6+
"name": {
7+
"type": "string",
8+
"minLength": 1
9+
}
10+
},
11+
"required": [
12+
"name"
13+
]
14+
}

testapp/services.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service id="json_body_listener.ui" class="Rezzza\SymfonyRestApiJson\JsonBodyListener">
9+
<argument type="service">
10+
<service class="Rezzza\SymfonyRestApiJson\PayloadValidator">
11+
<argument type="service">
12+
<service class="Rezzza\SymfonyRestApiJson\JsonSchemaTools" />
13+
</argument>
14+
</service>
15+
</argument>
16+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="24" />
17+
</service>
18+
19+
<service id="link_request_listener.ui" class="Rezzza\SymfonyRestApiJson\LinkRequestListener">
20+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="10" />
21+
</service>
22+
23+
<service id="json_exception_handler.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionHandler">
24+
<argument type="service">
25+
<service class="Rezzza\SymfonyRestApiJson\ExceptionHttpCodeMap">
26+
<argument>%exception_http_code_map%</argument>
27+
</service>
28+
</argument>
29+
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="32" />
30+
</service>
31+
32+
<service id="json_exception_controller.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionController">
33+
<argument>%kernel.debug%</argument>
34+
<argument>%show_exception_token%</argument>
35+
</service>
36+
37+
<service id="ui.event_listener.exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
38+
<tag name="kernel.event_subscriber" />
39+
<tag name="monolog.logger" channel="request" />
40+
<argument>json_exception_controller.ui:showException</argument>
41+
<argument type="service" id="logger" on-invalid="null" />
42+
</service>
43+
</services>
44+
</container>

0 commit comments

Comments
 (0)