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\TEntityTypeType; |
10
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReturnTypeType; |
11
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionType; |
12
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx; |
13
|
|
|
use Mockery as m; |
14
|
|
|
|
15
|
|
|
class MetadataManagerTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
public function testIsOKAtDefault() |
18
|
|
|
{ |
19
|
|
|
$ds = DIRECTORY_SEPARATOR; |
|
|
|
|
20
|
|
|
$metadataManager = new MetadataManager(); |
21
|
|
|
$msg = null; |
22
|
|
|
$edmx = $metadataManager->getEdmx(); |
23
|
|
|
$this->assertTrue($edmx->isOK($msg), $msg); |
24
|
|
|
$this->assertNull($msg); |
25
|
|
|
|
26
|
|
|
$d = $metadataManager->getEdmxXML(); |
27
|
|
|
$this->v3MetadataAgainstXSD($d); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function v3MetadataAgainstXSD($data) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$ds = DIRECTORY_SEPARATOR; |
33
|
|
|
|
34
|
|
|
$goodxsd = dirname(__DIR__) . $ds . "xsd" . $ds . "Microsoft.Data.Entity.Design.Edmx_3.Fixed.xsd"; |
35
|
|
|
if (!file_exists($goodxsd)) { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
$xml = new \DOMDocument(); |
39
|
|
|
$xml->loadXML($data); |
40
|
|
|
return $xml->schemaValidate($goodxsd); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testEntitysAndProperties() |
44
|
|
|
{ |
45
|
|
|
$metadataManager = new MetadataManager(); |
46
|
|
|
|
47
|
|
|
$eType = $metadataManager->addEntityType("Category"); |
48
|
|
|
$this->assertNotFalse($eType, "Etype is false not type " . $metadataManager->getLastError()); |
49
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CategoryID", "Int32", null, false, true, "Identity"); |
50
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CategoryName", "String"); |
51
|
|
|
$metadataManager->addPropertyToEntityType($eType, "Description", "String"); |
52
|
|
|
$metadataManager->addPropertyToEntityType($eType, "Picture", "Binary"); |
53
|
|
|
|
54
|
|
|
$eType = $metadataManager->addEntityType("CustomerDemographic"); |
55
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CustomerTypeID", "String", null, false, true); |
56
|
|
|
$metadataManager->addPropertyToEntityType($eType, "CustomerDesc", "String"); |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
$msg = null; |
60
|
|
|
$edmx = $metadataManager->getEdmx(); |
61
|
|
|
$this->assertTrue($edmx->isOK($msg), $msg); |
62
|
|
|
$this->assertNull($msg); |
63
|
|
|
|
64
|
|
|
$d = $metadataManager->getEdmxXML(); |
65
|
|
|
$this->v3MetadataAgainstXSD($d); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testEntitysAndPropertiesAndNavigationProperties() |
69
|
|
|
{ |
70
|
|
|
$msg = null; |
71
|
|
|
$metadataManager = new MetadataManager(); |
72
|
|
|
|
73
|
|
|
$CategoryType = $metadataManager->addEntityType("Category"); |
74
|
|
|
$this->assertNotFalse($CategoryType, "Etype is false not type " . $metadataManager->getLastError()); |
75
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "CategoryID", "Int32", null, false, true, "Identity"); |
76
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "CategoryName", "String"); |
77
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "Description", "String"); |
78
|
|
|
$metadataManager->addPropertyToEntityType($CategoryType, "Picture", "Binary"); |
79
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
80
|
|
|
|
81
|
|
|
$CustomerDemographicType = $metadataManager->addEntityType("CustomerDemographic"); |
82
|
|
|
$metadataManager->addPropertyToEntityType($CustomerDemographicType, "CustomerTypeID", "String", null, false, true); |
83
|
|
|
$metadataManager->addPropertyToEntityType($CustomerDemographicType, "CustomerDesc", "String"); |
84
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
85
|
|
|
|
86
|
|
|
$CustomerType = $metadataManager->addEntityType("Customer"); |
87
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "CustomerID", "String", null, false, true); |
88
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "CompanyName", "String"); |
89
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "ContactName", "String"); |
90
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "ContactTitle", "String"); |
91
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Address", "String"); |
92
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "City", "String"); |
93
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Region", "String"); |
94
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "PostalCode", "String"); |
95
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Country", "String"); |
96
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Phone", "String"); |
97
|
|
|
$metadataManager->addPropertyToEntityType($CustomerType, "Fax", "String"); |
98
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
99
|
|
|
|
100
|
|
|
$EmployeeType = $metadataManager->addEntityType("Employee"); |
101
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "EmployeeID", "Int32", null, false, true, "Identity"); |
102
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "LastName", "String"); |
103
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "FirstName", "String"); |
104
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Title", "String"); |
105
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "TitleOfCourtesy", "String"); |
106
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "BirthDate", "DateTime"); |
107
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "HireDate", "DateTime"); |
108
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Address", "String"); |
109
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "City", "String"); |
110
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Region", "String"); |
111
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "PostalCode", "String"); |
112
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Country", "String"); |
113
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "HomePhone", "String"); |
114
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Extension", "String"); |
115
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Photo", "Binary"); |
116
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "Notes", "String"); |
117
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "ReportsTo", "Int32"); |
118
|
|
|
$metadataManager->addPropertyToEntityType($EmployeeType, "PhotoPath", "String"); |
119
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
120
|
|
|
|
121
|
|
|
$Order_DetailType = $metadataManager->addEntityType("Order_Detail"); |
122
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "OrderID", "Int32", null, false, true); |
123
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "ProductID", "Int32", null, false, true); |
124
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "UnitPrice", "Decimal"); |
125
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "Quantity", "Int16"); |
126
|
|
|
$metadataManager->addPropertyToEntityType($Order_DetailType, "Discount", "Single"); |
127
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
128
|
|
|
|
129
|
|
|
$OrderType = $metadataManager->addEntityType("Order"); |
130
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "OrderID", "Int32", null, false, true, "Identity"); |
131
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "CustomerID", "String"); |
132
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "EmployeeID", "Int32"); |
133
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "OrderDate", "DateTime"); |
134
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "RequiredDate", "DateTime"); |
135
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShippedDate", "DateTime"); |
136
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipVia", "DateTime"); |
137
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "Freight", "Decimal"); |
138
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipName", "String"); |
139
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipAddress", "String"); |
140
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipCity", "String"); |
141
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipRegion", "String"); |
142
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipPostalCode", "String"); |
143
|
|
|
$metadataManager->addPropertyToEntityType($OrderType, "ShipCountry", "String"); |
144
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
145
|
|
|
|
146
|
|
|
$ProductType = $metadataManager->addEntityType("Product"); |
147
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "ProductID", "Int32", null, false, true, "Identity"); |
148
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "ProductName", "String"); |
149
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "SupplierID", "Int32"); |
150
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "CategoryID", "Int32"); |
151
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "QuantityPerUnit", "String"); |
152
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "UnitPrice", "Decimal"); |
153
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "UnitsInStock", "Int16"); |
154
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "UnitsOnOrder", "Int16"); |
155
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "ReorderLevel", "Int16"); |
156
|
|
|
$metadataManager->addPropertyToEntityType($ProductType, "Discontinued", "Boolean"); |
157
|
|
|
$this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg); |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
$metadataManager->addNavigationPropertyToEntityType( |
161
|
|
|
$CategoryType, "*", "Products", $ProductType, "0..1", "Category", ["CategoryID"], ["CategoryID"] |
162
|
|
|
); |
163
|
|
|
$metadataManager->addNavigationPropertyToEntityType( |
164
|
|
|
$Order_DetailType, "1", "Order", $ProductType, "*", "Order_Details", ["OrderID"], ["CategoryID"] |
165
|
|
|
); |
166
|
|
|
// <NavigationProperty Name="Order_Details" Relationship="NorthwindModel.FK_Order_Details_Products" ToRole="Order_Details" FromRole="Products"/> |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
$msg = null; |
170
|
|
|
$edmx = $metadataManager->getEdmx(); |
171
|
|
|
$this->assertTrue($edmx->isOK($msg), $msg); |
172
|
|
|
$this->assertNull($msg); |
173
|
|
|
|
174
|
|
|
$d = $metadataManager->getEdmxXML(); |
175
|
|
|
$this->v3MetadataAgainstXSD($d); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testMetadataSerialiseRoundTrip() |
179
|
|
|
{ |
180
|
|
|
$bar = new MetadataManager(); |
181
|
|
|
$foo = new MetadataManager(); |
182
|
|
|
|
183
|
|
|
$cereal = serialize($foo); |
184
|
|
|
|
185
|
|
|
$foo = unserialize($cereal); |
186
|
|
|
$this->assertTrue(null != $foo->getSerialiser()); |
187
|
|
|
$this->assertEquals($bar, $foo); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testCreateSingletonBadReturnType() |
191
|
|
|
{ |
192
|
|
|
$returnType = m::mock(IsOK::class); |
193
|
|
|
$foo = new MetadataManager(); |
194
|
|
|
|
195
|
|
|
$expected = "Expected return type must be either TEntityType or TComplexType"; |
196
|
|
|
$actual = null; |
197
|
|
|
|
198
|
|
|
try { |
199
|
|
|
$foo->createSingleton(null, $returnType); |
200
|
|
|
} catch (\InvalidArgumentException $e) { |
201
|
|
|
$actual = $e->getMessage(); |
202
|
|
|
} |
203
|
|
|
$this->assertEquals($expected, $actual); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testCreateSingletonEmptyName() |
207
|
|
|
{ |
208
|
|
|
$returnType = m::mock(TEntityTypeType::class); |
209
|
|
|
$this->assertTrue($returnType instanceof TEntityTypeType, get_class($returnType)); |
210
|
|
|
$foo = new MetadataManager(); |
211
|
|
|
|
212
|
|
|
$expected = "Name must be a non-empty string"; |
213
|
|
|
$actual = null; |
214
|
|
|
|
215
|
|
|
try { |
216
|
|
|
$foo->createSingleton(null, $returnType); |
217
|
|
|
} catch (\InvalidArgumentException $e) { |
218
|
|
|
$actual = $e->getMessage(); |
219
|
|
|
} |
220
|
|
|
$this->assertEquals($expected, $actual); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testCreateSingletonSuccessful() |
224
|
|
|
{ |
225
|
|
|
$msg = null; |
226
|
|
|
$name = "singleton"; |
227
|
|
|
$returnType = m::mock(TEntityTypeType::class)->makePartial(); |
228
|
|
|
$returnType->shouldReceive('getName')->andReturn('doubleton'); |
229
|
|
|
|
230
|
|
|
$entityContainer = m::mock(EntityContainer::class)->makePartial(); |
231
|
|
|
$entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once(); |
232
|
|
|
|
233
|
|
|
$schema = m::mock(Schema::class)->makePartial(); |
234
|
|
|
$schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once(); |
235
|
|
|
|
236
|
|
|
$edmx = m::mock(Edmx::class)->makePartial(); |
237
|
|
|
$edmx->shouldReceive('getDataServiceType->getSchema')->andReturn([$schema])->once(); |
238
|
|
|
|
239
|
|
|
$foo = m::mock(MetadataManager::class)->makePartial(); |
240
|
|
|
$foo->shouldReceive('getEdmx')->andReturn($edmx); |
241
|
|
|
|
242
|
|
|
$result = $foo->createSingleton($name, $returnType); |
243
|
|
|
$this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result)); |
244
|
|
|
$this->assertTrue($result->isOK($msg)); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
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.