Skip to content
This repository was archived by the owner on Jan 12, 2020. It is now read-only.

Commit fce5544

Browse files
committed
Merge branch 'release/2.0.0'
2 parents aa5120a + 62a8759 commit fce5544

File tree

7 files changed

+139
-134
lines changed

7 files changed

+139
-134
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"require": {
3030
"php": ">7.0",
31-
"yoanm/jsonrpc-server-sdk": "~v1.0",
31+
"yoanm/jsonrpc-server-sdk": "~1.2",
3232
"psr/container": "^1.0"
3333
},
3434
"require-dev": {

features/bootstrap/FeatureContext.php

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@
99
use Psr\Container\ContainerInterface;
1010
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
1111
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface;
12-
use Yoanm\JsonRpcServerPsr11Resolver\App\Resolver\DefaultServiceNameResolver;
12+
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface;
1313
use Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver\ContainerMethodResolver;
1414

1515
/**
1616
* Defines application features from the specific context.
1717
*/
1818
class FeatureContext implements Context
1919
{
20-
/** @var ContainerMethodResolver */
21-
private $methodResolver;
22-
20+
/** @var string[] */
21+
private $serviceNameResolverMapping = [];
2322
/** @var ObjectProphecy[] */
24-
private $prophesizedMethodList = [];
23+
private $methodList = [];
2524
/** @var JsonRpcMethodInterface|ObjectProphecy|null */
2625
private $lastMethod;
2726
/** @var JsonRpcMethodNotFoundException|null */
@@ -42,48 +41,53 @@ class FeatureContext implements Context
4241
public function __construct()
4342
{
4443
$this->prophet = new Prophet();
45-
4644
$this->container = $this->prophet->prophesize(ContainerInterface::class);
4745
// By default return false
4846
$this->container->has(Argument::cetera())->willReturn(false);
47+
}
4948

50-
$this->methodResolver = new ContainerMethodResolver(
51-
$this->container->reveal(),
52-
new DefaultServiceNameResolver()
53-
);
49+
/**
50+
* @Given there is a :serviceId service for :methodName JSON-RPC method
51+
*/
52+
public function givenThereIsAServiceMethodNamed($serviceId, $methodeName)
53+
{
54+
$this->createJsonRpcMethod($methodeName);
55+
$this->bindJsonRpcMethodToContainerServiceId($methodeName, $serviceId);
5456
}
5557

5658
/**
57-
* @Given there is a method named :methodName
59+
* @Given ServiceNameResolver will resolve :methodName JSON-RPC method to :serviceId service
5860
*/
59-
public function givenThereIsAMethodNamed($methodName)
61+
public function givenServiceNameResolverWillResolveMethodNameToServiceId($methodeName, $serviceId)
6062
{
61-
$this->prophesizedMethodList[$methodName] = $this->prophesizeMethod($methodName);
63+
$this->addServiceNameResolverMapping($methodeName, $serviceId);
6264
}
6365

6466
/**
65-
* @When I ask for :methodName method
67+
* @When I ask for :methodName JSON-RPC method
6668
*/
6769
public function whenIAskForMethod($methodName)
6870
{
6971
$this->lastException = $this->lastMethod = null;
7072
try {
71-
$this->lastMethod = $this->methodResolver->resolve($methodName);
73+
$this->lastMethod = $this->getMethodResolver()->resolve($methodName);
7274
} catch (JsonRpcMethodNotFoundException $exception) {
7375
$this->lastException = $exception;
7476
}
7577
}
7678

7779
/**
78-
* @Then I should have :methodName method
80+
* @Then I should have :methodName JSON-RPC method
81+
* @Then I should have a null JSON-RPC method
7982
*/
80-
public function thenIShouldHaveMethod($methodName)
83+
public function thenIShouldHaveMethod($methodName = null)
8184
{
8285
Assert::assertSame(
83-
$this->prophesizedMethodList[$methodName]->reveal(),
86+
null === $methodName ? $methodName : $this->methodList[$methodName]->reveal(),
8487
$this->lastMethod
8588
);
8689
}
90+
8791
/**
8892
* @Then I should have a JSON-RPC exception with code :errorCode
8993
*/
@@ -93,18 +97,61 @@ public function thenIShouldHaveAJsonRpcExceptionWithCode($errorCode)
9397
Assert::assertSame((int)$errorCode, $this->lastException->getErrorCode());
9498
}
9599

100+
/**
101+
* @return ContainerMethodResolver
102+
*/
103+
private function getMethodResolver() : ContainerMethodResolver
104+
{
105+
$resolver = new ContainerMethodResolver($this->container->reveal());
106+
107+
$this->prophesizeServiceNameResolverIfDefined($resolver);
108+
109+
return $resolver;
110+
}
111+
96112
/**
97113
* @param string $methodName
98-
*
99-
* @return ObjectProphecy
100114
*/
101-
private function prophesizeMethod(string $methodName)
115+
private function createJsonRpcMethod(string $methodName)
102116
{
103-
$method = $this->prophet->prophesize(JsonRpcMethodInterface::class);
117+
$this->methodList[$methodName] = $this->prophet->prophesize(JsonRpcMethodInterface::class);
118+
}
119+
120+
/**
121+
* @param $serviceId
122+
* @param $methodName
123+
*/
124+
private function bindJsonRpcMethodToContainerServiceId(string $methodName, string $serviceId)
125+
{
126+
$this->container->has($serviceId)->willReturn(true);
127+
$this->container->get($serviceId)->willReturn($this->methodList[$methodName]);
128+
}
129+
130+
/**
131+
* @param string $methodeName
132+
* @param string $serviceId
133+
*/
134+
private function addServiceNameResolverMapping(string $methodeName, string $serviceId)
135+
{
136+
$this->serviceNameResolverMapping[$methodeName] = $serviceId;
137+
}
104138

105-
$this->container->has($methodName)->willReturn(true);
106-
$this->container->get($methodName)->willReturn($method->reveal());
139+
/**
140+
* @return ObjectProphecy
141+
*/
142+
private function prophesizeServiceNameResolverIfDefined(ContainerMethodResolver $resolver)
143+
{
144+
// Append service name resolver if some mapping have been defined
145+
if (count($this->serviceNameResolverMapping)) {
146+
/** @var ServiceNameResolverInterface|ObjectProphecy $serviceNameResolver */
147+
$serviceNameResolver = $this->prophet->prophesize(ServiceNameResolverInterface::class);
148+
// Prophesize method calls based on given mapping
149+
foreach ($this->serviceNameResolverMapping as $methodName => $serviceId) {
150+
$serviceNameResolver->resolve($methodName)
151+
->willReturn($serviceId);
152+
}
107153

108-
return $method;
154+
$resolver->setServiceNameResolver($serviceNameResolver->reveal());
155+
}
109156
}
110157
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
Feature: Resolver
1+
Feature: Method Resolver
22

3-
Scenario: Should return the requested method
4-
Given there is a method named "my-method"
5-
When I ask for "my-method" method
6-
Then I should have "my-method" method
3+
Scenario: Should return the requested method from container
4+
Given there is a "getDummy" service for "getDummy" JSON-RPC method
5+
When I ask for "getDummy" JSON-RPC method
6+
Then I should have "getDummy" JSON-RPC method
77

8-
Scenario: Should throw an exception if requested method does not exist
9-
When I ask for "not-existing-method" method
10-
Then I should have a JSON-RPC exception with code "-32601"
8+
Scenario: Should return null if requested method does not exist
9+
When I ask for "not-existing-method" JSON-RPC method
10+
Then I should have a null JSON-RPC method
11+
12+
Scenario: Should return the requested method from container when service name resolver is used
13+
Given there is a "a.dummy.method.service" service for "getDummy" JSON-RPC method
14+
And ServiceNameResolver will resolve "getDummy" JSON-RPC method to "a.dummy.method.service" service
15+
When I ask for "getDummy" JSON-RPC method
16+
Then I should have "getDummy" JSON-RPC method

src/App/Resolver/DefaultServiceNameResolver.php

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

src/Infra/Resolver/ContainerMethodResolver.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
namespace Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver;
33

44
use Psr\Container\ContainerInterface;
5-
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
6-
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface;
75
use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface;
86
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface;
97

@@ -14,27 +12,42 @@ class ContainerMethodResolver implements MethodResolverInterface
1412
{
1513
/** @var ContainerInterface */
1614
private $container;
17-
/** @var ServiceNameResolverInterface */
18-
private $serviceNameResolver;
15+
/** @var ServiceNameResolverInterface|null */
16+
private $serviceNameResolver = null;
1917

20-
public function __construct(
21-
ContainerInterface $container,
22-
ServiceNameResolverInterface $serviceNameResolver
23-
) {
18+
/**
19+
* @param ContainerInterface $container
20+
*/
21+
public function __construct(ContainerInterface $container)
22+
{
2423
$this->container = $container;
25-
$this->serviceNameResolver = $serviceNameResolver;
2624
}
2725

2826
/**
2927
* {@inheritdoc}
3028
*/
31-
public function resolve(string $methodName) : JsonRpcMethodInterface
29+
public function resolve(string $methodName)
3230
{
33-
$serviceName = $this->serviceNameResolver->resolve($methodName);
31+
$serviceName = null !== $this->serviceNameResolver
32+
? $this->serviceNameResolver->resolve($methodName)
33+
: $methodName
34+
;
3435
if (!$this->container->has($serviceName)) {
35-
throw new JsonRpcMethodNotFoundException($methodName);
36+
return null;
3637
}
3738

3839
return $this->container->get($serviceName);
3940
}
41+
42+
/**
43+
* @param ServiceNameResolverInterface $serviceNameResolver
44+
*
45+
* @return ContainerMethodResolver
46+
*/
47+
public function setServiceNameResolver(ServiceNameResolverInterface $serviceNameResolver) : ContainerMethodResolver
48+
{
49+
$this->serviceNameResolver = $serviceNameResolver;
50+
51+
return $this;
52+
}
4053
}

tests/Functional/App/Resolver/DefaultServiceNameResolverTest.php

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

0 commit comments

Comments
 (0)