From fbd0ad82b67509cc62e211f70aacedc2b498dbf8 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Fri, 5 May 2023 08:48:07 +0200 Subject: [PATCH 1/6] Do not cast integer as string ids --- .../JsonRpcRequestDenormalizer.php | 6 +--- .../JsonRpcRequestDenormalizerTest.php | 29 ------------------- 2 files changed, 1 insertion(+), 34 deletions(-) diff --git a/src/App/Serialization/JsonRpcRequestDenormalizer.php b/src/App/Serialization/JsonRpcRequestDenormalizer.php index 83bdebcd..a6f2395a 100644 --- a/src/App/Serialization/JsonRpcRequestDenormalizer.php +++ b/src/App/Serialization/JsonRpcRequestDenormalizer.php @@ -50,11 +50,7 @@ protected function bindIdIfProvided(JsonRpcRequest $request, array $item) : void { /** If no id defined => request is a notification */ if (isset($item[self::KEY_ID])) { - $request->setId( - $item[self::KEY_ID] == (string)((int)$item[self::KEY_ID]) - ? (int)$item[self::KEY_ID] // Convert it in case it's a string containing an int - : (string)$item[self::KEY_ID] // Convert to string in all other cases - ); + $request->setId($item[self::KEY_ID]); } } diff --git a/tests/Technical/App/Serialization/JsonRpcRequestDenormalizerTest.php b/tests/Technical/App/Serialization/JsonRpcRequestDenormalizerTest.php index 71adcaab..3498fbb1 100644 --- a/tests/Technical/App/Serialization/JsonRpcRequestDenormalizerTest.php +++ b/tests/Technical/App/Serialization/JsonRpcRequestDenormalizerTest.php @@ -24,35 +24,6 @@ protected function setUp(): void $this->requestDenormalizer = new JsonRpcRequestDenormalizer(); } - /** - * @dataProvider integerRequestIdProvider - * @param mixed $requestId - */ - public function testDenormalizeShouldCastIdToIntWhenIdIs($requestId) - { - $item = [ - 'jsonrpc' => 'fake-json-rpc-version', - 'method' => 'fake-method', - 'id' => $requestId, - ]; - - $result = $this->requestDenormalizer->denormalize($item); - - $this->assertSame((int) $result->getId(), $result->getId()); - } - - public function integerRequestIdProvider() - { - return [ - 'real integer' => [ - 'requestId' => 321, - ], - 'integer stored as string' => [ - 'requestId' => '321', - ], - ]; - } - /** * @dataProvider invalidParamListProvider * From 7149d10b8d0c5cb3db2503d8869cda6f1ad7af9c Mon Sep 17 00:00:00 2001 From: Yoanm Date: Fri, 5 May 2023 08:48:35 +0200 Subject: [PATCH 2/6] Replace PHPUnit exact match with PHP-coduo --- composer.json | 1 + features/bootstrap/FeatureContext.php | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 8635b871..6f606a74 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ }, "require-dev": { "behat/behat": "^3.9.0", + "coduo/php-matcher": "^6.0", "dvdoug/behat-code-coverage": "^5.0", "phpspec/prophecy": "^1.15", "phpspec/prophecy-phpunit": "^2.0", diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index a92e8990..871b648a 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -4,7 +4,7 @@ use Behat\Behat\Context\Environment\InitializedContextEnvironment; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\PyStringNode; -use DemoApp\Dispatcher\BehatRequestLifecycleDispatcher; +use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions; use PHPUnit\Framework\Assert; use Tests\Functional\BehatContext\Helper\FakeEndpointCreator; @@ -13,6 +13,8 @@ */ class FeatureContext extends AbstractContext { + use PHPMatcherAssertions; + const KEY_JSON_RPC = 'jsonrpc'; const KEY_ID = 'id'; const KEY_RESULT = 'result'; @@ -75,8 +77,7 @@ public function thenLastResponseShouldBeAValidJsonRpcError() */ public function thenIShouldHaveTheFollowingResponse(PyStringNode $expectedResult) { - // Decode content to get rid of any indentation/spacing/... issues - Assert::assertEquals( + $this->assertMatchesPattern( $this->jsonDecode($expectedResult->getRaw()), $this->getLastResponseDecoded() ); From 3b0fd124ca6efd028e0fdbc55759b2235c40fb63 Mon Sep 17 00:00:00 2001 From: Brad Jones Date: Fri, 2 Jul 2021 00:49:59 -0600 Subject: [PATCH 3/6] Allow for zero IDs; notifications are only NULL --- src/App/Creator/ResponseCreator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App/Creator/ResponseCreator.php b/src/App/Creator/ResponseCreator.php index c8a6907f..a955610b 100644 --- a/src/App/Creator/ResponseCreator.php +++ b/src/App/Creator/ResponseCreator.php @@ -26,7 +26,7 @@ public function createEmptyResponse(JsonRpcRequest $fromRequest = null) : JsonRp ->setIsNotification($fromRequest->isNotification()) ; - if ($fromRequest->getId()) { + if (!is_null($fromRequest->getId())) { $response->setId($fromRequest->getId()); } From 3b594ab2c2721b2d2889e277f98866e62dd1689d Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 6 May 2023 07:58:40 +0200 Subject: [PATCH 4/6] Fix https://github.com/yoanm/php-jsonrpc-server-sdk/issues/94 --- src/App/Creator/ResponseCreator.php | 3 ++- .../ResponseCreator/CreateResultResponseTest.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/App/Creator/ResponseCreator.php b/src/App/Creator/ResponseCreator.php index a955610b..99ee7079 100644 --- a/src/App/Creator/ResponseCreator.php +++ b/src/App/Creator/ResponseCreator.php @@ -26,7 +26,8 @@ public function createEmptyResponse(JsonRpcRequest $fromRequest = null) : JsonRp ->setIsNotification($fromRequest->isNotification()) ; - if (!is_null($fromRequest->getId())) { + // Notification response doesn't have any ID defined (notification request doesn't either) + if (!$fromRequest->isNotification()) { $response->setId($fromRequest->getId()); } diff --git a/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php b/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php index c7440929..11aded6d 100644 --- a/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php +++ b/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php @@ -2,6 +2,7 @@ namespace Tests\Functional\App\Creator\ResponseCreator; use Prophecy\PhpUnit\ProphecyTrait; +use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest; /** * @covers \Yoanm\JsonRpcServer\App\Creator\ResponseCreator @@ -40,4 +41,17 @@ public function testShouldBindResultAndRequestParamToResponse() $this->assertSame($result, $response->getResult()); $this->assertFromRequestBinding($fromRequest, $response); } + + + + /** + * Bug fix: https://github.com/yoanm/php-jsonrpc-server-sdk/issues/94 + */ + public function testShouldConvertRequestWithZeroIdToResponseWithZeroId() { + $fromRequest = $this->createRequest(self::DEFAULT_METHOD, self::DEFAULT_JSONRPC, 0); + + $response = $this->responseCreator->createEmptyResponse($fromRequest); + + $this->assertSame(0, $response->getId()); + } } From 2955c9ad775844fad3714107e188f2758d20fec3 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 6 May 2023 07:59:16 +0200 Subject: [PATCH 5/6] Remove useless empty lines --- .../App/Creator/ResponseCreator/CreateResultResponseTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php b/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php index 11aded6d..e61ed487 100644 --- a/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php +++ b/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php @@ -42,8 +42,6 @@ public function testShouldBindResultAndRequestParamToResponse() $this->assertFromRequestBinding($fromRequest, $response); } - - /** * Bug fix: https://github.com/yoanm/php-jsonrpc-server-sdk/issues/94 */ From 746ae8d82bfe2dcf14dbf19030390d2491cdd977 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 6 May 2023 08:01:34 +0200 Subject: [PATCH 6/6] Fix lint --- .../App/Creator/ResponseCreator/CreateResultResponseTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php b/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php index e61ed487..0a7d17ea 100644 --- a/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php +++ b/tests/Functional/App/Creator/ResponseCreator/CreateResultResponseTest.php @@ -2,7 +2,6 @@ namespace Tests\Functional\App\Creator\ResponseCreator; use Prophecy\PhpUnit\ProphecyTrait; -use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest; /** * @covers \Yoanm\JsonRpcServer\App\Creator\ResponseCreator @@ -45,7 +44,8 @@ public function testShouldBindResultAndRequestParamToResponse() /** * Bug fix: https://github.com/yoanm/php-jsonrpc-server-sdk/issues/94 */ - public function testShouldConvertRequestWithZeroIdToResponseWithZeroId() { + public function testShouldConvertRequestWithZeroIdToResponseWithZeroId() + { $fromRequest = $this->createRequest(self::DEFAULT_METHOD, self::DEFAULT_JSONRPC, 0); $response = $this->responseCreator->createEmptyResponse($fromRequest);