diff --git a/.gitignore b/.gitignore index 7579f74..50b321e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index 7525604..499406e 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "require": { "php": "^7.2", "ext-json": "*", - "guzzlehttp/guzzle": "^6.3" + "guzzlehttp/guzzle": "^6.3", + "phpunit/phpunit": "^8.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..e05abab --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,7 @@ + + + + ./tests + + + diff --git a/src/JsonRpc/Response.php b/src/JsonRpc/Response.php index 2a91870..db8e7c6 100644 --- a/src/JsonRpc/Response.php +++ b/src/JsonRpc/Response.php @@ -30,9 +30,23 @@ public static function fromJson(string $json): Response if ($object = \json_decode($json)) { $error = isset($object->error) ? Error::fromObject($object->error) : null; + self::convertJsonClass($object->result); + return new Response($object->result, $error, $object->id); } throw new Exception(\json_last_error_msg()); } + + private static function convertJsonClass(object &$result): void { + foreach ($result as &$value) { + if (is_object($value) && isset($value->__jsonclass__)) { + switch ($value->__jsonclass__[0]) { + case 'datetime': + $value = new \DateTime($value->__jsonclass__[1], new \DateTimeZone('UTC')); + break; + } + } + } + } } diff --git a/tests/JsonRpc/ResponseTest.php b/tests/JsonRpc/ResponseTest.php new file mode 100644 index 0000000..e713cb1 --- /dev/null +++ b/tests/JsonRpc/ResponseTest.php @@ -0,0 +1,25 @@ +assertInstanceOf(Response::class, $response); + + $result = $response->getResult(); + $this->assertInstanceOf('stdClass', $result); + $this->assertObjectHasAttribute('lastModified', $result); + + $lastModified = $result->lastModified; + $this->assertInstanceOf('DateTime', $lastModified); + $this->assertSame(1536652232, $lastModified->getTimestamp()); + } + +}