-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic-failure.php
More file actions
32 lines (27 loc) · 1.17 KB
/
Copy pathatomic-failure.php
File metadata and controls
32 lines (27 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use Phore\JsonPatch\{JsonPatch, JsonPatchApplier, JsonValue, PatchApplyOptions, PatchConflictException, PatchTestFailedException};
$original = (object) ['title'=>'Draft', 'version'=>1];
$hash = JsonValue::hash($original);
$applier = new JsonPatchApplier();
try {
$applier->apply($original, JsonPatch::fromArray([
['op'=>'replace', 'path'=>'/title', 'value'=>'Published'],
['op'=>'test', 'path'=>'/version', 'value'=>2],
]));
throw new RuntimeException('Expected test failure.');
} catch (PatchTestFailedException $exception) {
if ($exception->operationIndex !== 1 || JsonValue::hash($original) !== $hash) {
throw new RuntimeException('Atomicity check failed.');
}
echo $exception->errorCode, ': original unchanged', PHP_EOL;
}
$newer = JsonValue::copy($original);
$newer->version = 2;
try {
$applier->apply($newer, new JsonPatch([]), new PatchApplyOptions(expectedHash: $hash));
throw new RuntimeException('Expected hash conflict.');
} catch (PatchConflictException $exception) {
echo $exception->errorCode, ': stale snapshot rejected', PHP_EOL;
}