99use Psr \Container \ContainerInterface ;
1010use Yoanm \JsonRpcServer \Domain \Exception \JsonRpcMethodNotFoundException ;
1111use Yoanm \JsonRpcServer \Domain \Model \JsonRpcMethodInterface ;
12- use Yoanm \JsonRpcServerPsr11Resolver \App \ Resolver \ DefaultServiceNameResolver ;
12+ use Yoanm \JsonRpcServerPsr11Resolver \Domain \ Model \ ServiceNameResolverInterface ;
1313use Yoanm \JsonRpcServerPsr11Resolver \Infra \Resolver \ContainerMethodResolver ;
1414
1515/**
1616 * Defines application features from the specific context.
1717 */
1818class 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}
0 commit comments