Skip to content

Commit 8bdd033

Browse files
authored
Add FT & UT (#4)
1 parent b231587 commit 8bdd033

33 files changed

+1150
-90
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ vendor
22
build
33
#A library must not provide a composer.lock file
44
composer.lock
5+
6+
features/demo_app/var

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ before_install:
2525
- phpenv config-rm xdebug.ini || true
2626

2727
install:
28-
- composer require symfony/http-foundation:$SYMFONY_VERSION symfony/http-kernel:$SYMFONY_VERSION symfony/config:$SYMFONY_VERSION symfony/dependency-injection:$SYMFONY_VERSION
28+
- composer require symfony/http-kernel:$SYMFONY_VERSION symfony/dependency-injection:$SYMFONY_VERSION
2929
- make build
3030
script:
3131
- make test-technical

behat.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ default:
22
suites:
33
default:
44
contexts:
5-
- Tests\Functional\BehatContext\FeatureContext: ~
5+
- Tests\Functional\BehatContext\DemoAppContext: ~

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
"autoload-dev": {
2323
"psr-4": {
2424
"Tests\\": "tests",
25-
"Tests\\Functional\\BehatContext\\": "features/bootstrap"
25+
"Tests\\Functional\\BehatContext\\": "features/bootstrap",
26+
"DemoApp\\": "features/demo_app/src"
2627
}
2728
},
2829
"require": {
2930
"php": ">=7.1",
3031
"yoanm/jsonrpc-server-doc-sdk": "dev-release/1.0.0",
3132
"yoanm/jsonrpc-http-server-swagger-doc-sdk": "dev-release/1.0.0",
3233
"yoanm/symfony-jsonrpc-http-server-doc": "dev-release/0.1.0",
33-
"symfony/http-foundation": "^3.0 || ^4.0",
3434
"symfony/http-kernel": "^3.0 || ^4.0",
35-
"symfony/config": "^3.0 || ^4.0",
3635
"symfony/dependency-injection": "^3.0 || ^4.0"
3736
},
3837
"require-dev": {
@@ -41,7 +40,7 @@
4140
"phpunit/phpunit": "^6.0 || ^7.0",
4241
"matthiasnoback/symfony-dependency-injection-test": "^2.0 || ^3.0",
4342
"matthiasnoback/symfony-config-test": "^3.0 || ^4.0",
44-
"symfony/framework-bundle": "^3.4",
43+
"symfony/framework-bundle": "^3.0 || ^4.0",
4544
"yoanm/php-unit-extended": "~1.0"
4645
}
4746
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Tests\Functional\BehatContext;
3+
4+
use Behat\Behat\Context\Context;
5+
6+
class AbstractContext implements Context
7+
{
8+
protected function jsonDecode($encodedData)
9+
{
10+
$decoded = json_decode($encodedData, true);
11+
12+
if (JSON_ERROR_NONE != json_last_error()) {
13+
throw new \Exception(
14+
json_last_error_msg(),
15+
json_last_error()
16+
);
17+
}
18+
19+
return $decoded;
20+
}
21+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
namespace Tests\Functional\BehatContext;
3+
4+
use Behat\Gherkin\Node\PyStringNode;
5+
use DemoApp\AbstractKernel;
6+
use DemoApp\DefaultKernel;
7+
use DemoApp\KernelWithDocCreatedListener;
8+
use PHPUnit\Framework\Assert;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
/**
13+
* Defines application features from the specific context.
14+
*/
15+
class DemoAppContext extends AbstractContext
16+
{
17+
/** @var Response|null */
18+
private $lastResponse;
19+
20+
/** @var bool */
21+
private $useKernelWithDocCreatedListener = false;
22+
23+
/**
24+
* @Given I will use kernel with MethodDocCreated listener
25+
*/
26+
public function givenIWillUseMethodDocCreatedListener()
27+
{
28+
$this->useKernelWithMethodDocCreatedListener = true;
29+
}
30+
31+
/**
32+
* @Given I will use kernel with DocCreated listener
33+
*/
34+
public function givenIWillUseKernelWithDocCreatedListener()
35+
{
36+
$this->useKernelWithDocCreatedListener = true;
37+
}
38+
39+
/**
40+
* @When I send a :httpMethod request on :uri demoApp kernel endpoint
41+
* @When I send following :httpMethod input on :uri demoApp kernel endpoint:
42+
*/
43+
public function whenISendFollowingPayloadToDemoApp($httpMethod, $uri, PyStringNode $payload = null)
44+
{
45+
$this->lastResponse = null;
46+
47+
$kernel = $this->getDemoAppKernel();
48+
$kernel->boot();
49+
$request = Request::create($uri, $httpMethod, [], [], [], [], $payload ? $payload->getRaw() : null);
50+
$this->lastResponse = $kernel->handle($request);
51+
$kernel->terminate($request, $this->lastResponse);
52+
$kernel->shutdown();
53+
}
54+
55+
/**
56+
* @Then I should have a :httpCode response from demoApp with following content:
57+
*/
58+
public function thenIShouldHaveAResponseFromDemoAppWithFollowingContent($httpCode, PyStringNode $payload)
59+
{
60+
Assert::assertInstanceOf(Response::class, $this->lastResponse);
61+
// Decode payload to get ride of indentation, spacing, etc
62+
Assert::assertEquals(
63+
$this->jsonDecode($payload->getRaw()),
64+
$this->jsonDecode($this->lastResponse->getContent())
65+
);
66+
Assert::assertSame((int) $httpCode, $this->lastResponse->getStatusCode());
67+
}
68+
69+
/**
70+
* @return AbstractKernel
71+
*/
72+
protected function getDemoAppKernel()
73+
{
74+
$env = 'prod';
75+
$debug = true;
76+
77+
if (true === $this->useKernelWithDocCreatedListener) {
78+
$kernelClass = KernelWithDocCreatedListener::class;
79+
} else {
80+
$kernelClass = DefaultKernel::class;
81+
}
82+
83+
return new $kernelClass($env, $debug);
84+
}
85+
}

features/bootstrap/FeatureContext.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)