Skip to content

Commit

Permalink
Merge pull request #13 from tyx/feature/add-behat-tests
Browse files Browse the repository at this point in the history
👮 Add behat tests
  • Loading branch information
tyx authored Aug 19, 2016
2 parents 671c6c2 + 0fc02e2 commit f9ca946
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
composer.lock
cache
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ php:
- 7.0

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

script:
- vendor/bin/atoum -ulr
- vendor/bin/behat -f progress
12 changes: 12 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
default:
suites:
web:
contexts:
- Rezzza\RestApiBehatExtension\RestApiContext
- Rezzza\RestApiBehatExtension\Json\JsonContext

extensions:
Rezzza\RestApiBehatExtension\Extension:
rest:
base_url: http://localhost:4224
store_response: true
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
"fzaninotto/faker": "^1.5",
"league/tactician-bundle": "^0.4.1",
"symfony/serializer": "^3.0",
"symfony/validator": "^3.0"
"symfony/validator": "^3.0",
"symfony/framework-bundle": "^3.0",
"rezzza/rest-api-behat-extension": "^5.0",
"php-http/curl-client": "^1.5",
"guzzlehttp/psr7": "^1.3"
},
"suggest": {
"league/tactician-bundle": "Support for ValidatorMiddleware of TacticianBundle",
Expand Down
15 changes: 15 additions & 0 deletions features/handle_exception.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Handle exception

In order to let our application throw specific exception
As a developper
I map these exceptions to http status code

Scenario: Throw mapped exception
When I send a POST request to "/exception?supported"
Then the response status code should be 409
And the JSON node "errors.message" should be equal to "This is my app message"

Scenario: Throw unmapped exception
When I send a POST request to "/exception"
Then the response status code should be 500
And the JSON node "exception[0].message" should be equal to "Something wrong"
49 changes: 49 additions & 0 deletions features/handle_payload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Feature: Handle json payload

In order to benefit of complex json payload
As a developper
I dump json payload into request attributes

Scenario: Send valid json
Given I set "Content-Type" header equal to "application/json"
When I send a POST request to "/echo" with body:
"""
{
"name": "Bond"
}
"""
Then the response should be in JSON
And the JSON node "name" should be equal to "Bond"

Scenario: Send invalid json
Given I set "Content-Type" header equal to "application/json"
When I send a POST request to "/echo" with body:
"""
{
"name": "Bond
}
"""
Then the response should be in JSON
And the response status code should be 400

Scenario: Send unsupported content-type
Given I set "Content-Type" header equal to "text/html"
When I send a POST request to "/echo" with body:
"""
{
"name": "Bond"
}
"""
Then the JSON node "name" should not exist

Scenario: Send invalid data
Given I set "Content-Type" header equal to "application/json"
When I send a POST request to "/echo" with body:
"""
{
"firstname": "James"
}
"""
Then the response status code should be 400
And the JSON node "errors[0].parameter" should be equal to "name"

4 changes: 4 additions & 0 deletions testapp/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
show_exception_token: s3s4m3
exception_http_code_map:
'Rezzza\SymfonyRestApiJson\Tests\Fixtures\MyOtherException': 409
59 changes: 59 additions & 0 deletions testapp/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

// require Composer's autoloader
require __DIR__.'/../vendor/autoload.php';

class AppKernel extends Kernel
{
use MicroKernelTrait;

public function registerBundles()
{
return [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
];
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
$c->loadFromExtension('framework', ['secret' => 'S0ME_SECRET']);
$loader->load(__DIR__.'/config.yml');
$loader->load(__DIR__.'/services.xml');
}

protected function configureRoutes(RouteCollectionBuilder $routes)
{
// kernel is a service that points to this class
// optional 3rd argument is the route name
$routes->add('/echo', 'kernel:echoAction')->setDefault('_jsonSchema', ['request' => 'schema.json']);
$routes->add('/exception', 'kernel:exceptionAction');
}

public function echoAction(Request $request)
{
return new JsonResponse($request->request->all() + $request->attributes->all());
}

public function exceptionAction(Request $request)
{
if ($request->query->has('supported')) {
throw new \Rezzza\SymfonyRestApiJson\Tests\Fixtures\MyOtherException('This is my app message');
}

throw new \RuntimeException('Something wrong');
}
}

$kernel = new AppKernel('dev', true);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
14 changes: 14 additions & 0 deletions testapp/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"minLength": 1
}
},
"required": [
"name"
]
}
44 changes: 44 additions & 0 deletions testapp/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="json_body_listener.ui" class="Rezzza\SymfonyRestApiJson\JsonBodyListener">
<argument type="service">
<service class="Rezzza\SymfonyRestApiJson\PayloadValidator">
<argument type="service">
<service class="Rezzza\SymfonyRestApiJson\JsonSchemaTools" />
</argument>
</service>
</argument>
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="24" />
</service>

<service id="link_request_listener.ui" class="Rezzza\SymfonyRestApiJson\LinkRequestListener">
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="10" />
</service>

<service id="json_exception_handler.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionHandler">
<argument type="service">
<service class="Rezzza\SymfonyRestApiJson\ExceptionHttpCodeMap">
<argument>%exception_http_code_map%</argument>
</service>
</argument>
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="32" />
</service>

<service id="json_exception_controller.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionController">
<argument>%kernel.debug%</argument>
<argument>%show_exception_token%</argument>
</service>

<service id="ui.event_listener.exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
<tag name="kernel.event_subscriber" />
<tag name="monolog.logger" channel="request" />
<argument>json_exception_controller.ui:showException</argument>
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>

0 comments on commit f9ca946

Please sign in to comment.