Skip to content

Commit 549fcd6

Browse files
authored
Add FT (#5)
1 parent 4d420b2 commit 549fcd6

30 files changed

+915
-38
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/

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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
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
},
28-
"suggest": {
29-
"yoanm/symfony-jsonrpc-server-psr11-resolver": ""
30-
},
3129
"require": {
3230
"php": ">=7.1",
3331
"yoanm/jsonrpc-server-sdk": "dev-release/3.0.0",
3432
"yoanm/jsonrpc-server-doc-sdk": "dev-release/1.0.0",
3533
"yoanm/symfony-jsonrpc-http-server": "dev-release/2.0.0",
36-
"symfony/http-foundation": "^3.0 || ^4.0",
3734
"symfony/http-kernel": "^3.0 || ^4.0",
3835
"symfony/config": "^3.0 || ^4.0",
3936
"symfony/dependency-injection": "^3.0 || ^4.0"
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
namespace Tests\Functional\BehatContext;
3+
4+
use Behat\Behat\Context\Context;
5+
use Behat\Gherkin\Node\PyStringNode;
6+
use DemoApp\AbstractKernel;
7+
use DemoApp\DefaultKernel;
8+
use DemoApp\KernelWithMethodDocCreatedListener;
9+
use DemoApp\KernelWithServerDocCreatedListener;
10+
use PHPUnit\Framework\Assert;
11+
use Symfony\Component\HttpFoundation\Request;
12+
use Symfony\Component\HttpFoundation\Response;
13+
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
14+
15+
/**
16+
* Defines application features from the specific context.
17+
*/
18+
class DemoAppContext implements Context
19+
{
20+
/** @var Response|null */
21+
private $lastResponse;
22+
23+
/** @var bool */
24+
private $useKernelWithMethodDocCreatedListener = false;
25+
/** @var bool */
26+
private $useKernelWithServerDocCreatedListener = false;
27+
28+
/**
29+
* @Given I will use kernel with MethodDocCreated listener
30+
*/
31+
public function givenIWillUseMethodDocCreatedListener()
32+
{
33+
$this->useKernelWithMethodDocCreatedListener = true;
34+
}
35+
36+
/**
37+
* @Given I will use kernel with ServerDocCreated listener
38+
*/
39+
public function givenIWillUseServerDocCreatedListener()
40+
{
41+
$this->useKernelWithServerDocCreatedListener = true;
42+
}
43+
44+
/**
45+
* @When I send a :httpMethod request on :uri demoApp kernel endpoint
46+
* @When I send following :httpMethod input on :uri demoApp kernel endpoint:
47+
*/
48+
public function whenISendFollowingPayloadToDemoApp($httpMethod, $uri, PyStringNode $payload = null)
49+
{
50+
$this->lastResponse = null;
51+
52+
$kernel = $this->getDemoAppKernel();
53+
$kernel->boot();
54+
$request = Request::create($uri, $httpMethod, [], [], [], [], $payload ? $payload->getRaw() : null);
55+
$this->lastResponse = $kernel->handle($request);
56+
$kernel->terminate($request, $this->lastResponse);
57+
$kernel->shutdown();
58+
}
59+
60+
/**
61+
* @Then I should have a :httpCode response from demoApp with following content:
62+
*/
63+
public function thenIShouldHaveAResponseFromDemoAppWithFollowingContent($httpCode, PyStringNode $payload)
64+
{
65+
Assert::assertInstanceOf(Response::class, $this->lastResponse);
66+
// Decode payload to get ride of indentation, spacing, etc
67+
Assert::assertEquals(
68+
json_decode($payload->getRaw(), true),
69+
json_decode($this->lastResponse->getContent(), true)
70+
);
71+
Assert::assertSame((int) $httpCode, $this->lastResponse->getStatusCode());
72+
}
73+
74+
/**
75+
* @Then Collector should have :methodClass JSON-RPC method with name :methodName
76+
*/
77+
public function thenCollectorShouldHaveAMethodWithName($methodClass, $methodName)
78+
{
79+
$kernel = $this->getDemoAppKernel();
80+
$kernel->boot();
81+
$mappingList = $kernel->getContainer()
82+
->get('mapping_aware_service')
83+
->getMappingList()
84+
;
85+
$kernel->shutdown();
86+
87+
if (!isset($mappingList[$methodName])) {
88+
throw new \Exception(sprintf('No mapping defined to method name "%s"', $methodName));
89+
}
90+
$method = $mappingList[$methodName];
91+
92+
Assert::assertInstanceOf(
93+
JsonRpcMethodInterface::class,
94+
$method,
95+
'Method must be a JsonRpcMethodInterface instance'
96+
);
97+
Assert::assertInstanceOf(
98+
$methodClass,
99+
$method,
100+
sprintf('Method "%s" is not an instance of "%s"', $methodName, $methodClass)
101+
);
102+
}
103+
104+
/**
105+
* @return AbstractKernel
106+
*/
107+
protected function getDemoAppKernel()
108+
{
109+
$env = 'prod';
110+
$debug = true;
111+
112+
if (true === $this->useKernelWithMethodDocCreatedListener) {
113+
$kernelClass = KernelWithMethodDocCreatedListener::class;
114+
} elseif (true === $this->useKernelWithServerDocCreatedListener) {
115+
$kernelClass = KernelWithServerDocCreatedListener::class;
116+
} else {
117+
$kernelClass = DefaultKernel::class;
118+
}
119+
120+
return new $kernelClass($env, $debug);
121+
}
122+
}

features/bootstrap/FeatureContext.php

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

0 commit comments

Comments
 (0)