Skip to content

Commit 3bd886b

Browse files
yoanmbradjones1
andauthored
Do not cast integer as string ids + Fix Zero ID + PHP Coduo (#93)
* Do not cast integer as string ids * Replace PHPUnit exact match with PHP-coduo * Allow for zero IDs; notifications are only NULL * Fix #94 --------- Co-authored-by: Brad Jones <brad.jones@fruition.net>
1 parent a9b7bc6 commit 3bd886b

File tree

6 files changed

+20
-38
lines changed

6 files changed

+20
-38
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"require-dev": {
3838
"behat/behat": "^3.9.0",
39+
"coduo/php-matcher": "^6.0",
3940
"dvdoug/behat-code-coverage": "^5.0",
4041
"phpspec/prophecy": "^1.15",
4142
"phpspec/prophecy-phpunit": "^2.0",

features/bootstrap/FeatureContext.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
55
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
66
use Behat\Gherkin\Node\PyStringNode;
7-
use DemoApp\Dispatcher\BehatRequestLifecycleDispatcher;
7+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;
88
use PHPUnit\Framework\Assert;
99
use Tests\Functional\BehatContext\Helper\FakeEndpointCreator;
1010

@@ -13,6 +13,8 @@
1313
*/
1414
class FeatureContext extends AbstractContext
1515
{
16+
use PHPMatcherAssertions;
17+
1618
const KEY_JSON_RPC = 'jsonrpc';
1719
const KEY_ID = 'id';
1820
const KEY_RESULT = 'result';
@@ -75,8 +77,7 @@ public function thenLastResponseShouldBeAValidJsonRpcError()
7577
*/
7678
public function thenIShouldHaveTheFollowingResponse(PyStringNode $expectedResult)
7779
{
78-
// Decode content to get rid of any indentation/spacing/... issues
79-
Assert::assertEquals(
80+
$this->assertMatchesPattern(
8081
$this->jsonDecode($expectedResult->getRaw()),
8182
$this->getLastResponseDecoded()
8283
);

src/App/Creator/ResponseCreator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public function createEmptyResponse(JsonRpcRequest $fromRequest = null) : JsonRp
2626
->setIsNotification($fromRequest->isNotification())
2727
;
2828

29-
if ($fromRequest->getId()) {
29+
// Notification response doesn't have any ID defined (notification request doesn't either)
30+
if (!$fromRequest->isNotification()) {
3031
$response->setId($fromRequest->getId());
3132
}
3233

src/App/Serialization/JsonRpcRequestDenormalizer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ protected function bindIdIfProvided(JsonRpcRequest $request, array $item) : void
5050
{
5151
/** If no id defined => request is a notification */
5252
if (isset($item[self::KEY_ID])) {
53-
$request->setId(
54-
$item[self::KEY_ID] == (string)((int)$item[self::KEY_ID])
55-
? (int)$item[self::KEY_ID] // Convert it in case it's a string containing an int
56-
: (string)$item[self::KEY_ID] // Convert to string in all other cases
57-
);
53+
$request->setId($item[self::KEY_ID]);
5854
}
5955
}
6056

tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@ public function testShouldBindResultAndRequestParamToResponse()
4040
$this->assertSame($result, $response->getResult());
4141
$this->assertFromRequestBinding($fromRequest, $response);
4242
}
43+
44+
/**
45+
* Bug fix: https://github.com/yoanm/php-jsonrpc-server-sdk/issues/94
46+
*/
47+
public function testShouldConvertRequestWithZeroIdToResponseWithZeroId()
48+
{
49+
$fromRequest = $this->createRequest(self::DEFAULT_METHOD, self::DEFAULT_JSONRPC, 0);
50+
51+
$response = $this->responseCreator->createEmptyResponse($fromRequest);
52+
53+
$this->assertSame(0, $response->getId());
54+
}
4355
}

tests/Technical/App/Serialization/JsonRpcRequestDenormalizerTest.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,6 @@ protected function setUp(): void
2424
$this->requestDenormalizer = new JsonRpcRequestDenormalizer();
2525
}
2626

27-
/**
28-
* @dataProvider integerRequestIdProvider
29-
* @param mixed $requestId
30-
*/
31-
public function testDenormalizeShouldCastIdToIntWhenIdIs($requestId)
32-
{
33-
$item = [
34-
'jsonrpc' => 'fake-json-rpc-version',
35-
'method' => 'fake-method',
36-
'id' => $requestId,
37-
];
38-
39-
$result = $this->requestDenormalizer->denormalize($item);
40-
41-
$this->assertSame((int) $result->getId(), $result->getId());
42-
}
43-
44-
public function integerRequestIdProvider()
45-
{
46-
return [
47-
'real integer' => [
48-
'requestId' => 321,
49-
],
50-
'integer stored as string' => [
51-
'requestId' => '321',
52-
],
53-
];
54-
}
55-
5627
/**
5728
* @dataProvider invalidParamListProvider
5829
*

0 commit comments

Comments
 (0)