Test Failed
Pull Request — master (#104)
by Alex
04:42
created

testAddOneToOneReverseNavProperty()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 52
Code Lines 42

Duplication

Lines 52
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 52
loc 52
rs 8.6868
cc 6
eloc 42
nc 20
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
0 ignored issues
show
Unused Code introduced by
$ds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $msg = null;
184
        $metadataManager = new MetadataManager("Data", "Container");
185
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
186
187
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
188
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
189
        $this->assertTrue($CategoryType->isOK($msg), $msg);
190
        $this->assertTrue($CustomerType->isOK($msg), $msg);
191
192
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
193
            $CategoryType,
194
            "*",
195
            "Customers",
196
            $CustomerType,
197
            "*",
198
            "Categories"
199
        );
200
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
201
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
202
        $this->assertEquals("Customers", $principal->getName());
203
        $this->assertEquals("Categories", $dependent->getName());
204
205
        $navProps = [$principal, $dependent];
206
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
207
        $this->assertEquals(1, count($assoc));
208
        $assoc = $assoc[0];
209
        $this->assertTrue($assoc instanceof TAssociationType);
210
        $this->assertTrue($assoc->isOK($msg), $msg);
211
212
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
213
        $ends = $assoc->getEnd();
214
215
        $this->assertEquals(2, count($ends));
216
        foreach ($navProps as $prop) {
217
            $fromMatch = $ends[0]->getRole() == $prop->getToRole()
218
                         || $ends[1]->getRole() == $prop->getToRole();
219
            $this->assertTrue($fromMatch, "toRole must match at least one end role");
220
            if ($ends[0]->getRole() == $prop->getToRole()) {
221
                $this->assertEquals($ends[1]->getRole(), $prop->getFromRole());
222
                $this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole());
223
            } else {
224
                $this->assertEquals($ends[0]->getRole(), $prop->getFromRole());
225
                $this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole());
226
            }
227
        }
228
        $principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1];
229
        $this->assertEquals('*', $principalEnd->getMultiplicity());
230
        $dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1];
231
        $this->assertEquals('*', $dependentEnd->getMultiplicity());
232
    }
233
234 View Code Duplication
    public function testAddOneToManyNavProperty()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
    {
236
        $msg = null;
237
        $metadataManager = new MetadataManager("Data", "Container");
238
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
239
240
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
241
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
242
        $this->assertTrue($CategoryType->isOK($msg), $msg);
243
        $this->assertTrue($CustomerType->isOK($msg), $msg);
244
245
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
246
            $CategoryType,
247
            "*",
248
            "Customers",
249
            $CustomerType,
250
            "1",
251
            "Categories"
252
        );
253
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
254
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
255
        $this->assertEquals("Customers", $principal->getName());
256
        $this->assertEquals("Categories", $dependent->getName());
257
258
        $navProps = [$principal, $dependent];
259
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
260
        $this->assertEquals(1, count($assoc));
261
        $assoc = $assoc[0];
262
        $this->assertTrue($assoc instanceof TAssociationType);
263
        $this->assertTrue($assoc->isOK($msg), $msg);
264
265
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
266
        $ends = $assoc->getEnd();
267
268
        $this->assertEquals(2, count($ends));
269
        foreach ($navProps as $prop) {
270
            $fromMatch = $ends[0]->getRole() == $prop->getToRole()
271
                         || $ends[1]->getRole() == $prop->getToRole();
272
            $this->assertTrue($fromMatch, "toRole must match at least one end role");
273
            if ($ends[0]->getRole() == $prop->getToRole()) {
274
                $this->assertEquals($ends[1]->getRole(), $prop->getFromRole());
275
                $this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole());
276
            } else {
277
                $this->assertEquals($ends[0]->getRole(), $prop->getFromRole());
278
                $this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole());
279
            }
280
        }
281
        $principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1];
282
        $this->assertEquals('*', $principalEnd->getMultiplicity());
283
        $dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1];
284
        $this->assertEquals('1', $dependentEnd->getMultiplicity());
285
    }
286
287 View Code Duplication
    public function testAddManyToOneNavProperty()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
    {
289
        $msg = null;
290
        $metadataManager = new MetadataManager("Data", "Container");
291
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
292
293
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
294
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
295
        $this->assertTrue($CategoryType->isOK($msg), $msg);
296
        $this->assertTrue($CustomerType->isOK($msg), $msg);
297
298
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
299
            $CategoryType,
300
            "1",
301
            "Customers",
302
            $CustomerType,
303
            "*",
304
            "Categories"
305
        );
306
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
307
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
308
        $this->assertEquals("Customers", $principal->getName());
309
        $this->assertEquals("Categories", $dependent->getName());
310
311
        $navProps = [$principal, $dependent];
312
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
313
        $this->assertEquals(1, count($assoc));
314
        $assoc = $assoc[0];
315
        $this->assertTrue($assoc instanceof TAssociationType);
316
        $this->assertTrue($assoc->isOK($msg), $msg);
317
318
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
319
        $ends = $assoc->getEnd();
320
321
        $this->assertEquals(2, count($ends));
322
        foreach ($navProps as $prop) {
323
            $fromMatch = $ends[0]->getRole() == $prop->getToRole()
324
                         || $ends[1]->getRole() == $prop->getToRole();
325
            $this->assertTrue($fromMatch, "toRole must match at least one end role");
326
            if ($ends[0]->getRole() == $prop->getToRole()) {
327
                $this->assertEquals($ends[1]->getRole(), $prop->getFromRole());
328
                $this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole());
329
            } else {
330
                $this->assertEquals($ends[0]->getRole(), $prop->getFromRole());
331
                $this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole());
332
            }
333
        }
334
        $principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1];
335
        $this->assertEquals('1', $principalEnd->getMultiplicity());
336
        $dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1];
337
        $this->assertEquals('*', $dependentEnd->getMultiplicity());
338
    }
339
340 View Code Duplication
    public function testAddOneToOneForwardNavProperty()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
    {
342
        $msg = null;
343
        $metadataManager = new MetadataManager("Data", "Container");
344
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
345
346
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
347
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
348
        $this->assertTrue($CategoryType->isOK($msg), $msg);
349
        $this->assertTrue($CustomerType->isOK($msg), $msg);
350
351
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
352
            $CategoryType,
353
            "0..1",
354
            "Customers",
355
            $CustomerType,
356
            "1",
357
            "Categories"
358
        );
359
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
360
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
361
        $this->assertEquals("Customers", $principal->getName());
362
        $this->assertEquals("Categories", $dependent->getName());
363
364
        $navProps = [$principal, $dependent];
365
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
366
        $this->assertEquals(1, count($assoc));
367
        $assoc = $assoc[0];
368
        $this->assertTrue($assoc instanceof TAssociationType);
369
        $this->assertTrue($assoc->isOK($msg), $msg);
370
371
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
372
        $ends = $assoc->getEnd();
373
374
        $this->assertEquals(2, count($ends));
375
        foreach ($navProps as $prop) {
376
            $fromMatch = $ends[0]->getRole() == $prop->getToRole()
377
                         || $ends[1]->getRole() == $prop->getToRole();
378
            $this->assertTrue($fromMatch, "toRole must match at least one end role");
379
            if ($ends[0]->getRole() == $prop->getToRole()) {
380
                $this->assertEquals($ends[1]->getRole(), $prop->getFromRole());
381
                $this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole());
382
            } else {
383
                $this->assertEquals($ends[0]->getRole(), $prop->getFromRole());
384
                $this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole());
385
            }
386
        }
387
        $principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1];
388
        $this->assertEquals('0..1', $principalEnd->getMultiplicity());
389
        $dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1];
390
        $this->assertEquals('1', $dependentEnd->getMultiplicity());
391
    }
392
393 View Code Duplication
    public function testAddOneToOneReverseNavProperty()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
394
    {
395
        $msg = null;
396
        $metadataManager = new MetadataManager("Data", "Container");
397
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
398
399
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
400
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
401
        $this->assertTrue($CategoryType->isOK($msg), $msg);
402
        $this->assertTrue($CustomerType->isOK($msg), $msg);
403
404
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
405
            $CategoryType,
406
            "1",
407
            "Customers",
408
            $CustomerType,
409
            "0..1",
410
            "Categories"
411
        );
412
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
413
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
414
        $this->assertEquals("Customers", $principal->getName());
415
        $this->assertEquals("Categories", $dependent->getName());
416
417
        $navProps = [$principal, $dependent];
418
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
419
        $this->assertEquals(1, count($assoc));
420
        $assoc = $assoc[0];
421
        $this->assertTrue($assoc instanceof TAssociationType);
422
        $this->assertTrue($assoc->isOK($msg), $msg);
423
424
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
425
        $ends = $assoc->getEnd();
426
427
        $this->assertEquals(2, count($ends));
428
        foreach ($navProps as $prop) {
429
            $fromMatch = $ends[0]->getRole() == $prop->getToRole()
430
                         || $ends[1]->getRole() == $prop->getToRole();
431
            $this->assertTrue($fromMatch, "toRole must match at least one end role");
432
            if ($ends[0]->getRole() == $prop->getToRole()) {
433
                $this->assertEquals($ends[1]->getRole(), $prop->getFromRole());
434
                $this->assertNotEquals($ends[0]->getRole(), $prop->getFromRole());
435
            } else {
436
                $this->assertEquals($ends[0]->getRole(), $prop->getFromRole());
437
                $this->assertNotEquals($ends[1]->getRole(), $prop->getFromRole());
438
            }
439
        }
440
        $principalEnd = ($ends[0]->getRole() == $principal->getToRole()) ? $ends[0] : $ends[1];
441
        $this->assertEquals('1', $principalEnd->getMultiplicity());
442
        $dependentEnd = ($ends[0]->getRole() == $dependent->getToRole()) ? $ends[0] : $ends[1];
443
        $this->assertEquals('0..1', $dependentEnd->getMultiplicity());
444
    }
445
446
    public function testMetadataSerialiseRoundTrip()
447
    {
448
        $bar = new MetadataManager();
449
        $foo = new MetadataManager();
450
451
        $cereal = serialize($foo);
452
453
        $foo = unserialize($cereal);
454
        $this->assertTrue(null != $foo->getSerialiser());
455
        $this->assertEquals($bar, $foo);
456
    }
457
458
    public function testCreateSingletonBadReturnType()
459
    {
460
        $returnType = m::mock(IsOK::class);
461
        $foo = new MetadataManager();
462
463
        $expected = "Expected return type must be either TEntityType or TComplexType";
464
        $actual = null;
465
466
        try {
467
            $foo->createSingleton(null, $returnType);
468
        } catch (\InvalidArgumentException $e) {
469
            $actual = $e->getMessage();
470
        }
471
        $this->assertEquals($expected, $actual);
472
    }
473
474
    public function testCreateSingletonEmptyName()
475
    {
476
        $returnType = m::mock(TEntityTypeType::class);
477
        $this->assertTrue($returnType instanceof TEntityTypeType, get_class($returnType));
478
        $foo = new MetadataManager();
479
480
        $expected = "Name must be a non-empty string";
481
        $actual = null;
482
483
        try {
484
            $foo->createSingleton(null, $returnType);
485
        } catch (\InvalidArgumentException $e) {
486
            $actual = $e->getMessage();
487
        }
488
        $this->assertEquals($expected, $actual);
489
    }
490
491
    public function testCreateSingletonSuccessful()
492
    {
493
        $msg = null;
494
        $name = "singleton";
495
        $returnType = m::mock(TEntityTypeType::class)->makePartial();
496
        $returnType->shouldReceive('getName')->andReturn('doubleton');
497
498
        $entityContainer = m::mock(EntityContainer::class)->makePartial();
499
        $entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once();
500
501
        $schema = m::mock(Schema::class)->makePartial();
502
        $schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once();
503
        $edmx = m::mock(Edmx::class)->makePartial();
504
        $edmx->shouldReceive('getDataServiceType->getSchema')->andReturn([$schema])->once();
505
506
        $foo = m::mock(MetadataManager::class)->makePartial();
507
        $foo->shouldReceive('getEdmx')->andReturn($edmx);
508
509
        $result = $foo->createSingleton($name, $returnType);
510
        $this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result));
511
        $this->assertTrue($result->isOK($msg));
512
    }
513
514 View Code Duplication
    public function testMalformedMultiplicity()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
515
    {
516
        $msg = null;
517
        $metadataManager = new MetadataManager("Data", "Container");
518
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
519
520
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
521
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
522
        $this->assertTrue($CategoryType->isOK($msg), $msg);
523
        $this->assertTrue($CustomerType->isOK($msg), $msg);
524
525
        $expected = "Malformed multiplicity - valid values are *, 0..1 and 1";
526
        $actual = null;
527
528
        try {
529
            list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
2 ignored issues
show
Unused Code introduced by
The assignment to $principal is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $dependent is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
530
                $CategoryType,
531
                "1",
532
                "Customers",
533
                $CustomerType,
534
                "ABC",
535
                "Categories"
536
            );
537
        } catch (\InvalidArgumentException $e) {
538
            $actual = $e->getMessage();
539
        }
540
        $this->assertEquals($expected, $actual);
541
    }
542
543 View Code Duplication
    public function testInvalidMultiplicityBelongsOnBothEnds()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
544
    {
545
        $msg = null;
546
        $metadataManager = new MetadataManager("Data", "Container");
547
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
548
549
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
550
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
551
        $this->assertTrue($CategoryType->isOK($msg), $msg);
552
        $this->assertTrue($CustomerType->isOK($msg), $msg);
553
554
        $expected =  "Invalid multiplicity combination - 1 1";
555
        $actual = null;
556
557
        try {
558
            list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
2 ignored issues
show
Unused Code introduced by
The assignment to $principal is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $dependent is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
559
                $CategoryType,
560
                "1",
561
                "Customers",
562
                $CustomerType,
563
                "1",
564
                "Categories"
565
            );
566
        } catch (\InvalidArgumentException $e) {
567
            $actual = $e->getMessage();
568
        }
569
        $this->assertEquals($expected, $actual);
570
    }
571
572 View Code Duplication
    public function testInvalidMultiplicityManyToHasMany()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
573
    {
574
        $msg = null;
575
        $metadataManager = new MetadataManager("Data", "Container");
576
        $result = null;
1 ignored issue
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
577
578
        list($CategoryType, $result) = $metadataManager->addEntityType("Category");
579
        list($CustomerType, $result) = $metadataManager->addEntityType("Customer");
580
        $this->assertTrue($CategoryType->isOK($msg), $msg);
581
        $this->assertTrue($CustomerType->isOK($msg), $msg);
582
583
        $expected =  "Invalid multiplicity combination - * 0..1";
584
        $actual = null;
585
586
        try {
587
            list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
2 ignored issues
show
Unused Code introduced by
The assignment to $principal is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $dependent is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
588
                $CategoryType,
589
                "*",
590
                "Customers",
591
                $CustomerType,
592
                "0..1",
593
                "Categories"
594
            );
595
        } catch (\InvalidArgumentException $e) {
596
            $actual = $e->getMessage();
597
        }
598
        $this->assertEquals($expected, $actual);
599
    }
600
}
601