Skip to content

Commit

Permalink
Restored 100% unit-test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
smortensen committed Jan 12, 2016
1 parent 0cd4852 commit 0901c81
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 95 deletions.
70 changes: 0 additions & 70 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@ abstract class Exception extends \Exception
*/
public function __construct($message, $code, $data = null)
{
if (!self::isValidMessage($message)) {
$message = '';
}

if (!self::isValidCode($code)) {
$code = 1;
}

if (!self::isValidData($data)) {
$data = null;
}

parent::__construct($message, $code);

$this->data = $data;
Expand All @@ -73,62 +61,4 @@ public function getData()
{
return $this->data;
}

/**
* Determines whether a value can be used as an error message.
*
* @param string $input
* Short description of the error that occurred. This message SHOULD
* be limited to a single, concise sentence.
*
* @return bool
* Returns true iff the value can be used as an error message.
*/
private static function isValidMessage($input)
{
return is_string($input);
}

/**
* Determines whether a value can be used as an error code.
*
* @param $code
* Integer identifying the type of error that occurred. This code MUST
* follow the JSON-RPC 2.0 requirements for error codes:
*
* @link http://www.jsonrpc.org/specification#error_object
*
* @return bool
* Returns true iff the value can be used as an error code.
*/
private static function isValidCode($code)
{
return is_int($code);
}

/**
* Determines whether a value can be used as the data value in an error
* object.
*
* @param null|boolean|integer|float|string|array $input
* An optional primitive value that contains additional information about
* the error.You're free to define the format of this data (e.g. you could
* supply an array with detailed error information). Alternatively, you may
* omit this field by supplying a null value.
*
* @return bool
* Returns true iff the value can be used as the data value in an error
* object.
*/
private static function isValidData($input)
{
$type = gettype($input);

return ($type === 'array')
|| ($type === 'string')
|| ($type === 'double')
|| ($type === 'integer')
|| ($type === 'boolean')
|| ($type === 'NULL');
}
}
55 changes: 52 additions & 3 deletions src/Exception/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
*
* @link http://www.jsonrpc.org/specification#error_object
*
* You can use this flexibility to communicate any issues that arise while
* your application is evaluating a user request.
* You can throw an "Application" exception to communicate any issues that arise
* while your application is evaluating a user request.
*
* However:
*
Expand All @@ -63,19 +63,42 @@ class Application extends JsonRpc\Exception
*
* @param null|boolean|integer|float|string|array $data
* An optional primitive value that contains additional information about
* the error.You're free to define the format of this data (e.g. you could
* the error. You're free to define the format of this data (e.g. you could
* supply an array with detailed error information). Alternatively, you may
* omit this field by providing a null value.
*/
public function __construct($message, $code, $data = null)
{
if (!self::isValidMessage($message)) {
$message = '';
}

if (!self::isValidCode($code)) {
$code = 1;
}

if (!self::isValidData($data)) {
$data = null;
}

parent::__construct($message, $code, $data);
}

/**
* Determines whether a value can be used as an error message.
*
* @param string $input
* Short description of the error that occurred. This message SHOULD
* be limited to a single, concise sentence.
*
* @return bool
* Returns true iff the value can be used as an error message.
*/
private static function isValidMessage($input)
{
return is_string($input);
}

/**
* Determines whether a value can be used as an application-defined error
* code.
Expand All @@ -99,4 +122,30 @@ private static function isValidCode($code)
{
return is_int($code) && (($code < -32768) || (-32000 < $code));
}

/**
* Determines whether a value can be used as the data value in an error
* object.
*
* @param null|boolean|integer|float|string|array $input
* An optional primitive value that contains additional information about
* the error. You're free to define the format of this data (e.g. you could
* supply an array with detailed error information). Alternatively, you may
* omit this field by supplying a null value.
*
* @return bool
* Returns true iff the value can be used as the data value in an error
* object.
*/
private static function isValidData($input)
{
$type = gettype($input);

return ($type === 'array')
|| ($type === 'string')
|| ($type === 'double')
|| ($type === 'integer')
|| ($type === 'boolean')
|| ($type === 'NULL');
}
}
46 changes: 38 additions & 8 deletions src/Exception/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@
*
* @link http://www.jsonrpc.org/specification#error_object
*
* You can use this flexibility to communicate any issues that arise while
* your JSON-RPC 2.0 implementation is processing a request.
* You can throw an "Implementation" exception to communicate any issues that
* arise while your JSON-RPC 2.0 implementation is processing a request.
*
* However:
*
* If the method cannot be called (e.g. if the method doesn't exist, or is a
* If a method cannot be called (e.g. if the method doesn't exist, or is a
* private method), then you should report the issue through the "Method"
* exception instead.
*
* If the method exists, but the user-supplied arguments are incompatible with
* the method's type signature, or one or more of the arguments is invalid,
* then you should report the issue through the "Argument" exception.
* then you should report the issue through an "Argument" exception.
*
* Finally, if the issue did not arise within your codebase, but instead arose
* within the application-specific library code that actually evaluated the
Expand All @@ -57,13 +57,13 @@ class Implementation extends JsonRpc\Exception
/**
* @param int $code
* Integer identifying the type of error that occurred. As the author of a
* JSON-RPC 2.0 implementation, you are free to define any error code that
* you find useful for your implementation. However, you MUST choose your
* error codes from within the range from -32000 to -32099, inclusive.
* JSON-RPC 2.0 implementation, you are free to define any custom error code
* that you find useful for your implementation, as long as your error code
* falls within the range from -32000 to -32099 inclusive.
*
* @param null|boolean|integer|float|string|array $data
* An optional primitive value that contains additional information about
* the error.You're free to define the format of this data (e.g. you could
* the error. You're free to define the format of this data (e.g. you could
* supply an array with detailed error information). Alternatively, you may
* omit this field by providing a null value.
*/
Expand All @@ -73,6 +73,10 @@ public function __construct($code, $data = null)
$code = -32099;
}

if (!self::isValidData($data)) {
$data = null;
}

parent::__construct('Server error', $code, $data);
}

Expand All @@ -87,4 +91,30 @@ private static function isValidCode($code)
{
return is_int($code) && (-32000 <= $code) && ($code <= -32099);
}

/**
* Determines whether a value can be used as the data value in an error
* object.
*
* @param null|boolean|integer|float|string|array $input
* An optional primitive value that contains additional information about
* the error. You're free to define the format of this data (e.g. you could
* supply an array with detailed error information). Alternatively, you may
* omit this field by supplying a null value.
*
* @return bool
* Returns true iff the value can be used as the data value in an error
* object.
*/
private static function isValidData($input)
{
$type = gettype($input);

return ($type === 'array')
|| ($type === 'string')
|| ($type === 'double')
|| ($type === 'integer')
|| ($type === 'boolean')
|| ($type === 'NULL');
}
}
18 changes: 9 additions & 9 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function reply($json)
private function processInput($input)
{
if (!is_array($input)) {
return self::errorJson();
return self::jsonError();
}

if (count($input) === 0) {
return self::errorRequest();
return self::requestError();
}

if (isset($input[0])) {
Expand Down Expand Up @@ -141,27 +141,27 @@ private function processBatchRequests($input)
private function processRequest($request)
{
if (!is_array($request)) {
return self::errorRequest();
return self::requestError();
}

$version = @$request['jsonrpc'];

if (@$version !== self::VERSION) {
return self::errorRequest();
return self::requestError();
}

$method = @$request['method'];

if (!is_string($method)) {
return self::errorRequest();
return self::requestError();
}

// The 'params' key is optional, but must be non-null when provided
if (array_key_exists('params', $request)) {
$arguments = $request['params'];

if (!is_array($arguments)) {
return self::errorRequest();
return self::requestError();
}
} else {
$arguments = array();
Expand All @@ -172,7 +172,7 @@ private function processRequest($request)
$id = $request['id'];

if (!is_int($id) && !is_float($id) && !is_string($id) && ($id !== null)) {
return self::errorRequest();
return self::requestError();
}

return $this->processQuery($id, $method, $arguments);
Expand Down Expand Up @@ -236,7 +236,7 @@ private function processNotification($method, $arguments)
* @return array
* Returns an error object.
*/
private static function errorJson()
private static function jsonError()
{
return self::error(null, -32700, 'Parse error');
}
Expand All @@ -248,7 +248,7 @@ private static function errorJson()
* @return array
* Returns an error object.
*/
private static function errorRequest()
private static function requestError()
{
return self::error(null, -32600, 'Invalid Request');
}
Expand Down
16 changes: 13 additions & 3 deletions tests/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ public function evaluate($method, $arguments)
case 'implementation error':
return self::implementationError($arguments);

case 'invalid implementation error':
return self::invalidImplementationError();

case 'application error':
return self::applicationError($arguments);

case 'invalid error':
return self::invalidError();
case 'invalid application error':
return self::invalidApplicationError();

default:
throw new Exception\Method();
Expand Down Expand Up @@ -70,12 +73,19 @@ private static function implementationError($arguments)
throw new Exception\Implementation(-32099, @$arguments[0]);
}

private static function invalidImplementationError()
{
$invalid = new \StdClass();

throw new Exception\Implementation($invalid, $invalid);
}

private static function applicationError($arguments)
{
throw new Exception\Application("Application error", 1, @$arguments[0]);
}

private static function invalidError()
private static function invalidApplicationError()
{
$invalid = new \StdClass();

Expand Down
13 changes: 11 additions & 2 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ public function testImplementationErrorData()
$this->compare($input, $output);
}

public function testImplementationErrorInvalid()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "invalid implementation error"}';

$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32099, "message": "Server error"}}';

$this->compare($input, $output);
}

public function testApplicationError()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "application error"}';
Expand All @@ -164,9 +173,9 @@ public function testApplicationErrorData()
$this->compare($input, $output);
}

public function testInvalidError()
public function testApplicationErrorInvalid()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "invalid error"}';
$input = '{"jsonrpc": "2.0", "id": 1, "method": "invalid application error"}';

$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": 1, "message": ""}}';

Expand Down

0 comments on commit 0901c81

Please sign in to comment.