Passed
Push — master ( 317eb4...7e3691 )
by Alex
04:52
created

testWithSingleEntitySerializeDeserializeRoundTrip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace AlgoWeb\ODataMetadata\Tests;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edm\Schema;
6
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx;
7
use AlgoWeb\ODataMetadata\MetadataV3\edmx\TDataServicesType;
8
use AlgoWeb\ODataMetadata\MetadataV3\edmx\TEdmxType;
9
10
class EdmxTest extends TestCase
11
{
12
    public function testIsOKAtDefault()
13
    {
14
        $msg = null;
15
        $edmx = new Edmx();
16
        $this->assertTrue($edmx->isOK($msg), $msg);
17
    }
18
19
    public function testDefaultSerializeOk()
20
    {
21
        $ds = DIRECTORY_SEPARATOR;
22
        $msg = null;
23
        $edmx = new Edmx();
24
        $this->assertTrue($edmx->isOK($msg), $msg);
25
        $this->assertNull($msg);
26
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
27
        $serializer =
28
            \JMS\Serializer\SerializerBuilder::create()
29
                ->addMetadataDir($ymlDir)
30
                ->build();
31
        $d = $serializer->serialize($edmx, "xml");
32
        $this->v3MetadataAgainstXSD($d);
33
    }
34
35 View Code Duplication
    public function v3MetadataAgainstXSD($data)
0 ignored issues
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...
36
    {
37
        $ds = DIRECTORY_SEPARATOR;
38
39
        $goodxsd = dirname(__DIR__) . $ds . "xsd" . $ds . "Microsoft.Data.Entity.Design.Edmx_3.Fixed.xsd";
40
        if (!file_exists($goodxsd)) {
41
            return true;
42
        }
43
        $xml = new \DOMDocument();
44
        $xml->loadXML($data);
45
        $xml->schemaValidate($goodxsd);
46
    }
47
48
    public function testDefaultSerializeDeserializeRoundTrip()
49
    {
50
        $ds = DIRECTORY_SEPARATOR;
51
        $msg = null;
52
        $edmx = new Edmx();
53
        $this->assertTrue($edmx->isOK($msg), $msg);
54
        $this->assertNull($msg);
55
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
56
    }
57
58
    /**
59
     * @param $ds
60
     * @param $edmx
61
     * @param $msg
62
     */
63
    private function checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg)
64
    {
65
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
66
        $serializer =
67
            \JMS\Serializer\SerializerBuilder::create()
68
                ->addMetadataDir($ymlDir)
69
                ->build();
70
        $d1 = $serializer->serialize($edmx, "xml");
71
        $this->v3MetadataAgainstXSD($d1);
72
        $edmxElectricBoogaloo = $serializer->deserialize($d1, get_class($edmx), 'xml');
73
        $this->assertTrue($edmxElectricBoogaloo->isOK($msg), $msg);
74
        $this->assertEquals($edmx, $edmxElectricBoogaloo);
75
        // and final serialize pass to further constrain undetected misbehaviour
76
        $d2 = $serializer->serialize($edmxElectricBoogaloo, 'xml');
77
        $this->v3MetadataAgainstXSD($d2);
78
        $this->assertEquals($d1, $d2);
79
    }
80
81
    public function testFirstLevelSerializeDeserializeRoundTrip()
82
    {
83
        $ds = DIRECTORY_SEPARATOR;
84
        $msg = null;
85
        $schema = new Schema('data', 'metadata');
86
        $this->assertTrue($schema->isOK($msg), $msg);
87
        $services = new TDataServicesType('3.0', '1.0');
88
        $services->addToSchema($schema);
89
        $this->assertTrue($services->isOK($msg), $msg);
90
        $edmx = new Edmx();
91
        $edmx->setDataServiceType($services);
92
        $this->assertTrue($edmx->isOK($msg), $msg);
93
        $this->assertNull($msg);
94
95
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
96
    }
97
98
    public function testWithSingleEntitySerializeOk()
99
    {
100
        $ds = DIRECTORY_SEPARATOR;
101
        $msg = null;
102
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
103
        $NewEntity->setName("simpleEntityType");
104
        $this->assertTrue($NewEntity->isOK($msg), $msg);
105
        $this->assertNull($msg);
106
        $edmx = new Edmx();
107
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
108
        $this->assertTrue($edmx->isOK($msg), $msg);
109
        $this->assertNull($msg);
110
111
112
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
113
        $serializer =
114
            \JMS\Serializer\SerializerBuilder::create()
115
                ->addMetadataDir($ymlDir)
116
                ->build();
117
        $d = $serializer->serialize($edmx, "xml");
118
119
        $this->v3MetadataAgainstXSD($d);
120
    }
121
122
    public function testWithSingleEntitySerializeDeserializeRoundTrip()
123
    {
124
        $ds = DIRECTORY_SEPARATOR;
125
        $msg = null;
126
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
127
        $NewEntity->setName("simpleEntityType");
128
        $this->assertTrue($NewEntity->isOK($msg), $msg);
129
        $this->assertNull($msg);
130
        $edmx = new Edmx();
131
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
132
        $this->assertTrue($edmx->isOK($msg), $msg);
133
        $this->assertNull($msg);
134
135
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
136
    }
137
138 View Code Duplication
    public function testKnownGoodV3DocumentDeserialiseToOk()
0 ignored issues
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...
139
    {
140
        $this->markTestSkipped('Skipped until service-document models get implemented');
141
        $ds = DIRECTORY_SEPARATOR;
142
        $msg = null;
143
144
        $docLocation = dirname(__DIR__) . $ds . "tests" . $ds . "exampleV3ServiceDocument.xml";
145
        $document = file_get_contents($docLocation);
146
        $type = 'AlgoWeb\ODataMetadata\MetadataV3\edmx\TDataServicesType';
147
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
148
149
        $serializer =
150
            \JMS\Serializer\SerializerBuilder::create()
151
                ->addMetadataDir($ymlDir)
152
                ->build();
153
154
        $d = $serializer->deserialize($document, $type, 'xml');
155
        $this->assertTrue($d instanceof TDataServicesType, get_class($this));
156
        $this->assertTrue($d->isOK($msg), $msg);
157
    }
158
159 View Code Duplication
    public function testKnownGoodV3MetadataDeserialiseToOk()
0 ignored issues
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...
160
    {
161
        $ds = DIRECTORY_SEPARATOR;
162
        $msg = null;
163
164
        $docLocation = dirname(__DIR__) . $ds . "tests" . $ds . "exampleV3ServiceMetadata.xml";
165
        $document = file_get_contents($docLocation);
166
        $type = 'AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx';
167
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
168
169
        $serializer =
170
            \JMS\Serializer\SerializerBuilder::create()
171
                ->addMetadataDir($ymlDir)
172
                ->build();
173
174
        $d = $serializer->deserialize($document, $type, 'xml');
175
        $this->assertTrue($d instanceof TEdmxType, get_class($d));
176
        $this->assertTrue($d->isOK($msg), $msg);
177
    }
178
179
    public function testKnownGoodV3MetadataDeserialiseToOkSerializeDeserializeRoundTrip()
180
    {
181
        $this->markTestSkipped();
182
        $ds = DIRECTORY_SEPARATOR;
183
        $msg = null;
184
185
        $docLocation = dirname(__DIR__) . $ds . "tests" . $ds . "exampleV3ServiceMetadata.xml";
186
        $document = file_get_contents($docLocation);
187
        $this->v3MetadataAgainstXSD($document);
188
        $type = 'AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx';
189
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
190
191
        $serializer =
192
            \JMS\Serializer\SerializerBuilder::create()
193
                ->addMetadataDir($ymlDir)
194
                ->build();
195
196
        $d = $serializer->deserialize($document, $type, 'xml');
197
        $this->assertTrue($d instanceof TEdmxType, get_class($d));
198
        $this->assertTrue($d->isOK($msg), $msg);
199
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $d, $msg);
200
    }
201
202
    public function testWithSingleEntityWithPropertiesSerializeOk()
203
    {
204
        $ds = DIRECTORY_SEPARATOR;
205
        $msg = null;
206
        $NewProperty = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType();
207
        $NewProperty->setName("TheFirstProperty");
208
        $NewProperty->setType("String");
209
        $this->assertTrue($NewProperty->isOK($msg), $msg);
210
        $this->assertNull($msg);
211
212
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
213
        $NewEntity->setName("simpleEntityType");
214
        $NewEntity->addToProperty($NewProperty);
215
        $this->assertTrue($NewEntity->isOK($msg), $msg);
216
        $this->assertNull($msg);
217
218
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
219
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
220
        $entitySet->setEntityType($NewEntity->getname());
221
        $this->assertTrue($entitySet->isOK($msg), $msg);
222
        $this->assertNull($msg);
223
        $edmx = new Edmx();
224
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
225
        $edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
226
        $this->assertTrue($edmx->isOK($msg), $msg);
227
        $this->assertNull($msg);
228
229
230
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
231
        $serializer =
232
            \JMS\Serializer\SerializerBuilder::create()
233
                ->addMetadataDir($ymlDir)
234
                ->build();
235
        $d = $serializer->serialize($edmx, "xml");
236
        $this->v3MetadataAgainstXSD($d);
237
    }
238
239
    /**
240
     * Pluralizes a word if quantity is not one.
241
     *
242
     * @param int $quantity Number of items
243
     * @param string $singular Singular form of word
244
     * @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided
245
     * @return string Pluralized word if quantity is not one, otherwise singular
246
     */
247 View Code Duplication
    public static function pluralize($quantity, $singular, $plural = null)
0 ignored issues
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...
248
    {
249
        if ($quantity == 1 || !strlen($singular)) {
250
            return $singular;
251
        }
252
        if ($plural !== null) {
253
            return $plural;
254
        }
255
256
        $last_letter = strtolower($singular[strlen($singular) - 1]);
257
        switch ($last_letter) {
258
            case 'y':
259
                return substr($singular, 0, -1) . 'ies';
260
            case 's':
261
                return $singular . 'es';
262
            default:
263
                return $singular . 's';
264
        }
265
    }
266
267
    public function testWithSingleEntityWithPropertiesSerializeDeserializeRoundTrip()
268
    {
269
        $ds = DIRECTORY_SEPARATOR;
270
        $msg = null;
271
        $NewProperty = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType();
272
        $NewProperty->setName("TheFirstProperty");
273
        $NewProperty->setType("String");
274
        $this->assertTrue($NewProperty->isOK($msg), $msg);
275
        $this->assertNull($msg);
276
277
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
278
        $NewEntity->setName("simpleEntityType");
279
        $NewEntity->addToProperty($NewProperty);
280
        $this->assertTrue($NewEntity->isOK($msg), $msg);
281
        $this->assertNull($msg);
282
283
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
284
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
285
        $entitySet->setEntityType($NewEntity->getname());
286
        $this->assertTrue($entitySet->isOK($msg), $msg);
287
        $this->assertNull($msg);
288
        $edmx = new Edmx();
289
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
290
        $edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
291
        $this->assertTrue($edmx->isOK($msg), $msg);
292
        $this->assertNull($msg);
293
294
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
295
    }
296
297
    public function testWithSingleEntityWithoutPropertiesSerializeDeserializeRoundTrip()
298
    {
299
        $ds = DIRECTORY_SEPARATOR;
300
        $msg = null;
301
302
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
303
        $NewEntity->setName("simpleEntityType");
304
        $this->assertTrue($NewEntity->isOK($msg), $msg);
305
        $this->assertNull($msg);
306
307
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
308
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
309
        $entitySet->setEntityType($NewEntity->getname());
310
        $this->assertTrue($entitySet->isOK($msg), $msg);
311
        $this->assertNull($msg);
312
        $edmx = new Edmx();
313
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
314
        $edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
315
        $this->assertTrue($edmx->isOK($msg), $msg);
316
        $this->assertNull($msg);
317
318
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
319
    }
320
}
321