|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ODM\CouchDB\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ODM\CouchDB\Mapping\ClassMetadata; |
|
6
|
|
|
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService; |
|
7
|
|
|
|
|
8
|
|
|
class ClassMetadataTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function testClassName() |
|
11
|
|
|
{ |
|
12
|
|
|
$cm = new ClassMetadata("Doctrine\Tests\ODM\CouchDB\Mapping\Person"); |
|
13
|
|
|
|
|
14
|
|
|
$this->assertEquals("Doctrine\Tests\ODM\CouchDB\Mapping\Person", $cm->name); |
|
15
|
|
|
|
|
16
|
|
|
return $cm; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @depends testClassName |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testMapFieldWithId($cm) |
|
23
|
|
|
{ |
|
24
|
|
|
$cm->mapField(array('fieldName' => 'id', 'id' => true)); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertArrayHasKey('id', $cm->fieldMappings); |
|
27
|
|
|
$this->assertEquals(array('jsonName' => '_id', 'id' => true, 'type' => 'string', 'fieldName' => 'id'), $cm->fieldMappings['id']); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertEquals('id', $cm->identifier); |
|
30
|
|
|
$this->assertEquals(array('_id' => 'id'), $cm->jsonNames); |
|
31
|
|
|
|
|
32
|
|
|
return $cm; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @depends testMapFieldWithId |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testMapField($cm) |
|
39
|
|
|
{ |
|
40
|
|
|
$cm->mapField(array('fieldName' => 'username', 'type' => 'string')); |
|
41
|
|
|
$cm->mapField(array('fieldName' => 'created', 'type' => 'datetime')); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertArrayHasKey('username', $cm->fieldMappings); |
|
44
|
|
|
$this->assertArrayHasKey('created', $cm->fieldMappings); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertEquals(array('jsonName' => 'username', 'type' => 'string', 'fieldName' => 'username'), $cm->fieldMappings['username']); |
|
47
|
|
|
$this->assertEquals(array('jsonName' => 'created', 'type' => 'datetime', 'fieldName' => 'created'), $cm->fieldMappings['created']); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals(array('_id' => 'id', 'username' => 'username', 'created' => 'created'), $cm->jsonNames); |
|
50
|
|
|
|
|
51
|
|
|
return $cm; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @depends testMapField |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testmapFieldWithoutNameThrowsException($cm) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->setExpectedException('Doctrine\ODM\CouchDB\Mapping\MappingException'); |
|
60
|
|
|
|
|
61
|
|
|
$cm->mapField(array()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @depends testMapField |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testNewInstance($cm) |
|
68
|
|
|
{ |
|
69
|
|
|
$instance1 = $cm->newInstance(); |
|
70
|
|
|
$instance2 = $cm->newInstance(); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertInstanceOf('Doctrine\Tests\ODM\CouchDB\Mapping\Person', $instance1); |
|
73
|
|
|
$this->assertNotSame($instance1, $instance2); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @depends testMapField |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testMapVersionField($cm) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->assertFalse($cm->isVersioned); |
|
82
|
|
|
$cm->mapField(array('fieldName' => 'version', 'jsonName' => '_rev', 'isVersionField' => true)); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertTrue($cm->isVersioned); |
|
85
|
|
|
$this->assertEquals('version', $cm->versionField); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testMapFieldWithoutType_DefaultsToMixed() |
|
89
|
|
|
{ |
|
90
|
|
|
$cm = new ClassMetadata("Doctrine\Tests\ODM\CouchDB\Mapping\Person"); |
|
91
|
|
|
|
|
92
|
|
|
$cm->mapField(array('fieldName' => 'username')); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals(array('jsonName' => 'username', 'type' => 'mixed', 'fieldName' => 'username'), $cm->fieldMappings['username']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param ClassMetadata $cm |
|
99
|
|
|
* @depends testClassName |
|
100
|
|
|
*/ |
|
101
|
|
|
public function testMapAssociationManyToOne($cm) |
|
102
|
|
|
{ |
|
103
|
|
|
$cm->mapManyToOne(array('fieldName' => 'address', 'targetDocument' => 'Doctrine\Tests\ODM\CouchDB\Mapping\Address')); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertArrayHasKey('address', $cm->associationsMappings, "No 'address' in associations map."); |
|
106
|
|
|
$this->assertEquals(array( |
|
107
|
|
|
'fieldName' => 'address', |
|
108
|
|
|
'targetDocument' => 'Doctrine\Tests\ODM\CouchDB\Mapping\Address', |
|
109
|
|
|
'jsonName' => 'address', |
|
110
|
|
|
'sourceDocument' => 'Doctrine\Tests\ODM\CouchDB\Mapping\Person', |
|
111
|
|
|
'isOwning' => true, |
|
112
|
|
|
'type' => ClassMetadata::MANY_TO_ONE, |
|
113
|
|
|
), $cm->associationsMappings['address']); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertArrayHasKey('address', $cm->jsonNames); |
|
116
|
|
|
$this->assertEquals('address', $cm->jsonNames['address']); |
|
117
|
|
|
|
|
118
|
|
|
return $cm; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param ClassMetadata $cm |
|
123
|
|
|
* @depends testClassName |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testMapAttachments($cm) |
|
126
|
|
|
{ |
|
127
|
|
|
$cm->mapAttachments("attachments"); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertTrue($cm->hasAttachments); |
|
130
|
|
|
$this->assertEquals("attachments", $cm->attachmentField); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @depends testClassName |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testSerializeUnserialize() |
|
137
|
|
|
{ |
|
138
|
|
|
$cm = new ClassMetadata("Doctrine\Tests\ODM\CouchDB\Mapping\Person"); |
|
139
|
|
|
$cm->initializeReflection(new RuntimeReflectionService()); |
|
140
|
|
|
$cm->indexed = true; |
|
141
|
|
|
|
|
142
|
|
|
$this->assertEquals("Doctrine\Tests\ODM\CouchDB\Mapping\Person", $cm->name); |
|
143
|
|
|
|
|
144
|
|
|
// property based comparison |
|
145
|
|
|
$unserialized = unserialize(serialize($cm)); |
|
146
|
|
|
$unserialized->wakeupReflection(new RuntimeReflectionService()); |
|
147
|
|
|
$this->assertEquals($cm, $unserialized); |
|
148
|
|
|
|
|
149
|
|
|
$cm->mapField(array('fieldName' => 'id', 'id' => true)); |
|
150
|
|
|
$cm->mapField(array('fieldName' => 'username', 'type' => 'string', 'indexed' => true)); |
|
151
|
|
|
$cm->mapField(array('fieldName' => 'created', 'type' => 'datetime')); |
|
152
|
|
|
$cm->mapField(array('fieldName' => 'version', 'jsonName' => '_rev', 'isVersionField' => true)); |
|
153
|
|
|
$cm->mapManyToOne(array('fieldName' => 'address', 'targetDocument' => 'Doctrine\Tests\ODM\CouchDB\Mapping\Address')); |
|
154
|
|
|
$cm->mapAttachments("attachments"); |
|
155
|
|
|
|
|
156
|
|
|
// @TODO This is the only way the $reflFields property is initialized |
|
157
|
|
|
// as far as I understand the ClassMetadata code. This seems wrong to |
|
158
|
|
|
// call here, but otherwise the comparison fails. The $reflFields are |
|
159
|
|
|
// accesssed all over the code though – I guess there are some bugs |
|
160
|
|
|
// hidden here. |
|
161
|
|
|
$cm->wakeupReflection(new RuntimeReflectionService()); |
|
162
|
|
|
|
|
163
|
|
|
// property based comparison |
|
164
|
|
|
$unserialized = unserialize(serialize($cm)); |
|
165
|
|
|
$unserialized->wakeupReflection(new RuntimeReflectionService()); |
|
166
|
|
|
$this->assertEquals($cm, $unserialized); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @depends testClassName |
|
171
|
|
|
*/ |
|
172
|
|
|
public function testSerializeUnserializeSerializableObject() |
|
173
|
|
|
{ |
|
174
|
|
|
$cm = new ClassMetadata("Doctrine\Tests\ODM\CouchDB\Mapping\SerializableObject"); |
|
175
|
|
|
$cm->initializeReflection(new RuntimeReflectionService()); |
|
176
|
|
|
|
|
177
|
|
|
$cm->mapField(array('fieldName' => 'property', 'type' => 'integer')); |
|
178
|
|
|
|
|
179
|
|
|
// @TODO This is the only way the $reflFields property is initialized |
|
180
|
|
|
// as far as I understand the ClassMetadata code. This seems wrong to |
|
181
|
|
|
// call here, but otherwise the comparison fails. The $reflFields are |
|
182
|
|
|
// accesssed all over the code though – I guess there are some bugs |
|
183
|
|
|
// hidden here. |
|
184
|
|
|
$cm->wakeupReflection(new RuntimeReflectionService()); |
|
185
|
|
|
|
|
186
|
|
|
// property based comparison |
|
187
|
|
|
$unserialized = unserialize(serialize($cm)); |
|
188
|
|
|
$unserialized->wakeupReflection(new RuntimeReflectionService()); |
|
189
|
|
|
$this->assertEquals($cm, $unserialized); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testDeriveChildMetadata() |
|
193
|
|
|
{ |
|
194
|
|
|
$cm = new ClassMetadata("Doctrine\Tests\ODM\CouchDB\Mapping\Person"); |
|
195
|
|
|
$cm->mapField(array('fieldName' => 'id', 'id' => true)); |
|
196
|
|
|
$cm->mapField(array('fieldName' =>'username', 'type' => 'string')); |
|
197
|
|
|
$cm->mapAttachments('attachments'); |
|
198
|
|
|
$cm->mapManyToOne(array('targetDocument' => 'Address', 'fieldName' => 'address')); |
|
199
|
|
|
|
|
200
|
|
|
$child = new ClassMetadata('Doctrine\Tests\ODM\CouchDB\Mapping\Employee'); |
|
201
|
|
|
$cm->deriveChildMetadata($child); |
|
202
|
|
|
|
|
203
|
|
|
$child->mapField(array('fieldName' => 'status', 'type' => 'string')); |
|
204
|
|
|
|
|
205
|
|
|
$this->assertArrayNotHasKey('declared', $child->fieldMappings['status']); |
|
206
|
|
|
|
|
207
|
|
|
$this->assertEquals("Doctrine\Tests\ODM\CouchDB\Mapping\Employee", $child->name); |
|
208
|
|
|
|
|
209
|
|
|
$this->assertArrayHasKey('id', $child->fieldMappings, "id field has to be on child metadata"); |
|
210
|
|
|
$this->assertEquals("Doctrine\Tests\ODM\CouchDB\Mapping\Person", $child->fieldMappings['id']['declared']); |
|
211
|
|
|
|
|
212
|
|
|
$this->assertArrayHasKey('username', $child->fieldMappings, "Username field has to be on child metadata"); |
|
213
|
|
|
$this->assertEquals("Doctrine\Tests\ODM\CouchDB\Mapping\Person", $child->fieldMappings['username']['declared']); |
|
214
|
|
|
|
|
215
|
|
|
$this->assertArrayHasKey('address', $child->associationsMappings, "address association has to be on child metadata"); |
|
216
|
|
|
$this->assertEquals("Doctrine\Tests\ODM\CouchDB\Mapping\Person", $child->associationsMappings['address']['declared']); |
|
217
|
|
|
|
|
218
|
|
|
$this->assertEquals("attachments", $child->attachmentField); |
|
219
|
|
|
$this->assertEquals("Doctrine\Tests\ODM\CouchDB\Mapping\Person", $child->attachmentDeclaredClass); |
|
220
|
|
|
|
|
221
|
|
|
return $child; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
class SerializableObject implements \Serializable |
|
226
|
|
|
{ |
|
227
|
|
|
public $property; |
|
228
|
|
|
|
|
229
|
|
|
public function serialize() |
|
230
|
|
|
{ |
|
231
|
|
|
return serialize(array('property' => $this->property)); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function unserialize($data) |
|
235
|
|
|
{ |
|
236
|
|
|
$values = unserialize($data); |
|
|
|
|
|
|
237
|
|
|
$this->property = $data['property']; |
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
class Person |
|
243
|
|
|
{ |
|
244
|
|
|
public $id; |
|
245
|
|
|
|
|
246
|
|
|
public $username; |
|
247
|
|
|
|
|
248
|
|
|
public $created; |
|
249
|
|
|
|
|
250
|
|
|
public $address; |
|
251
|
|
|
|
|
252
|
|
|
public $version; |
|
253
|
|
|
|
|
254
|
|
|
public $attachments; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
class Address |
|
258
|
|
|
{ |
|
259
|
|
|
public $id; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
class Employee extends Person |
|
263
|
|
|
{ |
|
264
|
|
|
public $status; |
|
265
|
|
|
} |
|
266
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.