Completed
Pull Request — master (#100)
by Alex
08:56
created

MetadataManagerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 175
Duplicated Lines 6.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 6
c 9
b 0
f 0
lcom 1
cbo 3
dl 12
loc 175
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testIsOKAtDefault() 0 12 1
A v3MetadataAgainstXSD() 12 12 2
B testEntitysAndProperties() 0 24 1
B testEntitysAndPropertiesAndNavigationProperties() 0 109 1
A testMetadataSerialiseRoundTrip() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Doc
5
 * Date: 5/11/2017
6
 * Time: 11:01 PM
7
 */
8
9
namespace AlgoWeb\ODataMetadata\Tests;
10
11
use AlgoWeb\ODataMetadata\MetadataManager;
12
13
class MetadataManagerTest extends \PHPUnit_Framework_TestCase
14
{
15
    public function testIsOKAtDefault()
16
    {
17
        $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...
18
        $metadataManager = new MetadataManager();
19
        $msg = null;
20
        $edmx = $metadataManager->getEdmx();
21
        $this->assertTrue($edmx->isOK($msg), $msg);
22
        $this->assertNull($msg);
23
24
        $d = $metadataManager->getEdmxXML();
25
        $this->v3MetadataAgainstXSD($d);
26
    }
27
28 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...
29
    {
30
        $ds = DIRECTORY_SEPARATOR;
31
32
        $goodxsd = dirname(__DIR__) . $ds . "xsd" . $ds . "Microsoft.Data.Entity.Design.Edmx_3.Fixed.xsd";
33
        if (!file_exists($goodxsd)) {
34
            return true;
35
        }
36
        $xml = new \DOMDocument();
37
        $xml->loadXML($data);
38
        return $xml->schemaValidate($goodxsd);
39
    }
40
41
    public function testEntitysAndProperties()
42
    {
43
        $metadataManager = new MetadataManager();
44
45
        $eType = $metadataManager->addEntityType("Category");
46
        $this->assertNotFalse($eType, "Etype is false not type " . $metadataManager->getLastError());
47
        $metadataManager->addPropertyToEntityType($eType, "CategoryID", "Int32", null, false, true, "Identity");
48
        $metadataManager->addPropertyToEntityType($eType, "CategoryName", "String");
49
        $metadataManager->addPropertyToEntityType($eType, "Description", "String");
50
        $metadataManager->addPropertyToEntityType($eType, "Picture", "Binary");
51
52
        $eType = $metadataManager->addEntityType("CustomerDemographic");
53
        $metadataManager->addPropertyToEntityType($eType, "CustomerTypeID", "String", null, false, true);
54
        $metadataManager->addPropertyToEntityType($eType, "CustomerDesc", "String");
55
56
57
        $msg = null;
58
        $edmx = $metadataManager->getEdmx();
59
        $this->assertTrue($edmx->isOK($msg), $msg);
60
        $this->assertNull($msg);
61
62
        $d = $metadataManager->getEdmxXML();
63
        $this->v3MetadataAgainstXSD($d);
64
    }
65
66
    public function testEntitysAndPropertiesAndNavigationProperties()
67
    {
68
        $msg = null;
69
        $metadataManager = new MetadataManager();
70
71
        $CategoryType = $metadataManager->addEntityType("Category");
72
        $this->assertNotFalse($CategoryType, "Etype is false not type " . $metadataManager->getLastError());
73
        $metadataManager->addPropertyToEntityType($CategoryType, "CategoryID", "Int32", null, false, true, "Identity");
74
        $metadataManager->addPropertyToEntityType($CategoryType, "CategoryName", "String");
75
        $metadataManager->addPropertyToEntityType($CategoryType, "Description", "String");
76
        $metadataManager->addPropertyToEntityType($CategoryType, "Picture", "Binary");
77
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
78
79
        $CustomerDemographicType = $metadataManager->addEntityType("CustomerDemographic");
80
        $metadataManager->addPropertyToEntityType($CustomerDemographicType, "CustomerTypeID", "String", null, false, true);
81
        $metadataManager->addPropertyToEntityType($CustomerDemographicType, "CustomerDesc", "String");
82
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
83
84
        $CustomerType = $metadataManager->addEntityType("Customer");
85
        $metadataManager->addPropertyToEntityType($CustomerType, "CustomerID", "String", null, false, true);
86
        $metadataManager->addPropertyToEntityType($CustomerType, "CompanyName", "String");
87
        $metadataManager->addPropertyToEntityType($CustomerType, "ContactName", "String");
88
        $metadataManager->addPropertyToEntityType($CustomerType, "ContactTitle", "String");
89
        $metadataManager->addPropertyToEntityType($CustomerType, "Address", "String");
90
        $metadataManager->addPropertyToEntityType($CustomerType, "City", "String");
91
        $metadataManager->addPropertyToEntityType($CustomerType, "Region", "String");
92
        $metadataManager->addPropertyToEntityType($CustomerType, "PostalCode", "String");
93
        $metadataManager->addPropertyToEntityType($CustomerType, "Country", "String");
94
        $metadataManager->addPropertyToEntityType($CustomerType, "Phone", "String");
95
        $metadataManager->addPropertyToEntityType($CustomerType, "Fax", "String");
96
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
97
98
        $EmployeeType = $metadataManager->addEntityType("Employee");
99
        $metadataManager->addPropertyToEntityType($EmployeeType, "EmployeeID", "Int32", null, false, true, "Identity");
100
        $metadataManager->addPropertyToEntityType($EmployeeType, "LastName", "String");
101
        $metadataManager->addPropertyToEntityType($EmployeeType, "FirstName", "String");
102
        $metadataManager->addPropertyToEntityType($EmployeeType, "Title", "String");
103
        $metadataManager->addPropertyToEntityType($EmployeeType, "TitleOfCourtesy", "String");
104
        $metadataManager->addPropertyToEntityType($EmployeeType, "BirthDate", "DateTime");
105
        $metadataManager->addPropertyToEntityType($EmployeeType, "HireDate", "DateTime");
106
        $metadataManager->addPropertyToEntityType($EmployeeType, "Address", "String");
107
        $metadataManager->addPropertyToEntityType($EmployeeType, "City", "String");
108
        $metadataManager->addPropertyToEntityType($EmployeeType, "Region", "String");
109
        $metadataManager->addPropertyToEntityType($EmployeeType, "PostalCode", "String");
110
        $metadataManager->addPropertyToEntityType($EmployeeType, "Country", "String");
111
        $metadataManager->addPropertyToEntityType($EmployeeType, "HomePhone", "String");
112
        $metadataManager->addPropertyToEntityType($EmployeeType, "Extension", "String");
113
        $metadataManager->addPropertyToEntityType($EmployeeType, "Photo", "Binary");
114
        $metadataManager->addPropertyToEntityType($EmployeeType, "Notes", "String");
115
        $metadataManager->addPropertyToEntityType($EmployeeType, "ReportsTo", "Int32");
116
        $metadataManager->addPropertyToEntityType($EmployeeType, "PhotoPath", "String");
117
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
118
119
        $Order_DetailType = $metadataManager->addEntityType("Order_Detail");
120
        $metadataManager->addPropertyToEntityType($Order_DetailType, "OrderID", "Int32", null, false, true);
121
        $metadataManager->addPropertyToEntityType($Order_DetailType, "ProductID", "Int32", null, false, true);
122
        $metadataManager->addPropertyToEntityType($Order_DetailType, "UnitPrice", "Decimal");
123
        $metadataManager->addPropertyToEntityType($Order_DetailType, "Quantity", "Int16");
124
        $metadataManager->addPropertyToEntityType($Order_DetailType, "Discount", "Single");
125
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
126
127
        $OrderType = $metadataManager->addEntityType("Order");
128
        $metadataManager->addPropertyToEntityType($OrderType, "OrderID", "Int32", null, false, true, "Identity");
129
        $metadataManager->addPropertyToEntityType($OrderType, "CustomerID", "String");
130
        $metadataManager->addPropertyToEntityType($OrderType, "EmployeeID", "Int32");
131
        $metadataManager->addPropertyToEntityType($OrderType, "OrderDate", "DateTime");
132
        $metadataManager->addPropertyToEntityType($OrderType, "RequiredDate", "DateTime");
133
        $metadataManager->addPropertyToEntityType($OrderType, "ShippedDate", "DateTime");
134
        $metadataManager->addPropertyToEntityType($OrderType, "ShipVia", "DateTime");
135
        $metadataManager->addPropertyToEntityType($OrderType, "Freight", "Decimal");
136
        $metadataManager->addPropertyToEntityType($OrderType, "ShipName", "String");
137
        $metadataManager->addPropertyToEntityType($OrderType, "ShipAddress", "String");
138
        $metadataManager->addPropertyToEntityType($OrderType, "ShipCity", "String");
139
        $metadataManager->addPropertyToEntityType($OrderType, "ShipRegion", "String");
140
        $metadataManager->addPropertyToEntityType($OrderType, "ShipPostalCode", "String");
141
        $metadataManager->addPropertyToEntityType($OrderType, "ShipCountry", "String");
142
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
143
144
        $ProductType = $metadataManager->addEntityType("Product");
145
        $metadataManager->addPropertyToEntityType($ProductType, "ProductID", "Int32", null, false, true, "Identity");
146
        $metadataManager->addPropertyToEntityType($ProductType, "ProductName", "String");
147
        $metadataManager->addPropertyToEntityType($ProductType, "SupplierID", "Int32");
148
        $metadataManager->addPropertyToEntityType($ProductType, "CategoryID", "Int32");
149
        $metadataManager->addPropertyToEntityType($ProductType, "QuantityPerUnit", "String");
150
        $metadataManager->addPropertyToEntityType($ProductType, "UnitPrice", "Decimal");
151
        $metadataManager->addPropertyToEntityType($ProductType, "UnitsInStock", "Int16");
152
        $metadataManager->addPropertyToEntityType($ProductType, "UnitsOnOrder", "Int16");
153
        $metadataManager->addPropertyToEntityType($ProductType, "ReorderLevel", "Int16");
154
        $metadataManager->addPropertyToEntityType($ProductType, "Discontinued", "Boolean");
155
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
156
157
158
        $metadataManager->addNavigationPropertyToEntityType(
159
            $CategoryType, "*", "Products", $ProductType, "0..1", "Category", ["CategoryID"], ["CategoryID"]
160
        );
161
        $metadataManager->addNavigationPropertyToEntityType(
162
            $Order_DetailType, "1", "Order", $ProductType, "*", "Order_Details", ["OrderID"], ["CategoryID"]
163
        );
164
//        <NavigationProperty Name="Order_Details" Relationship="NorthwindModel.FK_Order_Details_Products" ToRole="Order_Details" FromRole="Products"/>
165
166
167
        $msg = null;
168
        $edmx = $metadataManager->getEdmx();
169
        $this->assertTrue($edmx->isOK($msg), $msg);
170
        $this->assertNull($msg);
171
172
        $d = $metadataManager->getEdmxXML();
173
        $this->v3MetadataAgainstXSD($d);
174
    }
175
176
    public function testMetadataSerialiseRoundTrip()
177
    {
178
        $bar = new MetadataManager();
179
        $foo = new MetadataManager();
180
181
        $cereal = serialize($foo);
182
183
        $foo = unserialize($cereal);
184
        $this->assertTrue(null != $foo->getSerialiser());
185
        $this->assertEquals($bar, $foo);
186
    }
187
}
188