Skip to content

Commit 47c396c

Browse files
authored
Add bundle (#4)
1 parent 0224153 commit 47c396c

File tree

27 files changed

+484
-113
lines changed

27 files changed

+484
-113
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"symfony/dependency-injection": "^3.0 || ^4.0",
3535
"yoanm/jsonrpc-server-sdk-psr11-resolver": "^2.0"
3636
},
37+
"suggest": {
38+
"symfony/http-kernel": "To ease integration inside your application by using JsonRpcHttpServerBundle"
39+
},
3740
"require-dev": {
3841
"behat/behat": "~3.0",
3942
"squizlabs/php_codesniffer": "3.*",

features/bootstrap/DemoAppContext.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
use Behat\Behat\Context\Context;
55
use Behat\Gherkin\Node\PyStringNode;
6-
use DemoApp\BaseKernel;
6+
use DemoApp\AbstractKernel;
77
use DemoApp\DefaultKernel;
8+
use DemoApp\KernelWithBundle;
9+
use DemoApp\KernelWithBundleAndCustomResolver;
810
use DemoApp\KernelWithCustomResolver;
911
use PHPUnit\Framework\Assert;
1012
use Symfony\Component\HttpFoundation\Request;
@@ -19,6 +21,8 @@ class DemoAppContext implements Context
1921
private $lastResponse;
2022
/** @var bool */
2123
private $useCustomResolver = false;
24+
/** @var bool */
25+
private $useBundle = false;
2226

2327
/**
2428
* @Given I use my DemoApp custom method resolver
@@ -28,6 +32,14 @@ public function givenIUseMyDemoAppCustomMethodResolve()
2832
$this->useCustomResolver = true;
2933
}
3034

35+
/**
36+
* @Given DemoApp will use JsonRpcHttpServerBundle
37+
*/
38+
public function givenDemoAppWillUseBundle()
39+
{
40+
$this->useBundle = true;
41+
}
42+
3143
/**
3244
* @When I send following :httpMethod input on :uri demoApp kernel endpoint:
3345
*/
@@ -37,9 +49,10 @@ public function whenISendFollowingPayloadToDemoApp($httpMethod, $uri, PyStringNo
3749

3850
$kernel = $this->getDemoAppKernel();
3951
$kernel->boot();
40-
$this->lastResponse = $kernel->handle(
41-
Request::create($uri, $httpMethod, [], [], [], [], $payload->getRaw())
42-
);
52+
$request = Request::create($uri, $httpMethod, [], [], [], [], $payload->getRaw());
53+
$this->lastResponse = $kernel->handle($request);
54+
$kernel->terminate($request, $this->lastResponse);
55+
$kernel->shutdown();
4356
}
4457

4558
/**
@@ -57,14 +70,21 @@ public function thenIShouldHaveAResponseFromDemoAppWithFollowingContent($httpCod
5770
}
5871

5972
/**
60-
* @return BaseKernel
73+
* @return AbstractKernel
6174
*/
6275
protected function getDemoAppKernel()
6376
{
64-
if (true === $this->useCustomResolver) {
65-
return new KernelWithCustomResolver('prod', true);
77+
$env = 'prod';
78+
$debug = true;
79+
switch (true) {
80+
case true === $this->useBundle && true === $this->useCustomResolver:
81+
return new KernelWithBundleAndCustomResolver($env, $debug);
82+
case true === $this->useBundle && false === $this->useCustomResolver:
83+
return new KernelWithBundle($env, $debug);
84+
case false === $this->useBundle && true === $this->useCustomResolver:
85+
return new KernelWithCustomResolver($env, $debug);
6686
}
6787

68-
return new DefaultKernel('prod', true);
88+
return new DefaultKernel($env, $debug);
6989
}
7090
}

features/demo-app.feature

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,172 @@
11
Feature: demo symfony application
22

3-
Scenario: Default method resolver with JSON-RPC method tags
3+
@symfony-extension @symfony-jsonrpc-method-tag
4+
Scenario: Use Extension with default method resolver with JSON-RPC method tags
45
# Ensure the two methods with tag have been loaded
56
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
67
"""
7-
{"jsonrpc": "2.0", "method": "methodA", "id": 1}
8+
{"jsonrpc": "2.0", "method": "defaultMethodA", "id": 1}
89
"""
910
Then I should have a "200" response from demoApp with following content:
1011
"""
1112
{"jsonrpc":"2.0", "result":"MethodA", "id":1}
1213
"""
1314
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
1415
"""
15-
{"jsonrpc": "2.0", "method": "MethodB", "id": 2}
16+
{"jsonrpc": "2.0", "method": "defaultMethodAAlias", "id": 2}
1617
"""
1718
Then I should have a "200" response from demoApp with following content:
1819
"""
19-
{"jsonrpc":"2.0", "result":"MethodB", "id":2}
20+
{"jsonrpc":"2.0", "result":"MethodA", "id":2}
21+
"""
22+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
23+
"""
24+
{"jsonrpc": "2.0", "method": "defaultMethodB", "id": 3}
25+
"""
26+
Then I should have a "200" response from demoApp with following content:
27+
"""
28+
{"jsonrpc":"2.0", "result":"MethodB", "id":3}
29+
"""
30+
31+
@symfony-extension
32+
Scenario: Use Extension with default method resolver with JSON-RPC methods container injection
33+
# Ensure the two injected methods have been injected
34+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
35+
"""
36+
{"jsonrpc": "2.0", "method": "defaultGetDummy", "id": 1}
37+
"""
38+
Then I should have a "200" response from demoApp with following content:
39+
"""
40+
{"jsonrpc":"2.0", "result":"MethodC", "id":1}
41+
"""
42+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
43+
"""
44+
{"jsonrpc": "2.0", "method": "defaultGetAnotherDummy", "id": 2}
45+
"""
46+
Then I should have a "200" response from demoApp with following content:
47+
"""
48+
{"jsonrpc":"2.0", "result":"MethodD", "id":2}
2049
"""
2150

22-
Scenario: Default method resolver with JSON-RPC methods container injection
23-
# Ensure the two injected methods have been loaded
51+
@symfony-extension @symfony-method-resolver-tag
52+
Scenario: Use Extension with custom method resolver
53+
Given I use my DemoApp custom method resolver
54+
# Ensure all methods have been loaded
55+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
56+
"""
57+
{"jsonrpc": "2.0", "method": "customMethodA", "id": 1}
58+
"""
59+
Then I should have a "200" response from demoApp with following content:
60+
"""
61+
{"jsonrpc":"2.0", "result":"MethodA", "id":1}
62+
"""
63+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
64+
"""
65+
{"jsonrpc": "2.0", "method": "customMethodB", "id": 2}
66+
"""
67+
Then I should have a "200" response from demoApp with following content:
68+
"""
69+
{"jsonrpc":"2.0", "result":"MethodB", "id":2}
70+
"""
2471
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
2572
"""
26-
{"jsonrpc": "2.0", "method": "getDummy", "id": 3}
73+
{"jsonrpc": "2.0", "method": "customMethodC", "id": 3}
2774
"""
2875
Then I should have a "200" response from demoApp with following content:
2976
"""
3077
{"jsonrpc":"2.0", "result":"MethodC", "id":3}
3178
"""
3279
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
3380
"""
34-
{"jsonrpc": "2.0", "method": "getAnotherDummy", "id": 4}
81+
{"jsonrpc": "2.0", "method": "customMethodD", "id": 4}
3582
"""
3683
Then I should have a "200" response from demoApp with following content:
3784
"""
3885
{"jsonrpc":"2.0", "result":"MethodD", "id":4}
3986
"""
4087

41-
Scenario: custom method resolver
42-
Given I use my DemoApp custom method resolver
88+
@symfony-bundle @symfony-jsonrpc-method-tag
89+
Scenario: Use Bundle with default method resolver with JSON-RPC method tags
90+
Given DemoApp will use JsonRpcHttpServerBundle
91+
# Ensure the two methods with tag have been loaded
92+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
93+
"""
94+
{"jsonrpc": "2.0", "method": "bundledMethodA", "id": 1}
95+
"""
96+
Then I should have a "200" response from demoApp with following content:
97+
"""
98+
{"jsonrpc":"2.0", "result":"MethodA", "id":1}
99+
"""
100+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
101+
"""
102+
{"jsonrpc": "2.0", "method": "bundledMethodAAlias", "id": 2}
103+
"""
104+
Then I should have a "200" response from demoApp with following content:
105+
"""
106+
{"jsonrpc":"2.0", "result":"MethodA", "id":2}
107+
"""
108+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
109+
"""
110+
{"jsonrpc": "2.0", "method": "bundledMethodB", "id": 3}
111+
"""
112+
Then I should have a "200" response from demoApp with following content:
113+
"""
114+
{"jsonrpc":"2.0", "result":"MethodB", "id":3}
115+
"""
116+
117+
@symfony-bundle
118+
Scenario: Use Bundle with default method resolver with JSON-RPC methods container injection
119+
Given DemoApp will use JsonRpcHttpServerBundle
120+
# Ensure the two injected methods have been injected
121+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
122+
"""
123+
{"jsonrpc": "2.0", "method": "bundledGetDummy", "id": 1}
124+
"""
125+
Then I should have a "200" response from demoApp with following content:
126+
"""
127+
{"jsonrpc":"2.0", "result":"MethodC", "id":1}
128+
"""
129+
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
130+
"""
131+
{"jsonrpc": "2.0", "method": "bundledGetAnotherDummy", "id": 2}
132+
"""
133+
Then I should have a "200" response from demoApp with following content:
134+
"""
135+
{"jsonrpc":"2.0", "result":"MethodD", "id":2}
136+
"""
137+
138+
@symfony-bundle @symfony-method-resolver-tag
139+
Scenario: Bundle with custom method resolver tag
140+
Given DemoApp will use JsonRpcHttpServerBundle
141+
And I use my DemoApp custom method resolver
43142
# Ensure all methods have been loaded
44143
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
45144
"""
46-
{"jsonrpc": "2.0", "method": "custom_methodA", "id": 1}
145+
{"jsonrpc": "2.0", "method": "customBundledMethodA", "id": 1}
47146
"""
48147
Then I should have a "200" response from demoApp with following content:
49148
"""
50149
{"jsonrpc":"2.0", "result":"MethodA", "id":1}
51150
"""
52151
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
53152
"""
54-
{"jsonrpc": "2.0", "method": "custom_methodB", "id": 2}
153+
{"jsonrpc": "2.0", "method": "customBundledMethodB", "id": 2}
55154
"""
56155
Then I should have a "200" response from demoApp with following content:
57156
"""
58157
{"jsonrpc":"2.0", "result":"MethodB", "id":2}
59158
"""
60159
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
61160
"""
62-
{"jsonrpc": "2.0", "method": "custom_methodC", "id": 3}
161+
{"jsonrpc": "2.0", "method": "customBundledMethodC", "id": 3}
63162
"""
64163
Then I should have a "200" response from demoApp with following content:
65164
"""
66165
{"jsonrpc":"2.0", "result":"MethodC", "id":3}
67166
"""
68167
When I send following "POST" input on "/my-json-rpc-endpoint" demoApp kernel endpoint:
69168
"""
70-
{"jsonrpc": "2.0", "method": "custom_methodD", "id": 4}
169+
{"jsonrpc": "2.0", "method": "customBundledMethodD", "id": 4}
71170
"""
72171
Then I should have a "200" response from demoApp with following content:
73172
"""

features/demo_app/config/custom_method_resolver.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
framework:
22
secret: '%env(APP_SECRET)%'
3-
4-
yoanm_jsonrpc_http_server: ~

features/demo_app/config/services.yaml renamed to features/demo_app/default_config/services.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ services:
55
class: DemoApp\Method\MethodA
66
public: true
77
tags:
8-
- { name: 'yoanm.jsonrpc_http_server.jsonrpc_method', method: 'methodA' }
9-
- { name: 'yoanm.jsonrpc_http_server.jsonrpc_method', method: 'methodAAlias' }
8+
- { name: 'yoanm.jsonrpc_http_server.jsonrpc_method', method: 'defaultMethodA' }
9+
- { name: 'yoanm.jsonrpc_http_server.jsonrpc_method', method: 'defaultMethodAAlias' }
1010
# Configure Json-RPC method MethodB with tag also
1111
jsonrpc.method.b:
1212
class: DemoApp\Method\MethodB
1313
public: true
1414
tags:
15-
- { name: 'yoanm.jsonrpc_http_server.jsonrpc_method', method: 'MethodB' }
15+
- { name: 'yoanm.jsonrpc_http_server.jsonrpc_method', method: 'defaultMethodB' }
1616

1717
# Configure Json-RPC method MethodC as simple service, it will be injected on container building
1818
jsonrpc.method.c:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
return [
3+
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
4+
Yoanm\SymfonyJsonRpcHttpServer\Infra\Symfony\JsonRpcHttpServerBundle::class => ['all' => true],
5+
];
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
framework:
2+
secret: '%env(APP_SECRET)%'

0 commit comments

Comments
 (0)