Skip to content

Commit 5f297cb

Browse files
committed
Improve PatchTestOperationFailedException
This adds the failed operation and the actual value that failed the test operation check to the exception.
1 parent e618e58 commit 5f297cb

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/JsonPatch.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ public function apply(&$original, $stopOnError = true)
174174
$diff = new JsonDiff($operation->value, $value,
175175
JsonDiff::STOP_ON_DIFF);
176176
if ($diff->getDiffCnt() !== 0) {
177-
throw new PatchTestOperationFailedException('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
178-
. ' failed: ' . json_encode($value));
177+
throw new PatchTestOperationFailedException($operation, $value);
179178
}
180179
break;
181180
}
@@ -189,4 +188,4 @@ public function apply(&$original, $stopOnError = true)
189188
}
190189
return $errors;
191190
}
192-
}
191+
}

src/PatchTestOperationFailedException.php

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

55

6+
use Throwable;
7+
68
class PatchTestOperationFailedException extends Exception
79
{
8-
}
10+
/** @var object */
11+
private $operation;
12+
/** @var string */
13+
private $actualValue;
14+
15+
/**
16+
* @param object $operation
17+
* @param string $actualValue
18+
* @param int $code
19+
* @param Throwable|null $previous
20+
*/
21+
public function __construct(
22+
$operation,
23+
$actualValue,
24+
$code = 0,
25+
Throwable $previous = null
26+
)
27+
{
28+
parent::__construct('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
29+
. ' failed: ' . json_encode($actualValue), $code, $previous);
30+
$this->operation = $operation;
31+
$this->actualValue = $actualValue;
32+
}
33+
34+
/**
35+
* @return object
36+
*/
37+
public function getOperation()
38+
{
39+
return $this->operation;
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function getActualValue()
46+
{
47+
return $this->actualValue;
48+
}
49+
}

tests/src/JsonPatchTest.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,16 @@ public function testApplyNonExistentLevelOne()
162162

163163
public function testTestOperationFailed()
164164
{
165-
$data = array('abc' => 'xyz');
165+
$actualValue = 'xyz';
166+
$data = array('abc' => $actualValue);
167+
$operation = new JsonPatch\Test('/abc', 'def');
168+
166169
$p = new JsonPatch();
167-
$p->op(new JsonPatch\Test('/abc', 'def'));
168-
$errors = $p->apply($data, false);
169-
$this->assertInstanceOf(PatchTestOperationFailedException::class, $errors[0]);
170+
$p->op($operation);
171+
$testError = $p->apply($data, false)[0];
172+
$this->assertInstanceOf(PatchTestOperationFailedException::class, $testError);
173+
$this->assertSame($operation, $testError->getOperation());
174+
$this->assertSame($actualValue, $testError->getActualValue());
170175
}
171176

172177
}

0 commit comments

Comments
 (0)