Skip to content

Commit 02aabab

Browse files
committed
Throw different exception for failed test op
Having this additional exception type allows users to distinguish between apply() errors due to the patch not being applicable and failed test operations.
1 parent 6d7c9d5 commit 02aabab

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/JsonPatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function apply(&$original, $stopOnError = true)
170170
$diff = new JsonDiff($operation->value, $value,
171171
JsonDiff::STOP_ON_DIFF);
172172
if ($diff->getDiffCnt() !== 0) {
173-
throw new Exception('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
173+
throw new PatchTestOperationFailedException('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
174174
. ' failed: ' . json_encode($value));
175175
}
176176
break;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff;
4+
5+
6+
class PatchTestOperationFailedException extends Exception
7+
{
8+
}

tests/src/JsonPatchTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Swaggest\JsonDiff\Exception;
66
use Swaggest\JsonDiff\JsonDiff;
77
use Swaggest\JsonDiff\JsonPatch;
8+
use Swaggest\JsonDiff\PatchTestOperationFailedException;
89

910
class JsonPatchTest extends \PHPUnit_Framework_TestCase
1011
{
@@ -142,4 +143,13 @@ public function testApplyNonExistentLevelOne()
142143
$this->assertEquals((object)array('some' => 22), $data);
143144
}
144145

146+
public function testTestOperationFailed()
147+
{
148+
$data = array('abc' => 'xyz');
149+
$p = new JsonPatch();
150+
$p->op(new JsonPatch\Test('/abc', 'def'));
151+
$errors = $p->apply($data, false);
152+
$this->assertInstanceOf(PatchTestOperationFailedException::class, $errors[0]);
153+
}
154+
145155
}

0 commit comments

Comments
 (0)