Test Failed
Pull Request — master (#97)
by Christopher
08:39
created

testWithSingleEntityWithPropertiesSerializeDeserializeRoundTrip()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
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
8
use Illuminate\Support\Str;
9
10
use AlgoWeb\ODataMetadata\MetadataV3\edmx\TDataServicesType;
11
use AlgoWeb\ODataMetadata\MetadataV3\edmx\TEdmxType;
12
13
class EdmxTest extends TestCase
14
{
15
    public function testIsOKAtDefault()
16
    {
17
        $msg = null;
18
        $edmx = new Edmx();
19
        $this->assertTrue($edmx->isOK($msg), $msg);
20
    }
21
22
    public function testDefaultSerializeOk()
23
    {
24
        $ds = DIRECTORY_SEPARATOR;
25
        $msg = null;
26
        $edmx = new Edmx();
27
        $this->assertTrue($edmx->isOK($msg), $msg);
28
        $this->assertNull($msg);
29
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
30
        $serializer =
31
            \JMS\Serializer\SerializerBuilder::create()
32
                ->addMetadataDir($ymlDir)
33
                ->build();
34
        $d = $serializer->serialize($edmx, "xml");
35
        $this->v3MetadataAgainstXSD($d);
36
    }
37
38 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...
39
    {
40
        $ds = DIRECTORY_SEPARATOR;
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
        $xml->schemaValidate($goodxsd);
49
    }
50
51
    public function testDefaultSerializeDeserializeRoundTrip()
52
    {
53
        $ds = DIRECTORY_SEPARATOR;
54
        $msg = null;
55
        $edmx = new Edmx();
56
        $this->assertTrue($edmx->isOK($msg), $msg);
57
        $this->assertNull($msg);
58
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
59
    }
60
61
    /**
62
     * @param $ds
63
     * @param $edmx
64
     * @param $msg
65
     */
66
    private function checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg)
67
    {
68
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
69
        $serializer =
70
            \JMS\Serializer\SerializerBuilder::create()
71
                ->addMetadataDir($ymlDir)
72
                ->build();
73
        $d1 = $serializer->serialize($edmx, "xml");
74
        $this->v3MetadataAgainstXSD($d1);
75
        $edmxElectricBoogaloo = $serializer->deserialize($d1, get_class($edmx), 'xml');
76
        $this->assertTrue($edmxElectricBoogaloo->isOK($msg), $msg);
77
        $this->assertEquals($edmx, $edmxElectricBoogaloo);
78
        // and final serialize pass to further constrain undetected misbehaviour
79
        $d2 = $serializer->serialize($edmxElectricBoogaloo, 'xml');
80
        $this->v3MetadataAgainstXSD($d2);
81
        $this->assertEquals($d1, $d2);
82
    }
83
84
    public function testFirstLevelSerializeDeserializeRoundTrip()
85
    {
86
        $ds = DIRECTORY_SEPARATOR;
87
        $msg = null;
88
        $schema = new Schema('data', 'metadata');
89
        $this->assertTrue($schema->isOK($msg), $msg);
90
        $services = new TDataServicesType('3.0', '1.0');
91
        $services->addToSchema($schema);
92
        $this->assertTrue($services->isOK($msg), $msg);
93
        $edmx = new Edmx();
94
        $edmx->setDataServiceType($services);
95
        $this->assertTrue($edmx->isOK($msg), $msg);
96
        $this->assertNull($msg);
97
98
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
99
    }
100
101
    public function testWithSingleEntitySerializeOk()
102
    {
103
        $ds = DIRECTORY_SEPARATOR;
104
        $msg = null;
105
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
106
        $NewEntity->setName("simpleEntityType");
107
        $this->assertTrue($NewEntity->isOK($msg), $msg);
108
        $this->assertNull($msg);
109
        $edmx = new Edmx();
110
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
111
        $this->assertTrue($edmx->isOK($msg), $msg);
112
        $this->assertNull($msg);
113
114
115
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
116
        $serializer =
117
            \JMS\Serializer\SerializerBuilder::create()
118
                ->addMetadataDir($ymlDir)
119
                ->build();
120
        $d = $serializer->serialize($edmx, "xml");
121
122
        $this->v3MetadataAgainstXSD($d);
123
    }
124
125
    public function testWithSingleEntitySerializeDeserializeRoundTrip()
126
    {
127
        $ds = DIRECTORY_SEPARATOR;
128
        $msg = null;
129
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
130
        $NewEntity->setName("simpleEntityType");
131
        $this->assertTrue($NewEntity->isOK($msg), $msg);
132
        $this->assertNull($msg);
133
        $edmx = new Edmx();
134
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
135
        $this->assertTrue($edmx->isOK($msg), $msg);
136
        $this->assertNull($msg);
137
138
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
139
    }
140
141 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...
142
    {
143
        $this->markTestSkipped('Skipped until service-document models get implemented');
144
        $ds = DIRECTORY_SEPARATOR;
145
        $msg = null;
146
147
        $docLocation = dirname(__DIR__) . $ds . "tests" . $ds . "exampleV3ServiceDocument.xml";
148
        $document = file_get_contents($docLocation);
149
        $type = 'AlgoWeb\ODataMetadata\MetadataV3\edmx\TDataServicesType';
150
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
151
152
        $serializer =
153
            \JMS\Serializer\SerializerBuilder::create()
154
                ->addMetadataDir($ymlDir)
155
                ->build();
156
157
        $d = $serializer->deserialize($document, $type, 'xml');
158
        $this->assertTrue($d instanceof TDataServicesType, get_class($this));
159
        $this->assertTrue($d->isOK($msg), $msg);
160
    }
161
162 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...
163
    {
164
        $ds = DIRECTORY_SEPARATOR;
165
        $msg = null;
166
167
        $docLocation = dirname(__DIR__) . $ds . "tests" . $ds . "exampleV3ServiceMetadata.xml";
168
        $document = file_get_contents($docLocation);
169
        $type = 'AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx';
170
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
171
172
        $serializer =
173
            \JMS\Serializer\SerializerBuilder::create()
174
                ->addMetadataDir($ymlDir)
175
                ->build();
176
177
        $d = $serializer->deserialize($document, $type, 'xml');
178
        $this->assertTrue($d instanceof TEdmxType, get_class($d));
179
        $this->assertTrue($d->isOK($msg), $msg);
180
    }
181
182
    public function testKnownGoodV3MetadataDeserialiseToOkSerializeDeserializeRoundTrip()
183
    {
184
        $this->markTestSkipped();
185
        $ds = DIRECTORY_SEPARATOR;
186
        $msg = null;
187
188
        $docLocation = dirname(__DIR__) . $ds . "tests" . $ds . "exampleV3ServiceMetadata.xml";
189
        $document = file_get_contents($docLocation);
190
        $this->v3MetadataAgainstXSD($document);
191
        $type = 'AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx';
192
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
193
194
        $serializer =
195
            \JMS\Serializer\SerializerBuilder::create()
196
                ->addMetadataDir($ymlDir)
197
                ->build();
198
199
        $d = $serializer->deserialize($document, $type, 'xml');
200
        $this->assertTrue($d instanceof TEdmxType, get_class($d));
201
        $this->assertTrue($d->isOK($msg), $msg);
202
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $d, $msg);
203
    }
204
205
    public function testWithSingleEntityWithPropertiesSerializeOk()
206
    {
207
        $ds = DIRECTORY_SEPARATOR;
208
        $msg = null;
209
        $NewProperty = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType();
210
        $NewProperty->setName("TheFirstProperty");
211
        $NewProperty->setType("String");
212
        $this->assertTrue($NewProperty->isOK($msg), $msg);
213
        $this->assertNull($msg);
214
215
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
216
        $NewEntity->setName("simpleEntityType");
217
        $NewEntity->addToProperty($NewProperty);
218
        $this->assertTrue($NewEntity->isOK($msg), $msg);
219
        $this->assertNull($msg);
220
221
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
222
        $entitySet->setName(Str::plural($NewEntity->getName(), 2));
223
        $entitySet->setEntityType($NewEntity->getName());
224
        $this->assertTrue($entitySet->isOK($msg), $msg);
225
        $this->assertNull($msg);
226
        $edmx = new Edmx();
227
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
228
        $edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
229
        $this->assertTrue($edmx->isOK($msg), $msg);
230
        $this->assertNull($msg);
231
232
233
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
234
        $serializer =
235
            \JMS\Serializer\SerializerBuilder::create()
236
                ->addMetadataDir($ymlDir)
237
                ->build();
238
        $d = $serializer->serialize($edmx, "xml");
239
        $this->v3MetadataAgainstXSD($d);
240
    }
241
242
    public function testWithSingleEntityWithPropertiesSerializeDeserializeRoundTrip()
243
    {
244
        $ds = DIRECTORY_SEPARATOR;
245
        $msg = null;
246
        $NewProperty = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType();
247
        $NewProperty->setName("TheFirstProperty");
248
        $NewProperty->setType("String");
249
        $this->assertTrue($NewProperty->isOK($msg), $msg);
250
        $this->assertNull($msg);
251
252
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
253
        $NewEntity->setName("simpleEntityType");
254
        $NewEntity->addToProperty($NewProperty);
255
        $this->assertTrue($NewEntity->isOK($msg), $msg);
256
        $this->assertNull($msg);
257
258
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
259
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
0 ignored issues
show
Bug introduced by
The method pluralize() does not seem to exist on object<AlgoWeb\ODataMetadata\Tests\EdmxTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
260
        $entitySet->setEntityType($NewEntity->getname());
261
        $this->assertTrue($entitySet->isOK($msg), $msg);
262
        $this->assertNull($msg);
263
        $edmx = new Edmx();
264
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
265
        $edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
266
        $this->assertTrue($edmx->isOK($msg), $msg);
267
        $this->assertNull($msg);
268
269
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
270
    }
271
272
    public function testWithSingleEntityWithoutPropertiesSerializeDeserializeRoundTrip()
273
    {
274
        $ds = DIRECTORY_SEPARATOR;
275
        $msg = null;
276
277
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
278
        $NewEntity->setName("simpleEntityType");
279
        $this->assertTrue($NewEntity->isOK($msg), $msg);
280
        $this->assertNull($msg);
281
282
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
283
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
0 ignored issues
show
Bug introduced by
The method pluralize() does not seem to exist on object<AlgoWeb\ODataMetadata\Tests\EdmxTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
284
        $entitySet->setEntityType($NewEntity->getname());
285
        $this->assertTrue($entitySet->isOK($msg), $msg);
286
        $this->assertNull($msg);
287
        $edmx = new Edmx();
288
        $edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
289
        $edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
290
        $this->assertTrue($edmx->isOK($msg), $msg);
291
        $this->assertNull($msg);
292
293
        $this->checkEdmxSerialiseDeserialiseRoundTrip($ds, $edmx, $msg);
294
    }
295
}
296