Skip to content

Throw exception when field type is incorrect #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/InvalidFieldTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Swaggest\JsonDiff;


use Swaggest\JsonDiff\JsonPatch\OpPath;
use Throwable;

class InvalidFieldTypeException extends Exception
{
/** @var string */
private $field;
/** @var string */
private $expectedType;
/** @var OpPath|object */
private $operation;

/**
* @param string $field
* @param string $expectedType
* @param OpPath|object $operation
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
$field,
$expectedType,
$operation,
$code = 0,
Throwable $previous = null
)
{
parent::__construct(
'Invalid field type - "' . $field . '" should be of type: ' . $expectedType,
$code,
$previous
);
$this->field = $field;
$this->expectedType = $expectedType;
$this->operation = $operation;
}

/**
* @return string
*/
public function getField()
{
return $this->field;
}

/**
* @return string
*/
public function getExpectedType()
{
return $this->expectedType;
}

/**
* @return OpPath|object
*/
public function getOperation()
{
return $this->operation;
}
}
10 changes: 9 additions & 1 deletion src/JsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public static function import(array $data)
if (is_array($operation)) {
$operation = (object)$operation;
}

if (!is_object($operation)) {
throw new Exception('Invalid patch operation - should be a JSON object');
}
Expand All @@ -71,6 +70,13 @@ public static function import(array $data)
throw new MissingFieldException('path', $operation);
}

if (!is_string($operation->op)) {
throw new InvalidFieldTypeException('op', 'string', $operation);
}
if (!is_string($operation->path)) {
throw new InvalidFieldTypeException('path', 'string', $operation);
}

$op = null;
switch ($operation->op) {
case Add::OP:
Expand Down Expand Up @@ -104,6 +110,8 @@ public static function import(array $data)
} elseif ($op instanceof OpPathFrom) {
if (!isset($operation->from)) {
throw new MissingFieldException('from', $operation);
} elseif (!is_string($operation->from)) {
throw new InvalidFieldTypeException('from', 'string', $operation);
}
$op->from = $operation->from;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/src/JsonPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swaggest\JsonDiff\Tests;

use Swaggest\JsonDiff\Exception;
use Swaggest\JsonDiff\InvalidFieldTypeException;
use Swaggest\JsonDiff\JsonDiff;
use Swaggest\JsonDiff\JsonPatch;
use Swaggest\JsonDiff\JsonPatch\OpPath;
Expand Down Expand Up @@ -110,6 +111,52 @@ public function testInvalidOp()
}
}

/**
* @dataProvider provideInvalidFieldType
*
* @param object $operation
* @param string $expectedMessage
* @param string $expectedField
* @param string $expectedType
*/
public function testInvalidFieldType($operation, $expectedMessage, $expectedField, $expectedType)
{
try {
JsonPatch::import(array($operation));
$this->fail('Expected exception was not thrown');
} catch (Exception $exception) {
$this->assertInstanceOf(InvalidFieldTypeException::class, $exception);
$this->assertSame($expectedMessage, $exception->getMessage());
$this->assertSame($expectedField, $exception->getField());
$this->assertSame($expectedType, $exception->getExpectedType());
$this->assertSame($operation, $exception->getOperation());
}
}

public function provideInvalidFieldType()
{
return [
'"op" invalid type' => [
(object)array('op' => array('foo' => 'bar'), 'path' => '/123', 'value' => 'test'),
'Invalid field type - "op" should be of type: string',
'op',
'string'
],
'"path" invalid type' => [
(object)array('op' => 'add', 'path' => array('foo' => 'bar'), 'value' => 'test'),
'Invalid field type - "path" should be of type: string',
'path',
'string'
],
'"from" invalid type' => [
(object)array('op' => 'move', 'path' => '/123', 'from' => array('foo' => 'bar')),
'Invalid field type - "from" should be of type: string',
'from',
'string'
]
];
}

public function testMissingFrom()
{
$this->setExpectedException(get_class(new Exception()), 'Missing "from" in operation data');
Expand Down