This repository has been archived by the owner on Jan 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
306 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Feature: Resolver | ||
|
||
Scenario: Should return the requested method | ||
Given there is a method named "my-method" | ||
When I ask for "my-method" method | ||
Then I should have "my-method" method | ||
|
||
Scenario: Should throw an exception if requested method does not exist | ||
When I ask for "not-existing-method" method | ||
Then I should have a JSON-RPC exception with code "-32601" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Yoanm\JsonRpcServerPsr11Resolver\App\Resolver; | ||
|
||
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface; | ||
|
||
/** | ||
* Class ContainerMethodResolver | ||
*/ | ||
class DefaultServiceNameResolver implements ServiceNameResolverInterface | ||
{ | ||
/** @var string */ | ||
private $prefix = ''; | ||
|
||
/** | ||
* @param string $prefix | ||
*/ | ||
public function __construct(string $prefix = '') | ||
{ | ||
$this->prefix = $prefix; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(string $methodName) : string | ||
{ | ||
return $this->prefix.$methodName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace Yoanm\JsonRpcServerPsr11Resolver\Domain\Model; | ||
|
||
/** | ||
* Interface ServiceNameResolverInterface | ||
*/ | ||
interface ServiceNameResolverInterface | ||
{ | ||
/** | ||
* @param string $methodName | ||
* | ||
* @return string The corresponding service name | ||
*/ | ||
public function resolve(string $methodName) : string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
namespace Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException; | ||
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; | ||
use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface; | ||
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface; | ||
|
||
/** | ||
* Class ContainerMethodResolver | ||
*/ | ||
class ContainerMethodResolver implements MethodResolverInterface | ||
{ | ||
/** @var ContainerInterface */ | ||
private $container; | ||
/** @var ServiceNameResolverInterface */ | ||
private $serviceNameResolver; | ||
|
||
public function __construct( | ||
ContainerInterface $container, | ||
ServiceNameResolverInterface $serviceNameResolver | ||
) { | ||
$this->container = $container; | ||
$this->serviceNameResolver = $serviceNameResolver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(string $methodName) : JsonRpcMethodInterface | ||
{ | ||
$serviceName = $this->serviceNameResolver->resolve($methodName); | ||
if (!$this->container->has($serviceName)) { | ||
throw new JsonRpcMethodNotFoundException($methodName); | ||
} | ||
|
||
return $this->container->get($serviceName); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
tests/Functional/App/Resolver/DefaultServiceNameResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
namespace Tests\Functional\App\Resolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Yoanm\JsonRpcServerPsr11Resolver\App\Resolver\DefaultServiceNameResolver; | ||
|
||
/** | ||
* @covers \Yoanm\JsonRpcServerPsr11Resolver\App\Resolver\DefaultServiceNameResolver | ||
*/ | ||
class DefaultServiceNameResolverTest extends TestCase | ||
{ | ||
public function testShouldDoNothingByDefault() | ||
{ | ||
$serviceName = 'my-service-name'; | ||
$resolver = new DefaultServiceNameResolver(); | ||
|
||
$this->assertSame( | ||
$serviceName, | ||
$resolver->resolve($serviceName) | ||
); | ||
} | ||
|
||
|
||
public function testShouldPrependPrefixBeforeServiceName() | ||
{ | ||
$prefix = 'my-prefix'; | ||
$serviceName = 'my-service-name'; | ||
$resolver = new DefaultServiceNameResolver($prefix); | ||
|
||
$this->assertSame( | ||
$prefix.$serviceName, | ||
$resolver->resolve($serviceName) | ||
); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
tests/Functional/Infra/Resolver/ContainerMethodResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
namespace Tests\Functional\Infra\Resolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Container\ContainerInterface; | ||
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException; | ||
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; | ||
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface; | ||
use Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver\ContainerMethodResolver; | ||
|
||
/** | ||
* @covers \Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver\ContainerMethodResolver | ||
*/ | ||
class ContainerMethodResolverTest extends TestCase | ||
{ | ||
/** @var ContainerMethodResolver */ | ||
private $resolver; | ||
|
||
/** @var ContainerInterface|ObjectProphecy */ | ||
private $container; | ||
/** @var ServiceNameResolverInterface|ObjectProphecy */ | ||
private $serviceNameResolver; | ||
|
||
public function setUp() | ||
{ | ||
$this->container = $this->prophesize(ContainerInterface::class); | ||
$this->serviceNameResolver = $this->prophesize(ServiceNameResolverInterface::class); | ||
|
||
$this->resolver = new ContainerMethodResolver( | ||
$this->container->reveal(), | ||
$this->serviceNameResolver->reveal() | ||
); | ||
} | ||
|
||
|
||
public function testShouldResolveServiceNameAndLoadItFromContainer() | ||
{ | ||
$methodName = 'my-method-name'; | ||
$serviceName = 'my-service-name'; | ||
|
||
$method = $this->prophesize(JsonRpcMethodInterface::class); | ||
|
||
$this->serviceNameResolver->resolve($methodName) | ||
->willReturn($serviceName) | ||
->shouldBeCalled(); | ||
|
||
$this->container->has($serviceName) | ||
->willReturn(true) | ||
->shouldBeCalled(); | ||
|
||
$this->container->get($serviceName) | ||
->willReturn($method->reveal()) | ||
->shouldBeCalled(); | ||
|
||
$this->assertSame( | ||
$method->reveal(), | ||
$this->resolver->resolve($methodName) | ||
); | ||
} | ||
|
||
public function testShouldThrowAnExceptionIsMethodDoesNotExist() | ||
{ | ||
$methodName = 'my-method-name'; | ||
$serviceName = 'my-service-name'; | ||
|
||
$this->serviceNameResolver->resolve($methodName) | ||
->willReturn($serviceName) | ||
->shouldBeCalled(); | ||
|
||
$this->container->has($serviceName) | ||
->willReturn(false) | ||
->shouldBeCalled(); | ||
|
||
$this->expectException(JsonRpcMethodNotFoundException::class); | ||
|
||
try { | ||
$this->resolver->resolve($methodName); | ||
} catch (JsonRpcMethodNotFoundException $e) { | ||
$this->assertSame( | ||
$methodName, | ||
$e->getMethodName() | ||
); | ||
|
||
throw $e; | ||
} | ||
} | ||
} |