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

Commit aa5120a

Browse files
authored
Add Resolver (#1)
1 parent be406e8 commit aa5120a

File tree

9 files changed

+306
-6
lines changed

9 files changed

+306
-6
lines changed

features/bootstrap/FeatureContext.php

+87-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,36 @@
22
namespace Tests\Functional\BehatContext;
33

44
use Behat\Behat\Context\Context;
5-
use Behat\Gherkin\Node\PyStringNode;
65
use PHPUnit\Framework\Assert;
76
use Prophecy\Argument;
8-
use Tests\Functional\BehatContext\App\BehatMethodResolver;
9-
use Tests\Functional\BehatContext\App\FakeEndpointCreator;
10-
use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface;
11-
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint;
7+
use Prophecy\Prophecy\ObjectProphecy;
8+
use Prophecy\Prophet;
9+
use Psr\Container\ContainerInterface;
10+
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
11+
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface;
12+
use Yoanm\JsonRpcServerPsr11Resolver\App\Resolver\DefaultServiceNameResolver;
13+
use Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver\ContainerMethodResolver;
1214

1315
/**
1416
* Defines application features from the specific context.
1517
*/
1618
class FeatureContext implements Context
1719
{
20+
/** @var ContainerMethodResolver */
21+
private $methodResolver;
22+
23+
/** @var ObjectProphecy[] */
24+
private $prophesizedMethodList = [];
25+
/** @var JsonRpcMethodInterface|ObjectProphecy|null */
26+
private $lastMethod;
27+
/** @var JsonRpcMethodNotFoundException|null */
28+
private $lastException;
29+
30+
/** @var Prophet */
31+
private $prophet;
32+
/** @var ObjectProphecy|ContainerInterface */
33+
private $container;
34+
1835
/**
1936
* Initializes context.
2037
*
@@ -24,5 +41,70 @@ class FeatureContext implements Context
2441
*/
2542
public function __construct()
2643
{
44+
$this->prophet = new Prophet();
45+
46+
$this->container = $this->prophet->prophesize(ContainerInterface::class);
47+
// By default return false
48+
$this->container->has(Argument::cetera())->willReturn(false);
49+
50+
$this->methodResolver = new ContainerMethodResolver(
51+
$this->container->reveal(),
52+
new DefaultServiceNameResolver()
53+
);
54+
}
55+
56+
/**
57+
* @Given there is a method named :methodName
58+
*/
59+
public function givenThereIsAMethodNamed($methodName)
60+
{
61+
$this->prophesizedMethodList[$methodName] = $this->prophesizeMethod($methodName);
62+
}
63+
64+
/**
65+
* @When I ask for :methodName method
66+
*/
67+
public function whenIAskForMethod($methodName)
68+
{
69+
$this->lastException = $this->lastMethod = null;
70+
try {
71+
$this->lastMethod = $this->methodResolver->resolve($methodName);
72+
} catch (JsonRpcMethodNotFoundException $exception) {
73+
$this->lastException = $exception;
74+
}
75+
}
76+
77+
/**
78+
* @Then I should have :methodName method
79+
*/
80+
public function thenIShouldHaveMethod($methodName)
81+
{
82+
Assert::assertSame(
83+
$this->prophesizedMethodList[$methodName]->reveal(),
84+
$this->lastMethod
85+
);
86+
}
87+
/**
88+
* @Then I should have a JSON-RPC exception with code :errorCode
89+
*/
90+
public function thenIShouldHaveAJsonRpcExceptionWithCode($errorCode)
91+
{
92+
Assert::assertInstanceOf(JsonRpcMethodNotFoundException::class, $this->lastException);
93+
Assert::assertSame((int)$errorCode, $this->lastException->getErrorCode());
94+
}
95+
96+
/**
97+
* @param string $methodName
98+
*
99+
* @return ObjectProphecy
100+
*/
101+
private function prophesizeMethod(string $methodName)
102+
{
103+
$method = $this->prophet->prophesize(JsonRpcMethodInterface::class);
104+
105+
$this->container->has($methodName)->willReturn(true);
106+
$this->container->get($methodName)->willReturn($method->reveal());
107+
108+
return $method;
27109
}
28110
}

features/bootstrap/resolver.feature

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Resolver
2+
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
7+
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"

src/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Yoanm\JsonRpcServerPsr11Resolver\App\Resolver;
3+
4+
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface;
5+
6+
/**
7+
* Class ContainerMethodResolver
8+
*/
9+
class DefaultServiceNameResolver implements ServiceNameResolverInterface
10+
{
11+
/** @var string */
12+
private $prefix = '';
13+
14+
/**
15+
* @param string $prefix
16+
*/
17+
public function __construct(string $prefix = '')
18+
{
19+
$this->prefix = $prefix;
20+
}
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function resolve(string $methodName) : string
26+
{
27+
return $this->prefix.$methodName;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Yoanm\JsonRpcServerPsr11Resolver\Domain\Model;
3+
4+
/**
5+
* Interface ServiceNameResolverInterface
6+
*/
7+
interface ServiceNameResolverInterface
8+
{
9+
/**
10+
* @param string $methodName
11+
*
12+
* @return string The corresponding service name
13+
*/
14+
public function resolve(string $methodName) : string;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver;
3+
4+
use Psr\Container\ContainerInterface;
5+
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
6+
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface;
7+
use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface;
8+
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface;
9+
10+
/**
11+
* Class ContainerMethodResolver
12+
*/
13+
class ContainerMethodResolver implements MethodResolverInterface
14+
{
15+
/** @var ContainerInterface */
16+
private $container;
17+
/** @var ServiceNameResolverInterface */
18+
private $serviceNameResolver;
19+
20+
public function __construct(
21+
ContainerInterface $container,
22+
ServiceNameResolverInterface $serviceNameResolver
23+
) {
24+
$this->container = $container;
25+
$this->serviceNameResolver = $serviceNameResolver;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function resolve(string $methodName) : JsonRpcMethodInterface
32+
{
33+
$serviceName = $this->serviceNameResolver->resolve($methodName);
34+
if (!$this->container->has($serviceName)) {
35+
throw new JsonRpcMethodNotFoundException($methodName);
36+
}
37+
38+
return $this->container->get($serviceName);
39+
}
40+
}

tests/Functional/.gitkeep

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Tests\Functional\App\Resolver;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Prophecy\Argument;
6+
use Yoanm\JsonRpcServerPsr11Resolver\App\Resolver\DefaultServiceNameResolver;
7+
8+
/**
9+
* @covers \Yoanm\JsonRpcServerPsr11Resolver\App\Resolver\DefaultServiceNameResolver
10+
*/
11+
class DefaultServiceNameResolverTest extends TestCase
12+
{
13+
public function testShouldDoNothingByDefault()
14+
{
15+
$serviceName = 'my-service-name';
16+
$resolver = new DefaultServiceNameResolver();
17+
18+
$this->assertSame(
19+
$serviceName,
20+
$resolver->resolve($serviceName)
21+
);
22+
}
23+
24+
25+
public function testShouldPrependPrefixBeforeServiceName()
26+
{
27+
$prefix = 'my-prefix';
28+
$serviceName = 'my-service-name';
29+
$resolver = new DefaultServiceNameResolver($prefix);
30+
31+
$this->assertSame(
32+
$prefix.$serviceName,
33+
$resolver->resolve($serviceName)
34+
);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
namespace Tests\Functional\Infra\Resolver;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Prophecy\Argument;
6+
use Prophecy\Prophecy\ObjectProphecy;
7+
use Psr\Container\ContainerInterface;
8+
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
9+
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface;
10+
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface;
11+
use Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver\ContainerMethodResolver;
12+
13+
/**
14+
* @covers \Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver\ContainerMethodResolver
15+
*/
16+
class ContainerMethodResolverTest extends TestCase
17+
{
18+
/** @var ContainerMethodResolver */
19+
private $resolver;
20+
21+
/** @var ContainerInterface|ObjectProphecy */
22+
private $container;
23+
/** @var ServiceNameResolverInterface|ObjectProphecy */
24+
private $serviceNameResolver;
25+
26+
public function setUp()
27+
{
28+
$this->container = $this->prophesize(ContainerInterface::class);
29+
$this->serviceNameResolver = $this->prophesize(ServiceNameResolverInterface::class);
30+
31+
$this->resolver = new ContainerMethodResolver(
32+
$this->container->reveal(),
33+
$this->serviceNameResolver->reveal()
34+
);
35+
}
36+
37+
38+
public function testShouldResolveServiceNameAndLoadItFromContainer()
39+
{
40+
$methodName = 'my-method-name';
41+
$serviceName = 'my-service-name';
42+
43+
$method = $this->prophesize(JsonRpcMethodInterface::class);
44+
45+
$this->serviceNameResolver->resolve($methodName)
46+
->willReturn($serviceName)
47+
->shouldBeCalled();
48+
49+
$this->container->has($serviceName)
50+
->willReturn(true)
51+
->shouldBeCalled();
52+
53+
$this->container->get($serviceName)
54+
->willReturn($method->reveal())
55+
->shouldBeCalled();
56+
57+
$this->assertSame(
58+
$method->reveal(),
59+
$this->resolver->resolve($methodName)
60+
);
61+
}
62+
63+
public function testShouldThrowAnExceptionIsMethodDoesNotExist()
64+
{
65+
$methodName = 'my-method-name';
66+
$serviceName = 'my-service-name';
67+
68+
$this->serviceNameResolver->resolve($methodName)
69+
->willReturn($serviceName)
70+
->shouldBeCalled();
71+
72+
$this->container->has($serviceName)
73+
->willReturn(false)
74+
->shouldBeCalled();
75+
76+
$this->expectException(JsonRpcMethodNotFoundException::class);
77+
78+
try {
79+
$this->resolver->resolve($methodName);
80+
} catch (JsonRpcMethodNotFoundException $e) {
81+
$this->assertSame(
82+
$methodName,
83+
$e->getMethodName()
84+
);
85+
86+
throw $e;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)