Skip to content

Commit daa7f05

Browse files
authored
Merge pull request #60 from wmde/master
Throw exception when field type is incorrect
2 parents b9da3c5 + f8fcc60 commit daa7f05

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

src/InvalidFieldTypeException.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff;
4+
5+
6+
use Swaggest\JsonDiff\JsonPatch\OpPath;
7+
use Throwable;
8+
9+
class InvalidFieldTypeException extends Exception
10+
{
11+
/** @var string */
12+
private $field;
13+
/** @var string */
14+
private $expectedType;
15+
/** @var OpPath|object */
16+
private $operation;
17+
18+
/**
19+
* @param string $field
20+
* @param string $expectedType
21+
* @param OpPath|object $operation
22+
* @param int $code
23+
* @param Throwable|null $previous
24+
*/
25+
public function __construct(
26+
$field,
27+
$expectedType,
28+
$operation,
29+
$code = 0,
30+
Throwable $previous = null
31+
)
32+
{
33+
parent::__construct(
34+
'Invalid field type - "' . $field . '" should be of type: ' . $expectedType,
35+
$code,
36+
$previous
37+
);
38+
$this->field = $field;
39+
$this->expectedType = $expectedType;
40+
$this->operation = $operation;
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
public function getField()
47+
{
48+
return $this->field;
49+
}
50+
51+
/**
52+
* @return string
53+
*/
54+
public function getExpectedType()
55+
{
56+
return $this->expectedType;
57+
}
58+
59+
/**
60+
* @return OpPath|object
61+
*/
62+
public function getOperation()
63+
{
64+
return $this->operation;
65+
}
66+
}

src/JsonPatch.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static function import(array $data)
5959
if (is_array($operation)) {
6060
$operation = (object)$operation;
6161
}
62-
6362
if (!is_object($operation)) {
6463
throw new Exception('Invalid patch operation - should be a JSON object');
6564
}
@@ -71,6 +70,13 @@ public static function import(array $data)
7170
throw new MissingFieldException('path', $operation);
7271
}
7372

73+
if (!is_string($operation->op)) {
74+
throw new InvalidFieldTypeException('op', 'string', $operation);
75+
}
76+
if (!is_string($operation->path)) {
77+
throw new InvalidFieldTypeException('path', 'string', $operation);
78+
}
79+
7480
$op = null;
7581
switch ($operation->op) {
7682
case Add::OP:
@@ -104,6 +110,8 @@ public static function import(array $data)
104110
} elseif ($op instanceof OpPathFrom) {
105111
if (!isset($operation->from)) {
106112
throw new MissingFieldException('from', $operation);
113+
} elseif (!is_string($operation->from)) {
114+
throw new InvalidFieldTypeException('from', 'string', $operation);
107115
}
108116
$op->from = $operation->from;
109117
}

tests/src/JsonPatchTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Swaggest\JsonDiff\Tests;
44

55
use Swaggest\JsonDiff\Exception;
6+
use Swaggest\JsonDiff\InvalidFieldTypeException;
67
use Swaggest\JsonDiff\JsonDiff;
78
use Swaggest\JsonDiff\JsonPatch;
89
use Swaggest\JsonDiff\JsonPatch\OpPath;
@@ -110,6 +111,52 @@ public function testInvalidOp()
110111
}
111112
}
112113

114+
/**
115+
* @dataProvider provideInvalidFieldType
116+
*
117+
* @param object $operation
118+
* @param string $expectedMessage
119+
* @param string $expectedField
120+
* @param string $expectedType
121+
*/
122+
public function testInvalidFieldType($operation, $expectedMessage, $expectedField, $expectedType)
123+
{
124+
try {
125+
JsonPatch::import(array($operation));
126+
$this->fail('Expected exception was not thrown');
127+
} catch (Exception $exception) {
128+
$this->assertInstanceOf(InvalidFieldTypeException::class, $exception);
129+
$this->assertSame($expectedMessage, $exception->getMessage());
130+
$this->assertSame($expectedField, $exception->getField());
131+
$this->assertSame($expectedType, $exception->getExpectedType());
132+
$this->assertSame($operation, $exception->getOperation());
133+
}
134+
}
135+
136+
public function provideInvalidFieldType()
137+
{
138+
return [
139+
'"op" invalid type' => [
140+
(object)array('op' => array('foo' => 'bar'), 'path' => '/123', 'value' => 'test'),
141+
'Invalid field type - "op" should be of type: string',
142+
'op',
143+
'string'
144+
],
145+
'"path" invalid type' => [
146+
(object)array('op' => 'add', 'path' => array('foo' => 'bar'), 'value' => 'test'),
147+
'Invalid field type - "path" should be of type: string',
148+
'path',
149+
'string'
150+
],
151+
'"from" invalid type' => [
152+
(object)array('op' => 'move', 'path' => '/123', 'from' => array('foo' => 'bar')),
153+
'Invalid field type - "from" should be of type: string',
154+
'from',
155+
'string'
156+
]
157+
];
158+
}
159+
113160
public function testMissingFrom()
114161
{
115162
$this->setExpectedException(get_class(new Exception()), 'Missing "from" in operation data');

0 commit comments

Comments
 (0)