Skip to content

Commit

Permalink
Merge pull request #11 from Leosten/feature/27411
Browse files Browse the repository at this point in the history
Ajout de steps génériques pour les test API
  • Loading branch information
faouzic committed May 3, 2016
2 parents 3e687b8 + 25100e3 commit 789a278
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 25 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ default:
paths:
- %paths.base%/Tests/Functional/features
contexts:
- MainContext
- ApiContext
- DoctrineContext:
- ETNA\FeatureContext\MainContext
- ETNA\FeatureContext\ApiContext
- ETNA\FeatureContext\DoctrineContext:
max_queries: 10
- ElasticContext
- FixedDateContext:
- ETNA\FeatureContext\ElasticContext
- ETNA\FeatureContext\FixedDateContext:
date: "2016-04-12 14:42:42"
- AuthContext
- TimeProfilerContext:
- ETNA\FeatureContext\AuthContext
- ETNA\FeatureContext\TimeProfilerContext:
max_time: 200
formatters:
progress:
Expand All @@ -51,15 +51,15 @@ ci:
suites:
default:
contexts:
- MainContext
- ApiContext
- DoctrineContext:
- ETNA\FeatureContext\MainContext
- ETNA\FeatureContext\ApiContext
- ETNA\FeatureContext\DoctrineContext:
max_queries: 10
- ElasticContext
- FixedDateContext:
- ETNA\FeatureContext\ElasticContext
- ETNA\FeatureContext\FixedDateContext:
date: "2016-04-12 14:42:42"
- AuthContext
- CoverageContext:
- ETNA\FeatureContext\AuthContext
- ETNA\FeatureContext\CoverageContext:
coverage_path: /tmp/behat/coverage
whitelist:
- app
Expand Down
33 changes: 32 additions & 1 deletion src/contexts/ApiContext.php → src/ApiContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -83,7 +85,7 @@ function ($item) {
),
];

$this->getContext('DoctrineContext')->checkMaxQueries($method, $result);
$this->getContext('ETNA\FeatureContext\DoctrineContext')->checkMaxQueries($method, $result);

$this->response = $result;
}
Expand All @@ -100,6 +102,16 @@ public function leStatusHTTPDevraitEtre($code)
}
}

/**
* @Then /^le message HTTP devrait être "([^"]*)"$/
*/
public function leMessageHTTPDevraitEtre($mess)
{
if (trim($this->response['http_message']) != "$mess") {
throw new Exception("Bad message response message {$this->response['http_message']} != {$mess}");
}
}

/**
* @Then /^je devrais avoir un résultat d\'API en JSON$/
*/
Expand All @@ -119,6 +131,15 @@ public function jeDevraisAvoirUnResultatDApiEnJSON()
$this->data = $json;
}

/**
* @Given /^je devrais avoir un résultat d\'API en PDF$/
*/
public function jeDevraisAvoirUnResultatDApiEnPdf()
{
if ("application/pdf" !== $this->response["headers"]["content-type"]) {
throw new Exception("Invalid response type");
}
}

/**
* @Given /^je devrais avoir un résultat d\'API en CSV$/
Expand Down Expand Up @@ -199,4 +220,14 @@ public function leResultatDevraitRessemblerAuFichier($file)
$file = realpath($this->results_path . "/" . $file);
$this->leResultatDevraitRessemblerAuJsonSuivant(file_get_contents($file));
}

/**
* @Then /^je devrais avoir un objet comme résultat$/
*/
public function jeDevraisAvoirUnObjetCommeResultat()
{
if (!is_object($this->data)) {
throw new Exception("{$this->data} is not an object");
}
}
}
6 changes: 4 additions & 2 deletions src/contexts/AuthContext.php → src/AuthContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use ETNA\FeatureContext as EtnaFeatureContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\AfterStepScope;
Expand All @@ -14,7 +16,7 @@ public function getRequest(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

$this->request = $environment->getContext('ApiContext')->getRequest();
$this->request = $environment->getContext('ETNA\FeatureContext\ApiContext')->getRequest();
}

/**
Expand Down Expand Up @@ -50,7 +52,7 @@ public function setRequest(AfterStepScope $scope)
{
$environment = $scope->getEnvironment();

$environment->getContext('ApiContext')->setRequest($this->request);
$environment->getContext('ETNA\FeatureContext\ApiContext')->setRequest($this->request);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/contexts/BaseContext.php → src/BaseContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

Expand All @@ -20,7 +22,7 @@ public function setUp(BeforeScenarioScope $scope)
);

self::$contexts = $contexts;
self::$silex_app = $contexts['MainContext']->getSilexApp();
self::$silex_app = $contexts['ETNA\FeatureContext\MainContext']->getSilexApp();
}

/**
Expand All @@ -40,12 +42,12 @@ protected function getContext($context)
*/
protected function getParameter($name)
{
return self::$contexts['MainContext']->getParameter($name);
return self::$contexts['ETNA\FeatureContext\MainContext']->getParameter($name);
}

protected static function setParameter($name, $value)
{
self::$contexts['MainContext']->setParameter($name, $value);
self::$contexts['ETNA\FeatureContext\MainContext']->setParameter($name, $value);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/contexts/CoverageContext.php → src/CoverageContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Testwork\Hook\Scope\BeforeSuiteScope;

class CoverageContext extends BaseContext
Expand All @@ -21,7 +23,7 @@ public static function setUpCoverageParams(BeforeSuiteScope $scope)
{
$environment = $scope->getEnvironment();
$contexts_params = $environment->getContextClassesWithArguments();
self::$_coverage_params = $contexts_params['CoverageContext'];
self::$_coverage_params = $contexts_params['ETNA\FeatureContext\CoverageContext'];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/DoctrineContext.php → src/DoctrineContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Behat\Tester\Exception\PendingException;

class DoctrineContext extends BaseContext
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/ElasticContext.php → src/ElasticContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class ElasticContext extends BaseContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Behat\Event\SuiteEvent;

class FixedDateContext extends BaseContext
Expand Down
4 changes: 2 additions & 2 deletions src/contexts/MainContext.php → src/MainContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Behat\Context\Context;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;

Expand All @@ -10,8 +12,6 @@ class MainContext implements Context
static private $silex_app;
static private $parameters = [];

// use ETNA\FeatureContext\FixedTime;

/** @BeforeSuite */
public static function setUpSilexApp()
{
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/RabbitContext.php → src/RabbitContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

class RabbitContext extends BaseContext
{
private $vhosts = ["/test-behat"];
Expand Down
4 changes: 3 additions & 1 deletion src/contexts/ServerContext.php → src/ServerContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Testwork\Hook\Scope\BeforeSuiteScope;

class ServerContext extends BaseContext
Expand All @@ -23,7 +25,7 @@ public function __construct($url = null, $documentRoot = null, $timeout = null)
public static function setUp(BeforeSuiteScope $scope)
{
// Fetch config
$params = $scope->getEnvironment()->getContextClassesWithArguments()['ServerContext'];
$params = $scope->getEnvironment()->getContextClassesWithArguments()['ETNA\FeatureContext\ServerContext'];
$url = parse_url($params['url']);
$port = !empty($url['port']) ? $url['port'] : 80;

Expand Down
2 changes: 2 additions & 0 deletions src/contexts/SvnContext.php → src/SvnContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Symfony\Component\Filesystem\Filesystem;

class SvnContext extends BaseContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Tester\Exception\PendingException;
Expand Down Expand Up @@ -27,7 +29,7 @@ public function beginTimeProfiler()
*/
public function stopTimeProfiler(AfterScenarioScope $scope)
{
$has_coverage = $scope->getEnvironment()->hasContextClass("CoverageContext");
$has_coverage = $scope->getEnvironment()->hasContextClass("ETNA\FeatureContext\CoverageContext");
if (true === $has_coverage) {
return;
}
Expand Down

0 comments on commit 789a278

Please sign in to comment.