-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from tyx/feature/add-behat-tests
👮 Add behat tests
- Loading branch information
Showing
10 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |