Completed
Push — master ( 819957...3609f8 )
by Alex
55s queued 20s
created

MetadataManagerTest::testAddManyToOneNavProperty()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 33
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 33
loc 33
rs 8.8571
cc 1
eloc 26
nc 1
nop 0
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\EntityContainer\EntitySetAnonymousType;
9
use AlgoWeb\ODataMetadata\MetadataV3\edm\Schema;
10
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationType;
11
use AlgoWeb\ODataMetadata\MetadataV3\edm\TComplexTypePropertyType;
12
use AlgoWeb\ODataMetadata\MetadataV3\edm\TComplexTypeType;
13
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType;
14
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType;
15
use AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReturnTypeType;
16
use AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionType;
17
use AlgoWeb\ODataMetadata\MetadataV3\edm\TNavigationPropertyType;
18
use AlgoWeb\ODataMetadata\MetadataV3\edm\TTextType;
19
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx;
20
use AlgoWeb\ODataMetadata\MetadataV3\edmx\TDataServicesType;
21
use JMS\Serializer\Serializer;
22
use Mockery as m;
23
24
class MetadataManagerTest extends \PHPUnit_Framework_TestCase
25
{
26
    public function testIsOKAtDefault()
27
    {
28
        $metadataManager = new MetadataManager();
29
        $msg = null;
30
        $edmx = $metadataManager->getEdmx();
31
        $this->assertTrue($edmx->isOK($msg), $msg);
32
        $this->assertNull($msg);
33
34
        $d = $metadataManager->getEdmxXML();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $d. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
35
        $this->v3MetadataAgainstXSD($d);
36
    }
37
38 View Code Duplication
    public function v3MetadataAgainstXSD($data)
0 ignored issues
show
Coding Style introduced by
function v3MetadataAgainstXSD() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
39
    {
40
        $ds = DIRECTORY_SEPARATOR;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ds. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
41
42
        $goodxsd = dirname(__DIR__) . $ds . 'xsd' . $ds . 'Microsoft.Data.Entity.Design.Edmx_3.Fixed.xsd';
43
        if (!file_exists($goodxsd)) {
44
            return true;
45
        }
46
        $xml = new \DOMDocument();
47
        $xml->loadXML($data);
48
        return $xml->schemaValidate($goodxsd);
49
    }
50
51
    public function testEntitysAndProperties()
52
    {
53
        $metadataManager = new MetadataManager();
54
        $result = null;
55
56
        list($eType, $result) = $metadataManager->addEntityType('Category');
57
        $this->assertNotFalse($eType, 'Etype is false not type ' . $metadataManager->getLastError());
58
        $metadataManager->addPropertyToEntityType($eType, 'CategoryID', 'Int32', null, false, true, 'Identity');
59
        $metadataManager->addPropertyToEntityType($eType, 'CategoryName', 'String');
60
        $metadataManager->addPropertyToEntityType($eType, 'Description', 'String');
61
        $metadataManager->addPropertyToEntityType($eType, 'Picture', 'Binary');
62
63
        list($eType, $result) = $metadataManager->addEntityType('CustomerDemographic');
64
        $metadataManager->addPropertyToEntityType($eType, 'CustomerTypeID', 'String', null, false, true);
65
        $metadataManager->addPropertyToEntityType($eType, 'CustomerDesc', 'String');
66
67
        $msg = null;
68
        $edmx = $metadataManager->getEdmx();
69
        $this->assertTrue($edmx->isOK($msg), $msg);
70
        $this->assertNull($msg);
71
72
        $d = $metadataManager->getEdmxXML();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $d. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
73
        $this->v3MetadataAgainstXSD($d);
74
    }
75
76
    public function testEntitysAndPropertiesAndNavigationProperties()
77
    {
78
        $msg = null;
79
        $metadataManager = new MetadataManager();
80
        $result = null;
81
82
        list($categoryType, $result) = $metadataManager->addEntityType('Category');
83
        $this->assertNotFalse($categoryType, 'Etype is false not type ' . $metadataManager->getLastError());
84
        assert($categoryType instanceof TEntityTypeType, get_class($categoryType));
85
        $metadataManager->addPropertyToEntityType($categoryType, 'CategoryID', 'Int32', null, false, true, 'Identity');
86
        $metadataManager->addPropertyToEntityType($categoryType, 'CategoryName', 'String');
87
        $metadataManager->addPropertyToEntityType($categoryType, 'Description', 'String');
88
        $metadataManager->addPropertyToEntityType($categoryType, 'Picture', 'Binary');
89
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
90
91
        list($customerDemographicType, $result) = $metadataManager->addEntityType('CustomerDemographic');
92
        assert($customerDemographicType instanceof TEntityTypeType, get_class($customerDemographicType));
93
        $metadataManager->addPropertyToEntityType($customerDemographicType, 'CustomerTypeID', 'String', null, false, true);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
94
        $metadataManager->addPropertyToEntityType($customerDemographicType, 'CustomerDesc', 'String');
95
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
96
97
        list($customerType, $result) = $metadataManager->addEntityType('Customer');
98
        assert($customerType instanceof TEntityTypeType, get_class($customerType));
99
        $metadataManager->addPropertyToEntityType($customerType, 'CustomerID', 'String', null, false, true);
100
        $metadataManager->addPropertyToEntityType($customerType, 'CompanyName', 'String');
101
        $metadataManager->addPropertyToEntityType($customerType, 'ContactName', 'String');
102
        $metadataManager->addPropertyToEntityType($customerType, 'ContactTitle', 'String');
103
        $metadataManager->addPropertyToEntityType($customerType, 'Address', 'String');
104
        $metadataManager->addPropertyToEntityType($customerType, 'City', 'String');
105
        $metadataManager->addPropertyToEntityType($customerType, 'Region', 'String');
106
        $metadataManager->addPropertyToEntityType($customerType, 'PostalCode', 'String');
107
        $metadataManager->addPropertyToEntityType($customerType, 'Country', 'String');
108
        $metadataManager->addPropertyToEntityType($customerType, 'Phone', 'String');
109
        $metadataManager->addPropertyToEntityType($customerType, 'Fax', 'String');
110
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
111
112
        list($employeeType, $result) = $metadataManager->addEntityType('Employee');
113
        assert($employeeType instanceof TEntityTypeType, get_class($employeeType));
114
        $metadataManager->addPropertyToEntityType($employeeType, 'EmployeeID', 'Int32', null, false, true, 'Identity');
115
        $metadataManager->addPropertyToEntityType($employeeType, 'LastName', 'String');
116
        $metadataManager->addPropertyToEntityType($employeeType, 'FirstName', 'String');
117
        $metadataManager->addPropertyToEntityType($employeeType, 'Title', 'String');
118
        $metadataManager->addPropertyToEntityType($employeeType, 'TitleOfCourtesy', 'String');
119
        $metadataManager->addPropertyToEntityType($employeeType, 'BirthDate', 'DateTime');
120
        $metadataManager->addPropertyToEntityType($employeeType, 'HireDate', 'DateTime');
121
        $metadataManager->addPropertyToEntityType($employeeType, 'Address', 'String');
122
        $metadataManager->addPropertyToEntityType($employeeType, 'City', 'String');
123
        $metadataManager->addPropertyToEntityType($employeeType, 'Region', 'String');
124
        $metadataManager->addPropertyToEntityType($employeeType, 'PostalCode', 'String');
125
        $metadataManager->addPropertyToEntityType($employeeType, 'Country', 'String');
126
        $metadataManager->addPropertyToEntityType($employeeType, 'HomePhone', 'String');
127
        $metadataManager->addPropertyToEntityType($employeeType, 'Extension', 'String');
128
        $metadataManager->addPropertyToEntityType($employeeType, 'Photo', 'Binary');
129
        $metadataManager->addPropertyToEntityType($employeeType, 'Notes', 'String');
130
        $metadataManager->addPropertyToEntityType($employeeType, 'ReportsTo', 'Int32');
131
        $metadataManager->addPropertyToEntityType($employeeType, 'PhotoPath', 'String');
132
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
133
134
        list($orderDetailType, $result) = $metadataManager->addEntityType('Order_Detail');
135
        assert($orderDetailType instanceof TEntityTypeType, get_class($orderDetailType));
136
        $metadataManager->addPropertyToEntityType($orderDetailType, 'OrderID', 'Int32', null, false, true);
137
        $metadataManager->addPropertyToEntityType($orderDetailType, 'ProductID', 'Int32', null, false, true);
138
        $metadataManager->addPropertyToEntityType($orderDetailType, 'UnitPrice', 'Decimal');
139
        $metadataManager->addPropertyToEntityType($orderDetailType, 'Quantity', 'Int16');
140
        $metadataManager->addPropertyToEntityType($orderDetailType, 'Discount', 'Single');
141
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
142
143
        list($orderType, $result) = $metadataManager->addEntityType('Order');
144
        assert($orderType instanceof TEntityTypeType, get_class($orderType));
145
        $metadataManager->addPropertyToEntityType($orderType, 'OrderID', 'Int32', null, false, true, 'Identity');
146
        $metadataManager->addPropertyToEntityType($orderType, 'CustomerID', 'String');
147
        $metadataManager->addPropertyToEntityType($orderType, 'EmployeeID', 'Int32');
148
        $metadataManager->addPropertyToEntityType($orderType, 'OrderDate', 'DateTime');
149
        $metadataManager->addPropertyToEntityType($orderType, 'RequiredDate', 'DateTime');
150
        $metadataManager->addPropertyToEntityType($orderType, 'ShippedDate', 'DateTime');
151
        $metadataManager->addPropertyToEntityType($orderType, 'ShipVia', 'DateTime');
152
        $metadataManager->addPropertyToEntityType($orderType, 'Freight', 'Decimal');
153
        $metadataManager->addPropertyToEntityType($orderType, 'ShipName', 'String');
154
        $metadataManager->addPropertyToEntityType($orderType, 'ShipAddress', 'String');
155
        $metadataManager->addPropertyToEntityType($orderType, 'ShipCity', 'String');
156
        $metadataManager->addPropertyToEntityType($orderType, 'ShipRegion', 'String');
157
        $metadataManager->addPropertyToEntityType($orderType, 'ShipPostalCode', 'String');
158
        $metadataManager->addPropertyToEntityType($orderType, 'ShipCountry', 'String');
159
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
160
161
        list($productType, $result) = $metadataManager->addEntityType('Product');
162
        assert($productType instanceof TEntityTypeType, get_class($productType));
163
        $metadataManager->addPropertyToEntityType($productType, 'ProductID', 'Int32', null, false, true, 'Identity');
164
        $metadataManager->addPropertyToEntityType($productType, 'ProductName', 'String');
165
        $metadataManager->addPropertyToEntityType($productType, 'SupplierID', 'Int32');
166
        $metadataManager->addPropertyToEntityType($productType, 'CategoryID', 'Int32');
167
        $metadataManager->addPropertyToEntityType($productType, 'QuantityPerUnit', 'String');
168
        $metadataManager->addPropertyToEntityType($productType, 'UnitPrice', 'Decimal');
169
        $metadataManager->addPropertyToEntityType($productType, 'UnitsInStock', 'Int16');
170
        $metadataManager->addPropertyToEntityType($productType, 'UnitsOnOrder', 'Int16');
171
        $metadataManager->addPropertyToEntityType($productType, 'ReorderLevel', 'Int16');
172
        $metadataManager->addPropertyToEntityType($productType, 'Discontinued', 'Boolean');
173
        $this->assertTrue($metadataManager->getEdmx()->isOK($msg), $msg);
174
175
        $expectedRelation = 'Data.Category_Products_Product_Category';
176
        list($principalNav, ) = $metadataManager->addNavigationPropertyToEntityType(
177
            $categoryType, '*', 'Products', $productType, '1', 'Category', ['CategoryID'], ['CategoryID']
178
        );
179
        $this->assertEquals($expectedRelation, $principalNav->getRelationship());
180
        $metadataManager->addNavigationPropertyToEntityType(
181
            $orderDetailType, '1', 'Order', $productType, '*', 'Order_Details', ['OrderID'], ['CategoryID']
182
        );
183
//        <NavigationProperty Name="Order_Details" Relationship="NorthwindModel.FK_Order_Details_Products" ToRole="Order_Details" FromRole="Products"/>
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 151 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
184
185
186
        $msg = null;
187
        $edmx = $metadataManager->getEdmx();
188
        $this->assertTrue($edmx->isOK($msg), $msg);
189
        $this->assertNull($msg);
190
191
        $d = $metadataManager->getEdmxXML();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $d. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
192
        $this->v3MetadataAgainstXSD($d);
193
    }
194
195
    public function testAddManyToManyNavProperty()
196
    {
197
        list($msg, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
198
199
        $expectedRelation = 'Data.Category_custom_Customer_categor';
200
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
201
            $categoryType,
202
            '*',
203
            'custom',
204
            $customerType,
205
            '*',
206
            'categor'
207
        );
208
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
209
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
210
        $this->assertEquals('custom', $principal->getName());
211
        $this->assertEquals('categor', $dependent->getName());
212
        $this->assertEquals($expectedRelation, $principal->getRelationship());
213
        $this->assertEquals($expectedRelation, $dependent->getRelationship());
214
215
        $navProps = [$principal, $dependent];
216
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
217
        $this->assertEquals(1, count($assoc));
218
        $assoc = $assoc[0];
219
        $this->assertTrue($assoc instanceof TAssociationType);
220
        $this->assertTrue($assoc->isOK($msg), $msg);
221
222
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
223
        $ends = $assoc->getEnd();
224
225
        $this->assertEquals(2, count($ends));
226
        $this->checkNavProps($navProps, $ends);
227
        list($principalEnd, $dependentEnd) = $this->figureOutEnds($ends, $principal, $dependent);
228
        $this->assertEquals('*', $principalEnd->getMultiplicity());
229
        $this->assertEquals('*', $dependentEnd->getMultiplicity());
230
    }
231
232 View Code Duplication
    public function testAddOneToManyNavProperty()
233
    {
234
        list($msg, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
235
236
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
237
            $categoryType,
238
            '*',
239
            'custom',
240
            $customerType,
241
            '1',
242
            'categor'
243
        );
244
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
245
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
246
        $this->assertEquals('custom', $principal->getName());
247
        $this->assertEquals('categor', $dependent->getName());
248
249
        $navProps = [$principal, $dependent];
250
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
251
        $this->assertEquals(1, count($assoc));
252
        $assoc = $assoc[0];
253
        $this->assertTrue($assoc instanceof TAssociationType);
254
        $this->assertTrue($assoc->isOK($msg), $msg);
255
256
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
257
        $ends = $assoc->getEnd();
258
259
        $this->assertEquals(2, count($ends));
260
        $this->checkNavProps($navProps, $ends);
261
        list($principalEnd, $dependentEnd) = $this->figureOutEnds($ends, $principal, $dependent);
262
        $this->assertEquals('*', $principalEnd->getMultiplicity());
263
        $this->assertEquals('1', $dependentEnd->getMultiplicity());
264
    }
265
266 View Code Duplication
    public function testAddManyToOneNavProperty()
267
    {
268
        list($msg, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
269
270
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
271
            $categoryType,
272
            '1',
273
            'custom',
274
            $customerType,
275
            '*',
276
            'categor'
277
        );
278
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
279
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
280
        $this->assertEquals('custom', $principal->getName());
281
        $this->assertEquals('categor', $dependent->getName());
282
283
        $navProps = [$principal, $dependent];
284
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
285
        $this->assertEquals(1, count($assoc));
286
        $assoc = $assoc[0];
287
        $this->assertTrue($assoc instanceof TAssociationType);
288
        $this->assertTrue($assoc->isOK($msg), $msg);
289
290
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
291
        $ends = $assoc->getEnd();
292
293
        $this->assertEquals(2, count($ends));
294
        $this->checkNavProps($navProps, $ends);
295
        list($principalEnd, $dependentEnd) = $this->figureOutEnds($ends, $principal, $dependent);
296
        $this->assertEquals('1', $principalEnd->getMultiplicity());
297
        $this->assertEquals('*', $dependentEnd->getMultiplicity());
298
    }
299
300 View Code Duplication
    public function testAddOneToOneForwardNavProperty()
301
    {
302
        list($msg, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
303
304
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
305
            $categoryType,
306
            '0..1',
307
            'custom',
308
            $customerType,
309
            '1',
310
            'categor'
311
        );
312
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
313
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
314
        $this->assertEquals('custom', $principal->getName());
315
        $this->assertEquals('categor', $dependent->getName());
316
317
        $navProps = [$principal, $dependent];
318
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
319
        $this->assertEquals(1, count($assoc));
320
        $assoc = $assoc[0];
321
        $this->assertTrue($assoc instanceof TAssociationType);
322
        $this->assertTrue($assoc->isOK($msg), $msg);
323
324
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
325
        $ends = $assoc->getEnd();
326
327
        $this->assertEquals(2, count($ends));
328
        $this->checkNavProps($navProps, $ends);
329
        list($principalEnd, $dependentEnd) = $this->figureOutEnds($ends, $principal, $dependent);
330
        $this->assertEquals('0..1', $principalEnd->getMultiplicity());
331
        $this->assertEquals('1', $dependentEnd->getMultiplicity());
332
    }
333
334 View Code Duplication
    public function testAddOneToOneReverseNavProperty()
335
    {
336
        list($msg, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
337
338
        list($principal, $dependent) = $metadataManager->addNavigationPropertyToEntityType(
339
            $categoryType,
340
            '1',
341
            'custom',
342
            $customerType,
343
            '0..1',
344
            'categor'
345
        );
346
        $this->assertEquals($principal->getFromRole(), $dependent->getToRole());
347
        $this->assertEquals($dependent->getFromRole(), $principal->getToRole());
348
        $this->assertEquals('custom', $principal->getName());
349
        $this->assertEquals('categor', $dependent->getName());
350
351
        $navProps = [$principal, $dependent];
352
        $assoc = $metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getAssociation();
353
        $this->assertEquals(1, count($assoc));
354
        $assoc = $assoc[0];
355
        $this->assertTrue($assoc instanceof TAssociationType);
356
        $this->assertTrue($assoc->isOK($msg), $msg);
357
358
        $this->assertEquals('Data.'.$assoc->getName(), $principal->getRelationship());
359
        $ends = $assoc->getEnd();
360
361
        $this->assertEquals(2, count($ends));
362
        $this->checkNavProps($navProps, $ends);
363
        list($principalEnd, $dependentEnd) = $this->figureOutEnds($ends, $principal, $dependent);
364
        $this->assertEquals('1', $principalEnd->getMultiplicity());
365
        $this->assertEquals('0..1', $dependentEnd->getMultiplicity());
366
    }
367
368
    public function testMetadataSerialiseRoundTrip()
369
    {
370
        $bar = new MetadataManager();
371
        $foo = new MetadataManager();
372
373
        $cereal = serialize($foo);
374
375
        $foo = unserialize($cereal);
376
        $this->assertTrue(null != $foo->getSerialiser());
377
        $this->assertEquals($bar, $foo);
378
    }
379
380
    public function testCreateSingletonBadReturnType()
381
    {
382
        $returnType = m::mock(IsOK::class);
383
        $foo = new MetadataManager();
384
385
        $expected = 'Expected return type must be either TEntityType or TComplexType';
386
        $actual = null;
387
388
        try {
389
            $foo->createSingleton(null, $returnType, null);
390
        } catch (\InvalidArgumentException $e) {
391
            $actual = $e->getMessage();
392
        }
393
        $this->assertEquals($expected, $actual);
394
    }
395
396 View Code Duplication
    public function testCreateSingletonEmptyName()
397
    {
398
        $returnType = m::mock(TEntityTypeType::class);
399
        $this->assertTrue($returnType instanceof TEntityTypeType, get_class($returnType));
400
        $foo = new MetadataManager();
401
402
        $expected = 'Name must be a non-empty string';
403
        $actual = null;
404
405
        try {
406
            $foo->createSingleton(null, $returnType, null);
407
        } catch (\InvalidArgumentException $e) {
408
            $actual = $e->getMessage();
409
        }
410
        $this->assertEquals($expected, $actual);
411
    }
412
413 View Code Duplication
    public function testCreateSingletonNonStringName()
414
    {
415
        $returnType = m::mock(TEntityTypeType::class);
416
        $this->assertTrue($returnType instanceof TEntityTypeType, get_class($returnType));
417
        $foo = new MetadataManager();
418
419
        $expected = 'Name must be a non-empty string';
420
        $actual = null;
421
422
        try {
423
            $foo->createSingleton($returnType, $returnType, null);
424
        } catch (\InvalidArgumentException $e) {
425
            $actual = $e->getMessage();
426
        }
427
        $this->assertEquals($expected, $actual);
428
    }
429
430
    public function testCreateSingletonSuccessful()
431
    {
432
        $msg = null;
433
        $name = 'singleton';
434
        $returnType = m::mock(TEntityTypeType::class)->makePartial();
435
        $returnType->shouldReceive('getName')->andReturn('doubleton');
436
437
        $entityContainer = m::mock(EntityContainer::class)->makePartial();
438
        $entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once();
439
440
        $schema = m::mock(Schema::class)->makePartial();
441
        $schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once();
442
443
        $services = m::mock(TDataServicesType::class);
444
        $services->shouldReceive('getSchema')->andReturn([$schema])->once();
445
446
        $edmx = m::mock(Edmx::class)->makePartial();
447
        $edmx->shouldReceive('getDataServiceType')->andReturn($services);
448
449
        $foo = m::mock(MetadataManager::class)->makePartial()->shouldAllowMockingProtectedMethods();
450
        $foo->shouldReceive('getEdmx')->andReturn($edmx);
451
        $foo->shouldReceive('getNamespace')->andReturn('Data')->atLeast(1);
452
453
        $result = $foo->createSingleton($name, $returnType);
454
        $this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result));
455
        $this->assertTrue($result->isOK($msg));
456
        $this->assertNull($result->getDocumentation());
457
    }
458
459
    public function testCreateSingletonSuccessfulWithEntitySet()
460
    {
461
        $msg = null;
462
        $name = 'singleton';
463
        $returnType = m::mock(TEntityTypeType::class)->makePartial();
464
        $returnType->shouldReceive('getName')->andReturn('doubleton');
465
466
        $entityContainer = m::mock(EntityContainer::class)->makePartial();
467
        $entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once();
468
469
        $schema = m::mock(Schema::class)->makePartial();
470
        $schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once();
471
472
        $services = m::mock(TDataServicesType::class);
473
        $services->shouldReceive('getSchema')->andReturn([$schema])->once();
474
475
        $edmx = m::mock(Edmx::class)->makePartial();
476
        $edmx->shouldReceive('getDataServiceType')->andReturn($services);
477
478
        $foo = m::mock(MetadataManager::class)->makePartial()->shouldAllowMockingProtectedMethods();
479
        $foo->shouldReceive('getEdmx')->andReturn($edmx);
480
        $foo->shouldReceive('getNamespace')->andReturn('Data')->atLeast(1);
481
482
        $entitySet = m::mock(EntitySetAnonymousType::class);
483
        $entitySet->shouldReceive('getName')->andReturn('BorkBorkBorken')->atLeast(1);
484
485
        $result = $foo->createSingleton($name, $returnType, $entitySet);
486
        $this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result));
487
        $this->assertTrue($result->isOK($msg));
488
        $this->assertNull($result->getDocumentation());
489
        $this->assertEquals('BorkBorkBorken', $result->getReturnType()[0]->getEntitySetAttribute());
490
    }
491
492 View Code Duplication
    public function testCreateSingletonWithDocumentation()
493
    {
494
        $msg = null;
495
        $name = 'singleton';
496
        $shortDesc = new TTextType();
497
        $longDesc = new TTextType();
498
499
        $returnType = m::mock(TEntityTypeType::class)->makePartial();
500
        $returnType->shouldReceive('getName')->andReturn('doubleton');
501
502
        $entityContainer = m::mock(EntityContainer::class)->makePartial();
503
        $entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once();
504
505
        $schema = m::mock(Schema::class)->makePartial();
506
        $schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once();
507
        $edmx = m::mock(Edmx::class)->makePartial();
508
        $edmx->shouldReceive('getDataServiceType->getSchema')->andReturn([$schema])->once();
509
510
        $foo = m::mock(MetadataManager::class)->makePartial()->shouldAllowMockingProtectedMethods();
511
        $foo->shouldReceive('getEdmx')->andReturn($edmx);
512
        $foo->shouldReceive('getNamespace')->andReturn('Data')->atLeast(1);
513
514
        $result = $foo->createSingleton($name, $returnType, null, $shortDesc, $longDesc);
515
        $this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result));
516
        $this->assertTrue($result->isOK($msg));
517
        $this->assertNotNull($result->getDocumentation());
518
    }
519
520 View Code Duplication
    public function testCreateSingletonWithDocumentationOnlyShortDesc()
521
    {
522
        $msg = null;
523
        $name = 'singleton';
524
        $shortDesc = new TTextType();
525
        $longDesc = null;
526
527
        $returnType = m::mock(TEntityTypeType::class)->makePartial();
528
        $returnType->shouldReceive('getName')->andReturn('doubleton');
529
530
        $entityContainer = m::mock(EntityContainer::class)->makePartial();
531
        $entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once();
532
533
        $schema = m::mock(Schema::class)->makePartial();
534
        $schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once();
535
        $edmx = m::mock(Edmx::class)->makePartial();
536
        $edmx->shouldReceive('getDataServiceType->getSchema')->andReturn([$schema])->once();
537
538
        $foo = m::mock(MetadataManager::class)->makePartial()->shouldAllowMockingProtectedMethods();
539
        $foo->shouldReceive('getEdmx')->andReturn($edmx);
540
        $foo->shouldReceive('getNamespace')->andReturn('Data')->atLeast(1);
541
542
        $result = $foo->createSingleton($name, $returnType, null, $shortDesc, $longDesc);
543
        $this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result));
544
        $this->assertTrue($result->isOK($msg));
545
        $this->assertNull($result->getDocumentation());
546
    }
547
548 View Code Duplication
    public function testCreateSingletonWithDocumentationOnlyLongDesc()
549
    {
550
        $msg = null;
551
        $name = 'singleton';
552
        $shortDesc = null;
553
        $longDesc = new TTextType();
554
555
        $returnType = m::mock(TEntityTypeType::class)->makePartial();
556
        $returnType->shouldReceive('getName')->andReturn('doubleton');
557
558
        $entityContainer = m::mock(EntityContainer::class)->makePartial();
559
        $entityContainer->shouldReceive('addToFunctionImport')->andReturn(null)->once();
560
561
        $schema = m::mock(Schema::class)->makePartial();
562
        $schema->shouldReceive('getEntityContainer')->andReturn([$entityContainer])->once();
563
        $edmx = m::mock(Edmx::class)->makePartial();
564
        $edmx->shouldReceive('getDataServiceType->getSchema')->andReturn([$schema])->once();
565
566
        $foo = m::mock(MetadataManager::class)->makePartial()->shouldAllowMockingProtectedMethods();
567
        $foo->shouldReceive('getEdmx')->andReturn($edmx);
568
        $foo->shouldReceive('getNamespace')->andReturn('Data')->atLeast(1);
569
570
        $result = $foo->createSingleton($name, $returnType, $shortDesc, $longDesc);
571
        $this->assertTrue($result instanceof EntityContainer\FunctionImportAnonymousType, get_class($result));
572
        $this->assertTrue($result->isOK($msg));
573
        $this->assertNull($result->getDocumentation());
574
    }
575
576 View Code Duplication
    public function testMalformedMultiplicity()
577
    {
578
        list(, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
579
580
        $expected = 'Malformed multiplicity - valid values are *, 0..1 and 1';
581
        $actual = null;
582
583
        try {
584
            $metadataManager->addNavigationPropertyToEntityType(
585
                $categoryType,
586
                '1',
587
                'Customers',
588
                $customerType,
589
                'ABC',
590
                'Categories'
591
            );
592
        } catch (\InvalidArgumentException $e) {
593
            $actual = $e->getMessage();
594
        }
595
        $this->assertEquals($expected, $actual);
596
    }
597
598 View Code Duplication
    public function testInvalidMultiplicityBelongsOnBothEnds()
599
    {
600
        list(, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
601
602
        $expected =  'Invalid multiplicity combination - 1 1';
603
        $actual = null;
604
605
        try {
606
            $metadataManager->addNavigationPropertyToEntityType(
607
                $categoryType,
608
                '1',
609
                'Customers',
610
                $customerType,
611
                '1',
612
                'Categories'
613
            );
614
        } catch (\InvalidArgumentException $e) {
615
            $actual = $e->getMessage();
616
        }
617
        $this->assertEquals($expected, $actual);
618
    }
619
620 View Code Duplication
    public function testInvalidMultiplicityManyToHasMany()
621
    {
622
        list(, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
623
624
        $expected = null;
625
        $actual = null;
626
627
        try {
628
            $metadataManager->addNavigationPropertyToEntityType(
629
                $categoryType,
630
                '*',
631
                'Customers',
632
                $customerType,
633
                '0..1',
634
                'Categories'
635
            );
636
        } catch (\InvalidArgumentException $e) {
637
            $actual = $e->getMessage();
638
        }
639
        $this->assertEquals($expected, $actual);
640
    }
641
642 View Code Duplication
    public function testAddComplexType()
643
    {
644
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
645
646
        $name = 'Name';
647
        $accessType = 'Public';
648
        $summary = new TTextType();
649
        $longDescription = new TTextType();
650
651
        $oldCount = count($metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getComplexType());
652
653
        $result = $metadataManager->addComplexType($name, $accessType, $summary, $longDescription);
654
655
        $newCount = count($metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getComplexType());
656
        $this->assertEquals($oldCount+1, $newCount);
657
        $this->assertNotNull($result);
658
        $this->assertTrue($result instanceof TComplexTypeType, get_class($result));
659
        $this->assertNotNull($result->getDocumentation());
660
    }
661
662 View Code Duplication
    public function testAddComplexTypeWithOnlySummary()
663
    {
664
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
665
666
        $name = 'Name';
667
        $accessType = 'Public';
668
        $summary = new TTextType();
669
        $longDescription = null;
670
671
        $oldCount = count($metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getComplexType());
672
673
        $result = $metadataManager->addComplexType($name, $accessType, $summary, $longDescription);
674
675
        $newCount = count($metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getComplexType());
676
        $this->assertEquals($oldCount+1, $newCount);
677
        $this->assertNotNull($result);
678
        $this->assertTrue($result instanceof TComplexTypeType, get_class($result));
679
        $this->assertNull($result->getDocumentation());
680
    }
681
682 View Code Duplication
    public function testAddComplexTypeWithOnlyDescription()
683
    {
684
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
685
686
        $name = 'Name';
687
        $accessType = 'Public';
688
        $summary = null;
689
        $longDescription = new TTextType();
690
691
        $oldCount = count($metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getComplexType());
692
693
        $result = $metadataManager->addComplexType($name, $accessType, $summary, $longDescription);
694
695
        $newCount = count($metadataManager->getEdmx()->getDataServiceType()->getSchema()[0]->getComplexType());
696
        $this->assertEquals($oldCount+1, $newCount);
697
        $this->assertNotNull($result);
698
        $this->assertTrue($result instanceof TComplexTypeType, get_class($result));
699
        $this->assertNull($result->getDocumentation());
700
    }
701
702 View Code Duplication
    public function testAddPropertyToComplexTypeDefaultValueArray()
703
    {
704
        $expected = 'Default value cannot be object or array';
705
        $actual = null;
706
707
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
708
        $complex = m::mock(TComplexTypeType::class);
709
        $name = 'name';
710
        $type = 'type';
711
        $defaultValue = [];
712
713
        try {
714
            $metadataManager->addPropertyToComplexType($complex, $name, $type, $defaultValue);
715
        } catch (\InvalidArgumentException $e) {
716
            $actual = $e->getMessage();
717
        }
718
        $this->assertEquals($expected, $actual);
719
    }
720
721 View Code Duplication
    public function testAddPropertyToComplexTypeDefaultValueObject()
722
    {
723
        $expected = 'Default value cannot be object or array';
724
        $actual = null;
725
726
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
727
        $complex = m::mock(TComplexTypeType::class);
728
        $name = 'name';
729
        $type = 'type';
730
        $defaultValue = new \stdClass();
731
732
        try {
733
            $metadataManager->addPropertyToComplexType($complex, $name, $type, $defaultValue);
734
        } catch (\InvalidArgumentException $e) {
735
            $actual = $e->getMessage();
736
        }
737
        $this->assertEquals($expected, $actual);
738
    }
739
740 View Code Duplication
    public function testAddPropertyToComplexTypeDefaultValueBoolean()
741
    {
742
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
743
        $complex = m::mock(TComplexTypeType::class);
744
        $complex->shouldReceive('addToProperty')
745
            ->with(m::type(TComplexTypePropertyType::class))->andReturnNull()->once();
746
        $name = 'name';
747
        $type = 'type';
748
        $defaultValue = true;
749
        $summary = new TTextType();
750
        $longDescription = new TTextType();
751
        $expectedDefault = 'true';
752
753
        $result = $metadataManager->addPropertyToComplexType(
754
            $complex,
755
            $name,
756
            $type,
757
            $defaultValue,
758
            false,
759
            $summary,
760
            $longDescription
761
        );
762
        $this->assertEquals(1, count($result->getDocumentation()));
763
        $this->assertEquals($expectedDefault, $result->getDefaultValue());
764
    }
765
766 View Code Duplication
    public function testAddPropertyToComplexTypeDefaultValueBooleanOnlySummary()
767
    {
768
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
769
        $complex = m::mock(TComplexTypeType::class);
770
        $complex->shouldReceive('addToProperty')
771
            ->with(m::type(TComplexTypePropertyType::class))->andReturnNull()->once();
772
        $name = 'name';
773
        $type = 'type';
774
        $defaultValue = true;
775
        $summary = new TTextType();
776
        $longDescription = null;
777
        $expectedDefault = 'true';
778
779
        $result = $metadataManager->addPropertyToComplexType(
780
            $complex,
781
            $name,
782
            $type,
783
            $defaultValue,
784
            false,
785
            $summary,
786
            $longDescription
787
        );
788
        $this->assertNotNull($result);
789
        $this->assertEquals(0, count($result->getDocumentation()));
790
        $this->assertEquals($expectedDefault, $result->getDefaultValue());
791
    }
792
793 View Code Duplication
    public function testAddPropertyToComplexTypeDefaultValueBooleanOnlyDescription()
794
    {
795
        list(, $metadataManager, , ) = $this->setUpMetadataForNavTests();
796
        $complex = m::mock(TComplexTypeType::class);
797
        $complex->shouldReceive('addToProperty')
798
            ->with(m::type(TComplexTypePropertyType::class))->andReturnNull()->once();
799
        $name = 'name';
800
        $type = 'type';
801
        $defaultValue = true;
802
        $summary = null;
803
        $longDescription = new TTextType();
804
        $expectedDefault = 'true';
805
806
        $result = $metadataManager->addPropertyToComplexType(
807
            $complex,
808
            $name,
809
            $type,
810
            $defaultValue,
811
            false,
812
            $summary,
813
            $longDescription
814
        );
815
        $this->assertEquals(0, count($result->getDocumentation()));
816
        $this->assertEquals($expectedDefault, $result->getDefaultValue());
817
    }
818
819 View Code Duplication
    public function testAddPropertyToEntityType()
820
    {
821
        $metadataManager = new MetadataManager();
822
        $entity = m::mock(TEntityTypeType::class);
823
        $entity->shouldReceive('addToProperty')
824
            ->with(m::type(TEntityPropertyType::class))->andReturnNull()->once();
825
        $name = 'name';
826
        $type = 'type';
827
        $summary = new TTextType();
828
        $defaultValue = 'true';
829
        $longDescription = new TTextType();
830
831
        $result = $metadataManager->addPropertyToEntityType(
832
            $entity,
833
            $name,
834
            $type,
835
            $defaultValue,
836
            false,
837
            false,
838
            null,
839
            $summary,
840
            $longDescription
841
        );
842
        $this->assertNotNull($result);
843
        $this->assertTrue(is_array($result->getDocumentation()));
844
        $this->assertEquals(1, count($result->getDocumentation()));
845
        $this->assertEquals('true', $result->getDefaultValue());
846
    }
847
848 View Code Duplication
    public function testAddPropertyToEntityTypeOnlySummary()
849
    {
850
        $metadataManager = new MetadataManager();
851
        $entity = m::mock(TEntityTypeType::class);
852
        $entity->shouldReceive('addToProperty')
853
            ->with(m::type(TEntityPropertyType::class))->andReturnNull()->once();
854
        $name = 'name';
855
        $type = 'type';
856
        $summary = new TTextType();
857
        $defaultValue = 'true';
858
        $longDescription = null;
859
860
        $result = $metadataManager->addPropertyToEntityType(
861
            $entity,
862
            $name,
863
            $type,
864
            $defaultValue,
865
            false,
866
            false,
867
            null,
868
            $summary,
869
            $longDescription
870
        );
871
        $this->assertNotNull($result);
872
        $this->assertTrue(is_array($result->getDocumentation()));
873
        $this->assertEquals(0, count($result->getDocumentation()));
874
        $this->assertEquals('true', $result->getDefaultValue());
875
    }
876
877 View Code Duplication
    public function testAddPropertyToEntityTypeOnlyDescription()
878
    {
879
        $metadataManager = new MetadataManager();
880
        $entity = m::mock(TEntityTypeType::class);
881
        $entity->shouldReceive('addToProperty')
882
            ->with(m::type(TEntityPropertyType::class))->andReturnNull()->once();
883
        $name = 'name';
884
        $type = 'type';
885
        $summary = null;
886
        $defaultValue = 'true';
887
        $longDescription = new TTextType();
888
889
        $result = $metadataManager->addPropertyToEntityType(
890
            $entity,
891
            $name,
892
            $type,
893
            $defaultValue,
894
            false,
895
            false,
896
            null,
897
            $summary,
898
            $longDescription
899
        );
900
        $this->assertNotNull($result);
901
        $this->assertTrue(is_array($result->getDocumentation()));
902
        $this->assertEquals(0, count($result->getDocumentation()));
903
        $this->assertEquals('true', $result->getDefaultValue());
904
    }
905
906 View Code Duplication
    public function testAddEntityTypeWithDocumentation()
907
    {
908
        $name = 'name';
909
        $accessType = 'Public';
910
        $summary = new TTextType();
911
        $longDescription = new TTextType();
912
913
        $metadataManager = new MetadataManager();
914
        list($result, ) = $metadataManager->addEntityType($name, null, false, $accessType, $summary, $longDescription);
915
        $this->assertNotNull($result->getDocumentation());
916
    }
917
918 View Code Duplication
    public function testAddEntityTypeWithDocumentationFromOnlySummary()
919
    {
920
        $name = 'name';
921
        $accessType = 'Public';
922
        $summary = new TTextType();
923
        $longDescription = null;
924
925
        $metadataManager = new MetadataManager();
926
        list($result, ) = $metadataManager->addEntityType($name, null, false, $accessType, $summary, $longDescription);
927
        $this->assertNull($result->getDocumentation());
928
    }
929
930 View Code Duplication
    public function testAddEntityTypeWithDocumentationFromOnlyDocumentation()
931
    {
932
        $name = 'name';
933
        $accessType = 'Public';
934
        $summary = null;
935
        $longDescription = new TTextType();
936
937
        $metadataManager = new MetadataManager();
938
        list($result, ) = $metadataManager->addEntityType($name, null, false, $accessType, $summary, $longDescription);
939
        $this->assertNull($result->getDocumentation());
940
    }
941
942 View Code Duplication
    public function testAddNavigationPropertyToEntityTypeWithDocumentation()
943
    {
944
        list(, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
945
946
        $summary = new TTextType();
947
        $longDescription = new TTextType();
948
        $mult = '*';
949
        $principalProperty = 'Categories';
950
        $dependentProperty = 'Customers';
951
952
        list($principal, $dependent) = $metadataManager
953
            ->addNavigationPropertyToEntityType(
954
                $categoryType,
955
                $mult,
956
                $principalProperty,
957
                $customerType,
958
                $mult,
959
                $dependentProperty,
960
                null,
961
                null,
962
                'Public',
963
                'Public',
964
                'Public',
965
                'Public',
966
                $summary,
967
                $longDescription,
968
                $summary,
969
                $longDescription
970
            );
971
972
        $this->assertNotNull($principal->getDocumentation());
973
        $this->assertNotNull($dependent->getDocumentation());
974
    }
975
976 View Code Duplication
    public function testAddNavigationPropertyToEntityTypeWithDocumentationWithOnlySummary()
977
    {
978
        list(, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
979
980
        $summary = null;
981
        $longDescription = new TTextType();
982
        $mult = '*';
983
        $principalProperty = 'Categories';
984
        $dependentProperty = 'Customers';
985
986
        list($principal, $dependent) = $metadataManager
987
            ->addNavigationPropertyToEntityType(
988
                $categoryType,
989
                $mult,
990
                $principalProperty,
991
                $customerType,
992
                $mult,
993
                $dependentProperty,
994
                null,
995
                null,
996
                'Public',
997
                'Public',
998
                'Public',
999
                'Public',
1000
                $summary,
1001
                $longDescription,
1002
                $summary,
1003
                $longDescription
1004
            );
1005
1006
        $this->assertNull($principal->getDocumentation());
1007
        $this->assertNull($dependent->getDocumentation());
1008
    }
1009
1010 View Code Duplication
    public function testAddNavigationPropertyToEntityTypeWithDocumentationWithOnlyDescription()
1011
    {
1012
        list(, $metadataManager, $categoryType, $customerType) = $this->setUpMetadataForNavTests();
1013
1014
        $summary = new TTextType();
1015
        $longDescription = null;
1016
        $mult = '*';
1017
        $principalProperty = 'Categories';
1018
        $dependentProperty = 'Customers';
1019
1020
        list($principal, $dependent) = $metadataManager
1021
            ->addNavigationPropertyToEntityType(
1022
                $categoryType,
1023
                $mult,
1024
                $principalProperty,
1025
                $customerType,
1026
                $mult,
1027
                $dependentProperty,
1028
                null,
1029
                null,
1030
                'Public',
1031
                'Public',
1032
                'Public',
1033
                'Public',
1034
                $summary,
1035
                $longDescription,
1036
                $summary,
1037
                $longDescription
1038
            );
1039
1040
        $this->assertNull($principal->getDocumentation());
1041
        $this->assertNull($dependent->getDocumentation());
1042
    }
1043
1044
    public function testCreateAssociationFromNavigationPropertyRelationMismatch()
1045
    {
1046
        $principalType = m::mock(TEntityTypeType::class);
1047
        $dependentType = m::mock(TEntityTypeType::class);
1048
        $principalNav = m::mock(TNavigationPropertyType::class);
1049
        $principalNav->shouldReceive('getRelationship')->andReturn('foo')->once();
1050
        $dependentNav = m::mock(TNavigationPropertyType::class);
1051
        $dependentNav->shouldReceive('getRelationship')->andReturn('bar')->once();
1052
1053
        $metadataManager = new MetadataManagerDummy();
1054
1055
        $expected = 'If you have both a dependent property and a principal property, relationship should match';
1056
        $actual = null;
1057
1058
        try {
1059
            $metadataManager->createAssocationFromNavigationProperty(
1060
                $principalType,
1061
                $dependentType,
1062
                $principalNav,
1063
                $dependentNav,
1064
                '*',
1065
                '*'
1066
            );
1067
        } catch (\InvalidArgumentException $e) {
1068
            $actual = $e->getMessage();
1069
        }
1070
        $this->assertEquals($expected, $actual);
1071
    }
1072
1073 View Code Duplication
    public function testCreateAssociationFromNavigationPropertyForwardRoleMismatch()
1074
    {
1075
        $principalType = m::mock(TEntityTypeType::class);
1076
        $dependentType = m::mock(TEntityTypeType::class);
1077
        $principalNav = m::mock(TNavigationPropertyType::class);
1078
        $principalNav->shouldReceive('getRelationship')->andReturn('foo')->once();
1079
        $principalNav->shouldReceive('getToRole')->andReturn('Forwards');
1080
        $principalNav->shouldReceive('getFromRole')->andReturn('Reverse');
1081
        $dependentNav = m::mock(TNavigationPropertyType::class);
1082
        $dependentNav->shouldReceive('getRelationship')->andReturn('foo')->once();
1083
        $dependentNav->shouldReceive('getToRole')->andReturn('Reverse');
1084
        $dependentNav->shouldReceive('getFromRole')->andReturn('Sideways');
1085
1086
        $metadataManager = new MetadataManagerDummy();
1087
1088
        $expected = 'Principal to role should match dependent from role, and vice versa';
1089
        $actual = null;
1090
1091
        try {
1092
            $metadataManager->createAssocationFromNavigationProperty(
1093
                $principalType,
1094
                $dependentType,
1095
                $principalNav,
1096
                $dependentNav,
1097
                '*',
1098
                '*'
1099
            );
1100
        } catch (\InvalidArgumentException $e) {
1101
            $actual = $e->getMessage();
1102
        }
1103
        $this->assertEquals($expected, $actual);
1104
    }
1105
1106 View Code Duplication
    public function testCreateAssociationFromNavigationPropertyReverseRoleMismatch()
1107
    {
1108
        $principalType = m::mock(TEntityTypeType::class);
1109
        $dependentType = m::mock(TEntityTypeType::class);
1110
        $principalNav = m::mock(TNavigationPropertyType::class);
1111
        $principalNav->shouldReceive('getRelationship')->andReturn('foo')->once();
1112
        $principalNav->shouldReceive('getToRole')->andReturn('Forwards');
1113
        $principalNav->shouldReceive('getFromRole')->andReturn('Reverse');
1114
        $dependentNav = m::mock(TNavigationPropertyType::class);
1115
        $dependentNav->shouldReceive('getRelationship')->andReturn('foo')->once();
1116
        $dependentNav->shouldReceive('getToRole')->andReturn('Sideways');
1117
        $dependentNav->shouldReceive('getFromRole')->andReturn('Forwards');
1118
1119
        $metadataManager = new MetadataManagerDummy();
1120
1121
        $expected = 'Principal to role should match dependent from role, and vice versa';
1122
        $actual = null;
1123
1124
        try {
1125
            $metadataManager->createAssocationFromNavigationProperty(
1126
                $principalType,
1127
                $dependentType,
1128
                $principalNav,
1129
                $dependentNav,
1130
                '*',
1131
                '*'
1132
            );
1133
        } catch (\InvalidArgumentException $e) {
1134
            $actual = $e->getMessage();
1135
        }
1136
        $this->assertEquals($expected, $actual);
1137
    }
1138
1139
    public function testGetEmptyNamespace()
1140
    {
1141
        $schema = m::mock(Schema::class);
1142
        $schema->shouldReceive('getNamespace')->andReturn(' ');
1143
1144
        $edmx = m::mock(Edmx::class);
1145
        $edmx->shouldReceive('isOk')->andReturn(true);
1146
        $edmx->shouldReceive('getDataServiceType->getSchema')->andReturn([$schema]);
1147
1148
        $foo = new MetadataManagerDummy('Data', 'DefaultContainer', $edmx);
1149
1150
        $expected = '';
1151
        $actual = $foo->getNamespace();
1152
        $this->assertEquals($expected, $actual);
1153
    }
1154
1155
    public function testInitSerialiserFromNull()
1156
    {
1157
        $cereal = m::mock(Serializer::class);
1158
        $cereal->shouldReceive('serialize')->andReturn('cereal')->once();
1159
1160
        $foo = m::mock(MetadataManager::class)->makePartial()->shouldAllowMockingProtectedMethods();
1161
        $foo->shouldReceive('initSerialiser')->andReturn(null)->once();
1162
        $foo->shouldReceive('getSerialiser')->andReturn($cereal)->once();
1163
        $foo->shouldReceive('getEdmx')->andReturn(null)->once();
1164
1165
        $expected = 'cereal';
1166
        $actual = $foo->getEdmxXML();
1167
        $this->assertEquals($expected, $actual);
1168
    }
1169
1170
    public function testStaticGetterMethods()
1171
    {
1172
        $this->setUpMetadataForNavTests();
1173
1174
        $expectedCustomerType = 'Customer';
1175
        $expectedCategoryType = 'Category';
1176
1177
        $actualCustomerType = MetadataManager::getResourceTypeNameFromResourceSet('Customers');
1178
        $this->assertEquals($expectedCustomerType, $actualCustomerType);
1179
        $actualCategoryType = MetadataManager::getResourceTypeNameFromResourceSet('Categories');
1180
        $this->assertEquals($expectedCategoryType, $actualCategoryType);
1181
    }
1182
1183
    /**
1184
     * @return array
1185
     */
1186
    private function setUpMetadataForNavTests()
1187
    {
1188
        $msg = null;
1189
        $metadataManager = new MetadataManager('Data', 'Container');
1190
        $expectedCategorySetName = 'Categories';
1191
        $expectedCustomerSetName = 'Customers';
1192
1193
        list($categoryType, $categorySet) = $metadataManager->addEntityType('Category');
1194
        list($customerType, $customerSet) = $metadataManager->addEntityType('Customer');
1195
        $this->assertTrue($categoryType->isOK($msg), $msg);
1196
        $this->assertTrue($customerType->isOK($msg), $msg);
1197
        $this->assertEquals($expectedCategorySetName, $categorySet->getName());
1198
        $this->assertEquals($expectedCustomerSetName, $customerSet->getName());
1199
        return [$msg, $metadataManager, $categoryType, $customerType];
1200
    }
1201
1202
    /**
1203
     * @param $navProps
1204
     * @param $ends
1205
     */
1206
    private function checkNavProps($navProps, $ends)
1207
    {
1208
        foreach ($navProps as $prop) {
1209
            $propToRole = $prop->getToRole();
1210
            $propFromRole = $prop->getFromRole();
1211
            $fromMatch = $ends[0]->getRole() == $propToRole
1212
                         || $ends[1]->getRole() == $propToRole;
1213
            $this->assertTrue($fromMatch, 'toRole must match at least one end role');
1214
            if ($ends[0]->getRole() == $propToRole) {
1215
                $this->assertEquals($ends[1]->getRole(), $propFromRole);
1216
                $this->assertNotEquals($ends[0]->getRole(), $propFromRole);
1217
            } else {
1218
                $this->assertEquals($ends[0]->getRole(), $propFromRole);
1219
                $this->assertNotEquals($ends[1]->getRole(), $propFromRole);
1220
            }
1221
        }
1222
    }
1223
1224
    /**
1225
     * @param $ends
1226
     * @param $principal
1227
     * @param $dependent
1228
     * @return array
1229
     */
1230
    private function figureOutEnds($ends, $principal, $dependent)
1231
    {
1232
        // if role is from Products, then type must be from Products - ie, use getFromRole
1233
        $principalEnd = ($ends[0]->getRole() == $principal->getFromRole()) ? $ends[0] : $ends[1];
1234
        $dependentEnd = ($ends[0]->getRole() == $dependent->getFromRole()) ? $ends[0] : $ends[1];
1235
        return [$principalEnd, $dependentEnd];
1236
    }
1237
}
1238