Skip to content
This repository was archived by the owner on Feb 7, 2018. It is now read-only.

Add behat as integration test tool #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ foreach($controllers as $controller) {
```sh
composer install --dev
php vendor/bin/phpunit
./vendor/bin/behat
```

## Contributors
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"symfony/filesystem": "~2.6"
},
"require-dev": {
"phpunit/phpunit": "4.*"
"phpunit/phpunit": "4.*",
"symfony/event-dispatcher": "~2.6,>=2.6.7",
"behat/behat": "^3.0",
"beberlei/assert": "^2.4"
},
"autoload": {
"psr-4": {
Expand Down
105 changes: 105 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use Behat\Testwork\Hook\Scope\AfterSuiteScope;
use Assert\Assertion;
use Symfony\Component\Filesystem\Filesystem;
use Creads\Api2Symfony\Converter\RamlConverter;
use Creads\Api2Symfony\Dumper\SymfonyDumper;

/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context, SnippetAcceptingContext
{
const GENERATED_DIR = 'features/generated';
const SPECIFICATION_DIR = 'features/specification';

/**
* @var RamlConverter
*/
private $converter;

/**
* @var SymfonyDumper
*/
private $dumper;

/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
$this->converter = new RamlConverter();

$this->dumper = new SymfonyDumper();
}

/** @BeforeSuite */
public static function setup(BeforeSuiteScope $event)
{
$fs = new Filesystem();

$fs->remove(self::GENERATED_DIR);

$fs->mkdir(self::GENERATED_DIR);
}

/** @AfterSuite */
public static function teardown(AfterSuiteScope $event)
{
$fs = new Filesystem();

$fs->remove(self::GENERATED_DIR);
}

/**
* @Given I have :filename
*/
public function iHave($filename)
{
$this->filename = $filename;
}

/**
* @Given I want to use :namespace as namespace
*/
public function iWantToUseAsNamespace($namespace)
{
$this->namespace = $namespace;
}

/**
* @When I generate
*/
public function iGenerate()
{
//get controller models from specification
$controllers = $this->converter->convert(self::SPECIFICATION_DIR . '/' . $this->filename, $this->namespace);

//dump each controller into current directory
foreach($controllers as $controller) {
$this->dumper->dump($controller, self::GENERATED_DIR);
}
}

/**
* @Then I should get :pathfile file generated with given content:
*/
public function iShouldGetFileGeneratedWithGivenContent($pathfile, PyStringNode $content)
{
$pathfile = self::GENERATED_DIR . '/' . $pathfile;

Assertion::file($pathfile);
Assertion::readable($pathfile);
Assertion::same(file_get_contents($pathfile), (string)$content);
}
}
50 changes: 50 additions & 0 deletions features/core.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Feature: Generate automatically Symfony controllers
In order to make my life easier
As a developer
I need to generate my controllers automatically based on specification file

Scenario: Generate a simple controller
Given I have "simple.raml"
And I want to use "Foo\Bar" as namespace
When I generate
Then I should get "PostsController.php" file generated with given content:
"""
<?php

namespace Foo\Bar;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
* This class has been auto-generated by Api2Symfony: https://github.com/creads/api2symfony
*/
class PostsController extends Controller
{
/**
* Get a specific coment
*
* @Route("/posts/{slug}/comments/{page}/{id}", name="get_posts_slug_page_id")
* @Method({"GET"})
*/
public function getPostsSlugPageIdAction(Request $request, $slug, $page, $id)
{
/* You can provide a _code query parameter to get whatever response code you want */
/* If your api doesn't handle the requested response code, the first response available is returned */
/* You can provide a _content_type query parameter to get whatever response content type you want */
return new Response(
'',
,
array(
)
);
}

}

"""

9 changes: 9 additions & 0 deletions features/specification/simple.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#%RAML 0.8
title: uri parameters

/posts:
/{slug}:
/comments/{page}:
/{id}:
get:
description: Get a specific coment
4 changes: 2 additions & 2 deletions src/Resources/templates/symfony/controller.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
* {{ controller.description }}
*
{% if controller.description %}* {{ controller.description }}
*{% endif %}
* This class has been auto-generated by Api2Symfony: https://github.com/creads/api2symfony
*/
class {{ controller.name }} extends Controller
Expand Down
50 changes: 25 additions & 25 deletions src/Resources/templates/symfony/response.php.twig
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
{% autoescape false %}
/* You can provide a _content_type query parameter to get whatever response content type you want */
/* You can provide a _content_type query parameter to get whatever response content type you want */
{% for content in response.contents %}
if ('{{ content.type }}' == $this->get('request')->query->get('_content_type', null)) {
return new Response(
'{{ content.content|raw }}',
{{ response.code }},
array(
{% include 'key_value.php.twig' with { 'key' : 'Content-Type', 'value' : content.type } %}{{ ',' }}
{% for key, value in response.headers %}
{% include 'key_value.php.twig' with { 'key' : key, 'value' : value } %}{% if loop.last == false %},{% endif %}

{% endfor %}
)
);
}
{% endfor %}
{% if response.contents is empty %}
if ('{{ content.type }}' == $this->get('request')->query->get('_content_type', null)) {
return new Response(
'',
'{{ content.content|raw }}',
{{ response.code }},
array(
{% include 'key_value.php.twig' with { 'key' : 'Content-Type', 'value' : content.type } %}{{ ',' }}
{% for key, value in response.headers %}
{% include 'key_value.php.twig' with { 'key' : key, 'value' : value } %}{% if loop.last == false %},{% endif %}

{% endfor %}
)
);
}
{% endfor %}
{% if response.contents is empty %}
return new Response(
'',
{{ response.code }},
array(
{% for key, value in response.headers %}
{% include 'key_value.php.twig' with { 'key' : key, 'value' : value } %}{% if loop.last == false %},{% endif %}

{% endfor %}
)
);
{% else %}
return new Response(
'{{ response.contents[0].content|raw }}',
{{ response.code }},
array(
{% include 'key_value.php.twig' with { 'key' : 'Content-Type', 'value' : response.contents[0].type } %}{{ ',' }}
return new Response(
'{{ response.contents[0].content|raw }}',
{{ response.code }},
array(
{% include 'key_value.php.twig' with { 'key' : 'Content-Type', 'value' : response.contents[0].type } %}{{ ',' }}
{% for key, value in response.headers %}
{% include 'key_value.php.twig' with { 'key' : key, 'value' : value } %}{% if loop.last == false %},{% endif %}
{% include 'key_value.php.twig' with { 'key' : key, 'value' : value } %}{% if loop.last == false %},{% endif %}

{% endfor %}
)
);
)
);
{% endif %}
{% endautoescape %}