-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPatchTest.php
153 lines (128 loc) · 4.56 KB
/
PatchTest.php
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace tests\RavenDB\Test\Client;
use RavenDB\Documents\Operations\PatchByQueryOperation;
use RavenDB\Exceptions\Documents\Patching\JavaScriptException;
use tests\RavenDB\RemoteTestBase;
use tests\RavenDB\Infrastructure\Entity\User;
use RavenDB\Documents\Operations\PatchStatus;
use RavenDB\Documents\Operations\PatchRequest;
use RavenDB\Documents\Operations\PatchOperation;
use RavenDB\Documents\Session\IndexesWaitOptsBuilder;
use tests\RavenDB\Test\Client\Indexing\_IndexesFromClientTest\Users_ByName;
class PatchTest extends RemoteTestBase
{
public function testCanPatchSingleDocument(): void
{
$store = $this->getDocumentStore();
try {
$session = $store->openSession();
try {
$user = new User();
$user->setName("RavenDB");
$session->store($user, "users/1");
$session->saveChanges();
} finally {
$session->close();
}
$patchOperation = new PatchOperation("users/1", null,
PatchRequest::forScript("this.name = \"Patched\""));
/** @var PatchStatus $status */
$status = $store->operations()->send($patchOperation);
$this->assertTrue($status->isPatched());
$session = $store->openSession();
try {
$loadedUser = $session->load(User::class, "users/1");
$this->assertEquals("Patched", $loadedUser->getName());
} finally {
$session->close();
}
} finally {
$store->close();
}
}
/** @doesNotPerformAssertions */
public function testCanWaitForIndexAfterPatch(): void
{
$store = $this->getDocumentStore();
try {
(new Users_ByName())->execute($store);
$session = $store->openSession();
try {
$user = new User();
$user->setName("RavenDB");
$session->store($user, "users/1");
$session->saveChanges();
} finally {
$session->close();
}
$session = $store->openSession();
try {
$session->advanced()->waitForIndexesAfterSaveChanges(function(IndexesWaitOptsBuilder $x) {
$x->waitForIndexes("Users/ByName");
});
/** @var User $user */
$user = $session->load(User::class, "users/1");
$session->advanced()->patch($user, "name", "New Name");
$session->saveChanges();
} finally {
$session->close();
}
} finally {
$store->close();
}
}
// @todo: implement this test
public function canPatchManyDocuments(): void
{
$store = $this->getDocumentStore();
try {
$session = $store->openSession();
try {
$user = new User();
$user->setName("RavenDB");
$session->store($user, "users/1");
$session->saveChanges();
// $this->assertEquals(1, $session->query(User::class)->countLazily()->getValue());
} finally {
$session->close();
}
// $operation = new PatchByQueryOperation("from Users update { this.name= \"Patched\" }");
//
// Operation op = store.operations().sendAsync(operation);
//
// op.waitForCompletion();
$session = $store->openSession();
try {
// User loadedUser = session.load(User.class, "users/1");
//
// assertThat(loadedUser.getName())
// .isEqualTo("Patched");
} finally {
$session->close();
}
} finally {
$store->close();
}
}
public function testThrowsOnInvalidScript(): void
{
$store = $this->getDocumentStore();
try {
$session = $store->openSession();
try {
$user = new User();
$user->setName("RavenDB");
$session->store($user, "users/1");
$session->saveChanges();
} finally {
$session->close();
}
$operation = new PatchByQueryOperation("from Users update { throw 5 }");
$op = $store->operations()->sendAsync($operation);
$this->expectException(JavaScriptException::class);
$op->waitForCompletion();
} finally {
$store->close();
}
}
}