1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\ODataMetadata\Tests; |
4
|
|
|
|
5
|
|
|
use AlgoWeb\ODataMetadata\IsOK; |
6
|
|
|
use AlgoWeb\ODataMetadata\MetadataManager; |
7
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer; |
8
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\Schema; |
9
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationType; |
10
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType; |
11
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReturnTypeType; |
12
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionType; |
13
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx; |
14
|
|
|
use Mockery as m; |
15
|
|
|
|
16
|
|
|
class MetadataManagerTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
public function testIsOKAtDefault() |
19
|
|
|
{ |
20
|
|
|
$ds = DIRECTORY_SEPARATOR; |
|
|
|
|
21
|
|
|
$metadataManager = new MetadataManager(); |
22
|
|
|
$msg = null; |
23
|
|
|
$edmx = $metadataManager->getEdmx(); |
24
|
|
|
$this->assertTrue($edmx->isOK($msg), $msg); |
25
|
|
|
$this->assertNull($msg); |
26
|
|
|
|
27
|
|
|
$d = $metadataManager->getEdmxXML(); |
28
|
|
|
$this->v3MetadataAgainstXSD($d); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function v3MetadataAgainstXSD($data) |
32
|
|
|
{ |
33
|
|
|
$ds = DIRECTORY_SEPARATOR; |
34
|
|
|
|
35
|
|
|
$goodxsd = dirname(__DIR__) . $ds . "xsd" . $ds . "Microsoft.Data.Entity.Design.Edmx_3.Fixed.xsd"; |
36
|
|
|
if (!file_exists($goodxsd)) { |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
$xml = new \DOMDocument(); |
40
|
|
|
$xml->loadXML($data); |
41
|
|
|
return $xml->schemaValidate($goodxsd); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testEntitysAndProperties() |
45
|
|
|
{ |
46
|
|
|
$metadataManager = new MetadataManager(); |
47
|
|
|
$result = null; |
48
|
|
|
|
49
|
|
|
list($eType, $result) = $metadataManager->addEntityType("Category"); |
50
|
|
|
$this->assertNotFalse($eType, "Etype is false not type " . $metadataManager->getLastError()); |
51
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CategoryID", "Int32", null, false, true, "Identity"); |
52
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CategoryName", "String"); |
53
|
|
|
$metadataManager->addPropertyToEntityType($eType, "Description", "String"); |
54
|
|
|
$metadataManager->addPropertyToEntityType($eType, "Picture", "Binary"); |
55
|
|
|
|
56
|
|
|
list($eType, $result) = $metadataManager->addEntityType("CustomerDemographic"); |
57
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CustomerTypeID", "String", null, false, true); |
58
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CustomerDesc", "String"); |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$msg = null; |
62
|
|
|
$edmx = $metadataManager->getEdmx(); |
63
|
|
|
$this->assertTrue($edmx->isOK($msg), $msg); |
64
|
|
|
$this->assertNull($msg); |
65
|
|
|
|
66
|
|
|
$d = $metadataManager->getEdmxXML(); |
67
|
|
|
$this->v3MetadataAgainstXSD($d); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testEntitysAndPropertiesAndNavigationProperties() |
71
|
|
|
{ |
72
|
|
|
$msg = null; |
73
|
|
|
$metadataManager = new MetadataManager(); |
74
|
|
|
$result = null; |
75
|
|
|
|
76
|
|
|
list($CategoryType, $result) = $metadataManager->addEntityType("Category"); |
77
|
|
|
$this->assertNotFalse($CategoryType, "Etype is false not type " . $metadataManager->getLastError()); |
78
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "CategoryID", "Int32", null, false, true, "Identity"); |
79
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "CategoryName", "String"); |
80
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "Description", "String"); |
81
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "Picture", "Binary"); |
82
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
83
|
|
|
|
84
|
|
|
list($CustomerDemographicType, $result) = $metadataManager->addEntityType("CustomerDemographic"); |
85
|
|
|
$metadataManager->addPropertyToEntityType($CustomerDemographicType, "CustomerTypeID", "String", null, false, true); |
86
|
|
|
$metadataManager->addPropertyToEntityType($CustomerDemographicType, "CustomerDesc", "String"); |
87
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
88
|
|
|
|
89
|
|
|
list($CustomerType, $result) = $metadataManager->addEntityType("Customer"); |
90
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "CustomerID", "String", null, false, true); |
91
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "CompanyName", "String"); |
92
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "ContactName", "String"); |
93
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "ContactTitle", "String"); |
94
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Address", "String"); |
95
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "City", "String"); |
96
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Region", "String"); |
97
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "PostalCode", "String"); |
98
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Country", "String"); |
99
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Phone", "String"); |
100
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Fax", "String"); |
101
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
102
|
|
|
|
103
|
|
|
list($EmployeeType, $result) = $metadataManager->addEntityType("Employee"); |
104
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "EmployeeID", "Int32", null, false, true, "Identity"); |
105
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "LastName", "String"); |
106
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "FirstName", "String"); |
107
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Title", "String"); |
108
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "TitleOfCourtesy", "String"); |
109
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "BirthDate", "DateTime"); |
110
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "HireDate", "DateTime"); |
111
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Address", "String"); |
112
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "City", "String"); |
113
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Region", "String"); |
114
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "PostalCode", "String"); |
115
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Country", "String"); |
116
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "HomePhone", "String"); |
117
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Extension", "String"); |
118
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Photo", "Binary"); |
119
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Notes", "String"); |
120
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "ReportsTo", "Int32"); |
121
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "PhotoPath", "String"); |
122
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
123
|
|
|
|
124
|
|
|
list($Order_DetailType, $result) = $metadataManager->addEntityType("Order_Detail"); |
125
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "OrderID", "Int32", null, false, true); |
126
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "ProductID", "Int32", null, false, true); |
127
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "UnitPrice", "Decimal"); |
128
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "Quantity", "Int16"); |
129
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "Discount", "Single"); |
130
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
131
|
|
|
|
132
|
|
|
list($OrderType, $result) = $metadataManager->addEntityType("Order"); |
133
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "OrderID", "Int32", null, false, true, "Identity"); |
134
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "CustomerID", "String"); |
135
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "EmployeeID", "Int32"); |
136
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "OrderDate", "DateTime"); |
137
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "RequiredDate", "DateTime"); |
138
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShippedDate", "DateTime"); |
139
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipVia", "DateTime"); |
140
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "Freight", "Decimal"); |
141
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipName", "String"); |
142
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipAddress", "String"); |
143
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipCity", "String"); |
144
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipRegion", "String"); |
145
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipPostalCode", "String"); |
146
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipCountry", "String"); |
147
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
148
|
|
|
|
149
|
|
|
list($ProductType, $result) = $metadataManager->addEntityType("Product"); |
150
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "ProductID", "Int32", null, false, true, "Identity"); |
151
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "ProductName", "String"); |
152
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "SupplierID", "Int32"); |
153
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "CategoryID", "Int32"); |
154
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "QuantityPerUnit", "String"); |
155
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "UnitPrice", "Decimal"); |
156
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "UnitsInStock", "Int16"); |
157
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "UnitsOnOrder", "Int16"); |
158
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "ReorderLevel", "Int16"); |
159
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "Discontinued", "Boolean"); |
160
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
$metadataManager->addNavigationPropertyToEntityType( |
164
|
|
|
$CategoryType, "*", "Products", $ProductType, "1", "Category", ["CategoryID"], ["CategoryID"] |
165
|
|
|
); |
166
|
|
|
$metadataManager->addNavigationPropertyToEntityType( |
167
|
|
|
$Order_DetailType, "1", "Order", $ProductType, "*", "Order_Details", ["OrderID"], ["CategoryID"] |
168
|
|
|
); |
169
|
|
|
// <NavigationProperty Name="Order_Details" Relationship="NorthwindModel.FK_Order_Details_Products" ToRole="Order_Details" FromRole="Products"/> |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
$msg = null; |
173
|
|
|
$edmx = $metadataManager->getEdmx(); |
174
|
|
|
$this->assertTrue($edmx->isOK($msg), $msg); |
175
|
|
|
$this->assertNull($msg); |
176
|
|
|
|
177
|
|
|
$d = $metadataManager->getEdmxXML(); |
178
|
|
|
$this->v3MetadataAgainstXSD($d); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
View Code Duplication |
public function testAddManyToManyNavProperty() |
182
|
|
|
{ |
183
|
|
|
list($msg, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
184
|
|
|
|
185
|
|
|
list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType( |
186
|
|
|
$CategoryType, |
187
|
|
|
"*", |
188
|
|
|
"Customers", |
189
|
|
|
$CustomerType, |
190
|
|
|
"*", |
191
|
|
|
"Categories" |
192
|
|
|
); |
193
|
|
|
$this->assertEquals($principal->getFromRole(), $dependent->getToRole()); |
194
|
|
|
$this->assertEquals($dependent->getFromRole(), $principal->getToRole()); |
195
|
|
|
$this->assertEquals("Customers", $principal->getName()); |
196
|
|
|
$this->assertEquals("Categories", $dependent->getName()); |
197
|
|
|
|
198
|
|
|
$navProps = [$principal, $dependent]; |
199
|
|
|
$assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation(); |
200
|
|
|
$this->assertEquals(1, count($assoc)); |
201
|
|
|
$assoc = $assoc[0]; |
202
|
|
|
$this->assertTrue($assoc instanceof TAssociationType); |
203
|
|
|
$this->assertTrue($assoc->isOK($msg), $msg); |
204
|
|
|
|
205
|
|
|
$this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship()); |
206
|
|
|
$ends = $assoc->getEnd(); |
207
|
|
|
|
208
|
|
|
$this->assertEquals(2, count($ends)); |
209
|
|
|
foreach ($navProps as $prop) { |
210
|
|
|
$fromMatch = $ends[0]->getRole() == $prop->getToRole() |
211
|
|
|
|| $ends[1]->getRole() == $prop->getToRole(); |
212
|
|
|
$this->assertTrue($fromMatch, "toRole must match at least one end role"); |
213
|
|
|
if ($ends[0]->getRole() == $prop->getToRole()) { |
214
|
|
|
$this->assertEquals($ends[1]->getRole(), $prop->getFromRole()); |
215
|
|
|
$this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole()); |
216
|
|
|
} else { |
217
|
|
|
$this->assertEquals($ends[0]->getRole(), $prop->getFromRole()); |
218
|
|
|
$this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole()); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
$principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1]; |
222
|
|
|
$this->assertEquals('*', $principalEnd->getMultiplicity()); |
223
|
|
|
$dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1]; |
224
|
|
|
$this->assertEquals('*', $dependentEnd->getMultiplicity()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
View Code Duplication |
public function testAddOneToManyNavProperty() |
228
|
|
|
{ |
229
|
|
|
list($msg, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
230
|
|
|
|
231
|
|
|
list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType( |
232
|
|
|
$CategoryType, |
233
|
|
|
"*", |
234
|
|
|
"Customers", |
235
|
|
|
$CustomerType, |
236
|
|
|
"1", |
237
|
|
|
"Categories" |
238
|
|
|
); |
239
|
|
|
$this->assertEquals($principal->getFromRole(), $dependent->getToRole()); |
240
|
|
|
$this->assertEquals($dependent->getFromRole(), $principal->getToRole()); |
241
|
|
|
$this->assertEquals("Customers", $principal->getName()); |
242
|
|
|
$this->assertEquals("Categories", $dependent->getName()); |
243
|
|
|
|
244
|
|
|
$navProps = [$principal, $dependent]; |
245
|
|
|
$assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation(); |
246
|
|
|
$this->assertEquals(1, count($assoc)); |
247
|
|
|
$assoc = $assoc[0]; |
248
|
|
|
$this->assertTrue($assoc instanceof TAssociationType); |
249
|
|
|
$this->assertTrue($assoc->isOK($msg), $msg); |
250
|
|
|
|
251
|
|
|
$this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship()); |
252
|
|
|
$ends = $assoc->getEnd(); |
253
|
|
|
|
254
|
|
|
$this->assertEquals(2, count($ends)); |
255
|
|
|
foreach ($navProps as $prop) { |
256
|
|
|
$fromMatch = $ends[0]->getRole() == $prop->getToRole() |
257
|
|
|
|| $ends[1]->getRole() == $prop->getToRole(); |
258
|
|
|
$this->assertTrue($fromMatch, "toRole must match at least one end role"); |
259
|
|
|
if ($ends[0]->getRole() == $prop->getToRole()) { |
260
|
|
|
$this->assertEquals($ends[1]->getRole(), $prop->getFromRole()); |
261
|
|
|
$this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole()); |
262
|
|
|
} else { |
263
|
|
|
$this->assertEquals($ends[0]->getRole(), $prop->getFromRole()); |
264
|
|
|
$this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole()); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
$principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1]; |
268
|
|
|
$this->assertEquals('*', $principalEnd->getMultiplicity()); |
269
|
|
|
$dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1]; |
270
|
|
|
$this->assertEquals('1', $dependentEnd->getMultiplicity()); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
View Code Duplication |
public function testAddManyToOneNavProperty() |
274
|
|
|
{ |
275
|
|
|
list($msg, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
276
|
|
|
|
277
|
|
|
list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType( |
278
|
|
|
$CategoryType, |
279
|
|
|
"1", |
280
|
|
|
"Customers", |
281
|
|
|
$CustomerType, |
282
|
|
|
"*", |
283
|
|
|
"Categories" |
284
|
|
|
); |
285
|
|
|
$this->assertEquals($principal->getFromRole(), $dependent->getToRole()); |
286
|
|
|
$this->assertEquals($dependent->getFromRole(), $principal->getToRole()); |
287
|
|
|
$this->assertEquals("Customers", $principal->getName()); |
288
|
|
|
$this->assertEquals("Categories", $dependent->getName()); |
289
|
|
|
|
290
|
|
|
$navProps = [$principal, $dependent]; |
291
|
|
|
$assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation(); |
292
|
|
|
$this->assertEquals(1, count($assoc)); |
293
|
|
|
$assoc = $assoc[0]; |
294
|
|
|
$this->assertTrue($assoc instanceof TAssociationType); |
295
|
|
|
$this->assertTrue($assoc->isOK($msg), $msg); |
296
|
|
|
|
297
|
|
|
$this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship()); |
298
|
|
|
$ends = $assoc->getEnd(); |
299
|
|
|
|
300
|
|
|
$this->assertEquals(2, count($ends)); |
301
|
|
|
foreach ($navProps as $prop) { |
302
|
|
|
$fromMatch = $ends[0]->getRole() == $prop->getToRole() |
303
|
|
|
|| $ends[1]->getRole() == $prop->getToRole(); |
304
|
|
|
$this->assertTrue($fromMatch, "toRole must match at least one end role"); |
305
|
|
|
if ($ends[0]->getRole() == $prop->getToRole()) { |
306
|
|
|
$this->assertEquals($ends[1]->getRole(), $prop->getFromRole()); |
307
|
|
|
$this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole()); |
308
|
|
|
} else { |
309
|
|
|
$this->assertEquals($ends[0]->getRole(), $prop->getFromRole()); |
310
|
|
|
$this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole()); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
$principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1]; |
314
|
|
|
$this->assertEquals('1', $principalEnd->getMultiplicity()); |
315
|
|
|
$dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1]; |
316
|
|
|
$this->assertEquals('*', $dependentEnd->getMultiplicity()); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
View Code Duplication |
public function testAddOneToOneForwardNavProperty() |
320
|
|
|
{ |
321
|
|
|
list($msg, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
322
|
|
|
|
323
|
|
|
list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType( |
324
|
|
|
$CategoryType, |
325
|
|
|
"0..1", |
326
|
|
|
"Customers", |
327
|
|
|
$CustomerType, |
328
|
|
|
"1", |
329
|
|
|
"Categories" |
330
|
|
|
); |
331
|
|
|
$this->assertEquals($principal->getFromRole(), $dependent->getToRole()); |
332
|
|
|
$this->assertEquals($dependent->getFromRole(), $principal->getToRole()); |
333
|
|
|
$this->assertEquals("Customers", $principal->getName()); |
334
|
|
|
$this->assertEquals("Categories", $dependent->getName()); |
335
|
|
|
|
336
|
|
|
$navProps = [$principal, $dependent]; |
337
|
|
|
$assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation(); |
338
|
|
|
$this->assertEquals(1, count($assoc)); |
339
|
|
|
$assoc = $assoc[0]; |
340
|
|
|
$this->assertTrue($assoc instanceof TAssociationType); |
341
|
|
|
$this->assertTrue($assoc->isOK($msg), $msg); |
342
|
|
|
|
343
|
|
|
$this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship()); |
344
|
|
|
$ends = $assoc->getEnd(); |
345
|
|
|
|
346
|
|
|
$this->assertEquals(2, count($ends)); |
347
|
|
|
foreach ($navProps as $prop) { |
348
|
|
|
$fromMatch = $ends[0]->getRole() == $prop->getToRole() |
349
|
|
|
|| $ends[1]->getRole() == $prop->getToRole(); |
350
|
|
|
$this->assertTrue($fromMatch, "toRole must match at least one end role"); |
351
|
|
|
if ($ends[0]->getRole() == $prop->getToRole()) { |
352
|
|
|
$this->assertEquals($ends[1]->getRole(), $prop->getFromRole()); |
353
|
|
|
$this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole()); |
354
|
|
|
} else { |
355
|
|
|
$this->assertEquals($ends[0]->getRole(), $prop->getFromRole()); |
356
|
|
|
$this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole()); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
$principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1]; |
360
|
|
|
$this->assertEquals('0..1', $principalEnd->getMultiplicity()); |
361
|
|
|
$dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1]; |
362
|
|
|
$this->assertEquals('1', $dependentEnd->getMultiplicity()); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
View Code Duplication |
public function testAddOneToOneReverseNavProperty() |
366
|
|
|
{ |
367
|
|
|
list($msg, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
368
|
|
|
|
369
|
|
|
list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType( |
370
|
|
|
$CategoryType, |
371
|
|
|
"1", |
372
|
|
|
"Customers", |
373
|
|
|
$CustomerType, |
374
|
|
|
"0..1", |
375
|
|
|
"Categories" |
376
|
|
|
); |
377
|
|
|
$this->assertEquals($principal->getFromRole(), $dependent->getToRole()); |
378
|
|
|
$this->assertEquals($dependent->getFromRole(), $principal->getToRole()); |
379
|
|
|
$this->assertEquals("Customers", $principal->getName()); |
380
|
|
|
$this->assertEquals("Categories", $dependent->getName()); |
381
|
|
|
|
382
|
|
|
$navProps = [$principal, $dependent]; |
383
|
|
|
$assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation(); |
384
|
|
|
$this->assertEquals(1, count($assoc)); |
385
|
|
|
$assoc = $assoc[0]; |
386
|
|
|
$this->assertTrue($assoc instanceof TAssociationType); |
387
|
|
|
$this->assertTrue($assoc->isOK($msg), $msg); |
388
|
|
|
|
389
|
|
|
$this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship()); |
390
|
|
|
$ends = $assoc->getEnd(); |
391
|
|
|
|
392
|
|
|
$this->assertEquals(2, count($ends)); |
393
|
|
|
foreach ($navProps as $prop) { |
394
|
|
|
$fromMatch = $ends[0]->getRole() == $prop->getToRole() |
395
|
|
|
|| $ends[1]->getRole() == $prop->getToRole(); |
396
|
|
|
$this->assertTrue($fromMatch, "toRole must match at least one end role"); |
397
|
|
|
if ($ends[0]->getRole() == $prop->getToRole()) { |
398
|
|
|
$this->assertEquals($ends[1]->getRole(), $prop->getFromRole()); |
399
|
|
|
$this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole()); |
400
|
|
|
} else { |
401
|
|
|
$this->assertEquals($ends[0]->getRole(), $prop->getFromRole()); |
402
|
|
|
$this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole()); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
$principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1]; |
406
|
|
|
$this->assertEquals('1', $principalEnd->getMultiplicity()); |
407
|
|
|
$dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1]; |
408
|
|
|
$this->assertEquals('0..1', $dependentEnd->getMultiplicity()); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
public function testMetadataSerialiseRoundTrip() |
412
|
|
|
{ |
413
|
|
|
$bar = new MetadataManager(); |
414
|
|
|
$foo = new MetadataManager(); |
415
|
|
|
|
416
|
|
|
$cereal = serialize($foo); |
417
|
|
|
|
418
|
|
|
$foo = unserialize($cereal); |
419
|
|
|
$this->assertTrue(null != $foo->getSerialiser()); |
420
|
|
|
$this->assertEquals($bar, $foo); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
public function testCreateSingletonBadReturnType() |
424
|
|
|
{ |
425
|
|
|
$returnType = m::mock(IsOK::class); |
426
|
|
|
$foo = new MetadataManager(); |
427
|
|
|
|
428
|
|
|
$expected = "Expected return type must be either TEntityType or TComplexType"; |
429
|
|
|
$actual = null; |
430
|
|
|
|
431
|
|
|
try { |
432
|
|
|
$foo->createSingleton(null, $returnType); |
433
|
|
|
} catch (\InvalidArgumentException $e) { |
434
|
|
|
$actual = $e->getMessage(); |
435
|
|
|
} |
436
|
|
|
$this->assertEquals($expected, $actual); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
public function testCreateSingletonEmptyName() |
440
|
|
|
{ |
441
|
|
|
$returnType = m::mock(TEntityTypeType::class); |
442
|
|
|
$this->assertTrue($returnType instanceof TEntityTypeType, get_class($returnType)); |
443
|
|
|
$foo = new MetadataManager(); |
444
|
|
|
|
445
|
|
|
$expected = "Name must be a non-empty string"; |
446
|
|
|
$actual = null; |
447
|
|
|
|
448
|
|
|
try { |
449
|
|
|
$foo->createSingleton(null, $returnType); |
450
|
|
|
} catch (\InvalidArgumentException $e) { |
451
|
|
|
$actual = $e->getMessage(); |
452
|
|
|
} |
453
|
|
|
$this->assertEquals($expected, $actual); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
public function testCreateSingletonSuccessful() |
457
|
|
|
{ |
458
|
|
|
$msg = null; |
459
|
|
|
$name = "singleton"; |
460
|
|
|
$returnType = m::mock(TEntityTypeType::class)->makePartial(); |
461
|
|
|
$returnType->shouldReceive('getName')->andReturn('doubleton'); |
462
|
|
|
|
463
|
|
|
$entityContainer = m::mock(EntityContainer::class)->makePartial(); |
464
|
|
|
$entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once(); |
465
|
|
|
|
466
|
|
|
$schema = m::mock(Schema::class)->makePartial(); |
467
|
|
|
$schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once(); |
468
|
|
|
$edmx = m::mock(Edmx::class)->makePartial(); |
469
|
|
|
$edmx->shouldReceive('getDataServiceType->getSchema')->andReturn([$schema])->once(); |
470
|
|
|
|
471
|
|
|
$foo = m::mock(MetadataManager::class)->makePartial(); |
472
|
|
|
$foo->shouldReceive('getEdmx')->andReturn($edmx); |
473
|
|
|
|
474
|
|
|
$result = $foo->createSingleton($name, $returnType); |
475
|
|
|
$this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result)); |
476
|
|
|
$this->assertTrue($result->isOK($msg)); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
View Code Duplication |
public function testMalformedMultiplicity() |
480
|
|
|
{ |
481
|
|
|
list(, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
482
|
|
|
|
483
|
|
|
$expected = "Malformed multiplicity - valid values are *, 0..1 and 1"; |
484
|
|
|
$actual = null; |
485
|
|
|
|
486
|
|
|
try { |
487
|
|
|
$metadataManager->addNavigationPropertyToEntityType( |
488
|
|
|
$CategoryType, |
489
|
|
|
"1", |
490
|
|
|
"Customers", |
491
|
|
|
$CustomerType, |
492
|
|
|
"ABC", |
493
|
|
|
"Categories" |
494
|
|
|
); |
495
|
|
|
} catch (\InvalidArgumentException $e) { |
496
|
|
|
$actual = $e->getMessage(); |
497
|
|
|
} |
498
|
|
|
$this->assertEquals($expected, $actual); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
View Code Duplication |
public function testInvalidMultiplicityBelongsOnBothEnds() |
502
|
|
|
{ |
503
|
|
|
list(, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
504
|
|
|
|
505
|
|
|
$expected = "Invalid multiplicity combination - 1 1"; |
506
|
|
|
$actual = null; |
507
|
|
|
|
508
|
|
|
try { |
509
|
|
|
$metadataManager->addNavigationPropertyToEntityType( |
510
|
|
|
$CategoryType, |
511
|
|
|
"1", |
512
|
|
|
"Customers", |
513
|
|
|
$CustomerType, |
514
|
|
|
"1", |
515
|
|
|
"Categories" |
516
|
|
|
); |
517
|
|
|
} catch (\InvalidArgumentException $e) { |
518
|
|
|
$actual = $e->getMessage(); |
519
|
|
|
} |
520
|
|
|
$this->assertEquals($expected, $actual); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
View Code Duplication |
public function testInvalidMultiplicityManyToHasMany() |
524
|
|
|
{ |
525
|
|
|
list(, $metadataManager, $CategoryType, $CustomerType) = $this->setUpMetadataForNavTests(); |
526
|
|
|
|
527
|
|
|
$expected = "Invalid multiplicity combination - * 0..1"; |
528
|
|
|
$actual = null; |
529
|
|
|
|
530
|
|
|
try { |
531
|
|
|
$metadataManager->addNavigationPropertyToEntityType( |
532
|
|
|
$CategoryType, |
533
|
|
|
"*", |
534
|
|
|
"Customers", |
535
|
|
|
$CustomerType, |
536
|
|
|
"0..1", |
537
|
|
|
"Categories" |
538
|
|
|
); |
539
|
|
|
} catch (\InvalidArgumentException $e) { |
540
|
|
|
$actual = $e->getMessage(); |
541
|
|
|
} |
542
|
|
|
$this->assertEquals($expected, $actual); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* @return array |
547
|
|
|
*/ |
548
|
|
|
private function setUpMetadataForNavTests() |
549
|
|
|
{ |
550
|
|
|
$msg = null; |
551
|
|
|
$metadataManager = new MetadataManager("Data", "Container"); |
552
|
|
|
$result = null; |
553
|
|
|
|
554
|
|
|
list($CategoryType, ) = $metadataManager->addEntityType("Category"); |
555
|
|
|
list($CustomerType, ) = $metadataManager->addEntityType("Customer"); |
556
|
|
|
$this->assertTrue($CategoryType->isOK($msg), $msg); |
557
|
|
|
$this->assertTrue($CustomerType->isOK($msg), $msg); |
558
|
|
|
return array($msg, $metadataManager, $CategoryType, $CustomerType); |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.