-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathJsonRpcHttpServerExtensionTest.php
188 lines (152 loc) · 6.84 KB
/
JsonRpcHttpServerExtensionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Tests\Functional\DependencyInjection;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;
use Tests\Common\DependencyInjection\AbstractTestClass;
use Tests\Common\DependencyInjection\ConcreteJsonRpcServerDispatcherAware;
use Tests\Common\Mock\ConcreteParamsValidator;
use Yoanm\JsonRpcServer\App\Dispatcher\JsonRpcServerDispatcherAwareTrait;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodAwareInterface;
use Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection\JsonRpcHttpServerExtension;
/**
* @covers \Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection\JsonRpcHttpServerExtension
*/
class JsonRpcHttpServerExtensionTest extends AbstractTestClass
{
/**
* {@inheritdoc}
*/
protected function getContainerExtensions(): array
{
return [
new JsonRpcHttpServerExtension()
];
}
public function testShouldBeLoadable()
{
$this->loadContainer();
$this->assertEndpointIsUsable();
}
public function testShouldManageCustomEndpointPathFromConfiguration()
{
$myCustomEndpoint = 'my-custom-endpoint';
$this->loadContainer(['endpoint' => $myCustomEndpoint]);
// Assert custom resolver is an alias of the stub
$this->assertContainerBuilderHasParameter(self::EXPECTED_HTTP_ENDPOINT_PATH_CONTAINER_PARAM, $myCustomEndpoint);
$this->assertEndpointIsUsable();
}
public function testShouldReturnAnXsdValidationBasePath()
{
$this->assertNotNull((new JsonRpcHttpServerExtension())->getXsdValidationBasePath());
}
public function testShouldBindServerDispatcherToDispatcherAwareService()
{
$dispatcherAwareServiceDefinition = new Definition(ConcreteJsonRpcServerDispatcherAware::class);
$dispatcherAwareServiceDefinition->addTag('json_rpc_http_server.server_dispatcher_aware');
$this->setDefinition('my-dispatcher-aware-service', $dispatcherAwareServiceDefinition);
$this->loadContainer();
// Assert custom resolver is an alias of the stub
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'my-dispatcher-aware-service',
'setJsonRpcServerDispatcher',
[new Reference('json_rpc_http_server.dispatcher.server')],
0
);
$this->assertEndpointIsUsable();
}
public function testShouldThrowAnExceptionIfDispatcherAwareServiceDoesNotUseRightTrait()
{
$dispatcherAwareServiceDefinition = new Definition(\stdClass::class);
$dispatcherAwareServiceDefinition->addTag('json_rpc_http_server.server_dispatcher_aware');
$this->setDefinition('my-dispatcher-aware-service', $dispatcherAwareServiceDefinition);
$this->expectException(LogicException::class);
// Check that exception is for the second method
$this->expectExceptionMessage(
'Service "my-dispatcher-aware-service" is taggued with '
.'"json_rpc_http_server.server_dispatcher_aware" but does not use '
.'"'.JsonRpcServerDispatcherAwareTrait::class.'"'
);
$this->loadContainer();
}
public function testShouldInjectParamsValidatorAliasIfDefined()
{
$myValidatorServiceId = 'my-params-validator-service';
$paramsValidator = new Definition(ConcreteParamsValidator::class);
$this->setDefinition($myValidatorServiceId, $paramsValidator);
$this->container->setAlias(self::EXPECTED_PARAMS_VALIDATOR_ALIAS, $myValidatorServiceId);
$this->loadContainer();
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
self::EXPECTED_REQUEST_HANDLER_SERVICE_ID,
'setMethodParamsValidator',
[new Reference(self::EXPECTED_PARAMS_VALIDATOR_ALIAS)]
);
$this->assertEndpointIsUsable();
}
public function testShouldNotInjectParamsValidatorAliasIfNotDefined()
{
$this->loadContainer();
$handlerDefinition = $this->container->getDefinition(self::EXPECTED_REQUEST_HANDLER_SERVICE_ID);
foreach ($handlerDefinition->getMethodCalls() as $methodCall) {
if ('setMethodParamsValidator' === $methodCall[0]) {
$this->fail('Method call found for method "setMethodParamsValidator"');
}
}
$this->assertEndpointIsUsable();
}
public function testShouldBindJsonRpcMethodsToMethodAwareServices()
{
$methodAwareServiceServiceId = uniqid();
$jsonRpcMethodServiceId = uniqid();
$jsonRpcMethodServiceId2 = uniqid();
$methodName = 'my-method-name';
$methodName2 = 'my-method-name-2';
// A first method
$methodService = $this->createJsonRpcMethodDefinition();
$this->addJsonRpcMethodTag($methodService, $methodName);
$this->setDefinition($jsonRpcMethodServiceId, $methodService);
// A second method
$methodService2 = $this->createJsonRpcMethodDefinition();
$this->addJsonRpcMethodTag($methodService2, $methodName2);
$this->setDefinition($jsonRpcMethodServiceId2, $methodService2);
$methodAwareDefinition = new Definition(JsonRpcMethodAwareInterface::class);
$methodAwareDefinition->addTag(JsonRpcHttpServerExtension::JSONRPC_METHOD_AWARE_TAG);
$this->setDefinition($methodAwareServiceServiceId, $methodAwareDefinition);
$this->loadContainer();
// Assert that method mapping have been correctly injected
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
$methodAwareServiceServiceId,
'addJsonRpcMethod',
[
$methodName,
$jsonRpcMethodServiceId
],
0
);
// Assert that method mapping have been correctly injected
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
$methodAwareServiceServiceId,
'addJsonRpcMethod',
[
$methodName2,
$jsonRpcMethodServiceId2
],
1
);
$this->assertEndpointIsUsable();
}
public function testShouldThowAnExceptionIfMethodAwareServiceDoesNotImplementRightInterface()
{
$methodAwareServiceServiceId = uniqid();
$methodAwareDefinition = new Definition(\stdClass::class);
$methodAwareDefinition->addTag(JsonRpcHttpServerExtension::JSONRPC_METHOD_AWARE_TAG);
$this->setDefinition($methodAwareServiceServiceId, $methodAwareDefinition);
$this->expectException(LogicException::class);
$this->expectExceptionMessage(sprintf(
'Service "%s" is tagged as JSON-RPC method aware but does not implement %s',
$methodAwareServiceServiceId,
JsonRpcMethodAwareInterface::class
));
$this->loadContainer();
}
}