1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ODM\CouchDB\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\CouchDB\InvalidDocumentTypeException; |
6
|
|
|
|
7
|
|
|
class BasicCrudTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var DocumentManager |
11
|
|
|
*/ |
12
|
|
|
private $dm; |
13
|
|
|
|
14
|
|
|
private $type; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
$this->type = 'Doctrine\Tests\ODM\CouchDB\Functional\User'; |
19
|
|
|
$this->dm = $this->createDocumentManager(); |
|
|
|
|
20
|
|
|
|
21
|
|
|
$httpClient = $this->dm->getHttpClient(); |
22
|
|
|
|
23
|
|
|
$data = json_encode( |
24
|
|
|
array( |
25
|
|
|
'_id' => "1", |
26
|
|
|
'username' => 'lsmith', |
27
|
|
|
'type' => $this->type, |
28
|
|
|
) |
29
|
|
|
); |
30
|
|
|
$resp = $httpClient->request('PUT', '/' . $this->dm->getDatabase() . '/1', $data); |
31
|
|
|
$this->assertEquals(201, $resp->status); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testFind() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$user = $this->dm->find($this->type, 1); |
37
|
|
|
|
38
|
|
|
$this->assertInstanceOf($this->type, $user); |
39
|
|
|
$this->assertEquals('1', $user->id); |
40
|
|
|
$this->assertEquals('lsmith', $user->username); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testFindUsesIdentityMap() |
44
|
|
|
{ |
45
|
|
|
$user1 = $this->dm->find($this->type, 1); |
46
|
|
|
$user2 = $this->dm->find($this->type, 1); |
47
|
|
|
|
48
|
|
|
$this->assertSame($user1, $user2); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testInsert() |
52
|
|
|
{ |
53
|
|
|
$user = new User(); |
54
|
|
|
$user->id = "myuser-1234"; |
55
|
|
|
$user->username = "test"; |
56
|
|
|
|
57
|
|
|
$this->dm->persist($user); |
58
|
|
|
$this->dm->flush(); |
59
|
|
|
$this->dm->clear(); |
60
|
|
|
|
61
|
|
|
$userNew = $this->dm->find($this->type, $user->id); |
62
|
|
|
|
63
|
|
|
$this->assertNotNull($userNew, "Have to hydrate user object!"); |
64
|
|
|
$this->assertEquals($user->id, $userNew->id); |
65
|
|
|
$this->assertEquals($user->username, $userNew->username); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testDelete() |
69
|
|
|
{ |
70
|
|
|
$user = $this->dm->find($this->type, 1); |
71
|
|
|
|
72
|
|
|
$this->dm->remove($user); |
73
|
|
|
$this->dm->flush(); |
74
|
|
|
$this->dm->clear(); |
75
|
|
|
|
76
|
|
|
$userRemoved = $this->dm->find($this->type, 1); |
77
|
|
|
|
78
|
|
|
$this->assertNull($userRemoved, "Have to delete user object!"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testDeleteProxy() |
82
|
|
|
{ |
83
|
|
|
$user = $this->dm->getReference($this->type, 1); |
84
|
|
|
|
85
|
|
|
$this->dm->remove($user); |
86
|
|
|
$this->dm->flush(); |
87
|
|
|
$this->dm->clear(); |
88
|
|
|
|
89
|
|
|
$userRemoved = $this->dm->find($this->type, 1); |
90
|
|
|
|
91
|
|
|
$this->assertNull($userRemoved, "Have to delete user object!"); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testUpdate1() |
95
|
|
|
{ |
96
|
|
|
$user = new User(); |
97
|
|
|
$user->id = "myuser-1234"; |
98
|
|
|
$user->username = "test"; |
99
|
|
|
|
100
|
|
|
$this->dm->persist($user); |
101
|
|
|
$this->dm->flush(); |
102
|
|
|
$this->dm->clear(); |
103
|
|
|
|
104
|
|
|
$user = $this->dm->find($this->type, $user->id); |
105
|
|
|
$user->username = "test2"; |
106
|
|
|
|
107
|
|
|
$this->dm->flush(); |
108
|
|
|
$this->dm->clear(); |
109
|
|
|
|
110
|
|
|
$userNew = $this->dm->find($this->type, $user->id); |
111
|
|
|
|
112
|
|
|
$this->assertEquals($user->username, $userNew->username); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function testUpdate2() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$user = $this->dm->find($this->type, 1); |
118
|
|
|
$user->username = "new-name"; |
119
|
|
|
|
120
|
|
|
$this->dm->flush(); |
121
|
|
|
$this->dm->clear(); |
122
|
|
|
|
123
|
|
|
$newUser = $this->dm->find($this->type, 1); |
124
|
|
|
$this->assertEquals('new-name', $newUser->username); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testRemove() |
128
|
|
|
{ |
129
|
|
|
$user = $this->dm->find($this->type, 1); |
130
|
|
|
|
131
|
|
|
$this->dm->remove($user); |
132
|
|
|
$this->dm->flush(); |
133
|
|
|
|
134
|
|
|
$newUser = $this->dm->find($this->type, 1); |
135
|
|
|
$this->assertNull($newUser); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testInsertUpdateMultiple() |
139
|
|
|
{ |
140
|
|
|
$user1 = $this->dm->find($this->type, 1); |
141
|
|
|
$user1->username = "new-name"; |
142
|
|
|
|
143
|
|
|
$user2 = new User(); |
144
|
|
|
$user2->id = "myuser-1111"; |
145
|
|
|
$user2->username = "test"; |
146
|
|
|
|
147
|
|
|
$user3 = new User(); |
148
|
|
|
$user3->id = "myuser-2222"; |
149
|
|
|
$user3->username = "test"; |
150
|
|
|
|
151
|
|
|
$this->dm->persist($user2); |
152
|
|
|
$this->dm->persist($user3); |
153
|
|
|
$this->dm->flush(); |
154
|
|
|
$this->dm->clear(); |
155
|
|
|
|
156
|
|
|
$pUser1 = $this->dm->find($this->type, 1); |
157
|
|
|
$pUser2 = $this->dm->find($this->type, 'myuser-1111'); |
158
|
|
|
$pUser3 = $this->dm->find($this->type, 'myuser-2222'); |
159
|
|
|
|
160
|
|
|
$this->assertEquals('new-name', $pUser1->username); |
161
|
|
|
$this->assertEquals('myuser-1111', $pUser2->id); |
162
|
|
|
$this->assertEquals('myuser-2222', $pUser3->id); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testFindTypeValidation() |
166
|
|
|
{ |
167
|
|
|
$this->setExpectedException('Doctrine\ODM\CouchDB\InvalidDocumentTypeException'); |
168
|
|
|
$user = $this->dm->find($this->type.'2', 1); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
public function testNullConversionHandledAutomatically() |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$user1 = $this->dm->find($this->type, 1); |
174
|
|
|
$user1->username = null; |
175
|
|
|
|
176
|
|
|
$this->dm->flush(); |
177
|
|
|
$this->dm->clear(); |
178
|
|
|
|
179
|
|
|
$pUser1 = $this->dm->find($this->type, 1); |
180
|
|
|
|
181
|
|
|
$this->assertNull($pUser1->username); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testFindWrongTypesDoesNotMessUnitOfWork() |
185
|
|
|
{ |
186
|
|
|
try { |
187
|
|
|
$user1 = $this->dm->find(__NAMESPACE__ . '\\User2', 1); |
|
|
|
|
188
|
|
|
|
189
|
|
|
$this->fail('Expecting InvalidDocumentTypeException'); |
190
|
|
|
} catch (InvalidDocumentTypeException $e) { |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$user1 = $this->dm->find(__NAMESPACE__ . '\\User', 1); |
194
|
|
|
|
195
|
|
|
$this->assertEquals('lsmith', $user1->username); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testKeepTrackOfUnmappedData() |
199
|
|
|
{ |
200
|
|
|
$httpClient = $this->dm->getHttpClient(); |
201
|
|
|
|
202
|
|
|
$data = array( |
203
|
|
|
'_id' => "2", |
204
|
|
|
'username' => 'beberlei', |
205
|
|
|
'email' => '[email protected]', |
206
|
|
|
'address' => array('city' => 'Bonn', 'country' => 'DE'), |
207
|
|
|
'type' => str_replace("\\", ".", $this->type), |
208
|
|
|
); |
209
|
|
|
$resp = $httpClient->request('PUT', '/' . $this->dm->getDatabase() . '/2', json_encode($data)); |
210
|
|
|
$this->assertEquals(201, $resp->status); |
211
|
|
|
|
212
|
|
|
$user = $this->dm->find($this->type, 2); |
213
|
|
|
$this->assertInstanceOf($this->type, $user); |
214
|
|
|
$this->assertEquals('beberlei', $user->username); |
215
|
|
|
|
216
|
|
|
$user->username = 'beberlei2'; |
217
|
|
|
$this->dm->flush(); |
218
|
|
|
|
219
|
|
|
$resp = $httpClient->request('GET', '/' . $this->dm->getDatabase() . '/2'); |
220
|
|
|
$this->assertEquals(200, $resp->status); |
221
|
|
|
|
222
|
|
|
$data['username'] = 'beberlei2'; |
223
|
|
|
|
224
|
|
|
ksort($resp->body); |
225
|
|
|
ksort($data); |
226
|
|
|
unset($resp->body['_rev']); |
227
|
|
|
|
228
|
|
|
$this->assertEquals($data, $resp->body); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @Document |
234
|
|
|
*/ |
235
|
|
|
class User |
236
|
|
|
{ |
237
|
|
|
/** @Id(strategy="ASSIGNED") */ |
238
|
|
|
public $id; |
239
|
|
|
/** @Field(type="string") */ |
240
|
|
|
public $username; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @Document |
245
|
|
|
*/ |
246
|
|
|
class User2 |
247
|
|
|
{ |
248
|
|
|
/** @Id(strategy="ASSIGNED") */ |
249
|
|
|
public $id; |
250
|
|
|
/** @Field(type="string") */ |
251
|
|
|
public $username; |
252
|
|
|
} |
253
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..