Passed
Push — master ( 59e1a5...f37dc8 )
by Christopher
05:53
created

TOperandTypeTest   D

Complexity

Total Complexity 61

Size/Duplication

Total Lines 567
Duplicated Lines 59.26 %

Coupling/Cohesion

Components 2
Dependencies 16

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 61
lcom 2
cbo 16
dl 336
loc 567
rs 4.054
c 3
b 0
f 0

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TOperandTypeTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TOperandTypeTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace AlgoWeb\ODataMetadata\Tests\v3\edm;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAnonymousFunctionExpressionType;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\TApplyExpressionType;
7
use AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionExpressionType;
8
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntitySetReferenceExpressionType;
9
use AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReferenceExpressionType;
10
use AlgoWeb\ODataMetadata\MetadataV3\edm\TIfExpressionType;
11
use AlgoWeb\ODataMetadata\MetadataV3\edm\TOperandType;
12
use AlgoWeb\ODataMetadata\MetadataV3\edm\TParameterReferenceExpressionType;
13
use AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyReferenceExpressionType;
14
use AlgoWeb\ODataMetadata\MetadataV3\edm\TRecordExpressionType;
15
use AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeAssertExpressionType;
16
use AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeTestExpressionType;
17
use AlgoWeb\ODataMetadata\MetadataV3\edm\TValueTermReferenceExpressionType;
18
use AlgoWeb\ODataMetadata\Tests\TestCase;
19
use Mockery as m;
20
21
class TOperandTypeTest extends TestCase
22
{
23
    public function testSetStringNullString()
24
    {
25
        $foo = new TOperandType();
26
        $foo->setString(null);
27
        $this->assertEquals(null, $foo->getString());
28
    }
29
30
    public function testSetStringActualString()
31
    {
32
        $foo = new TOperandType();
33
        $foo->setString('string');
34
        $this->assertEquals('string', $foo->getString());
35
    }
36
37
    public function testSetStringEmptyArrayAsString()
38
    {
39
        $expected = 'String must be a string';
40
        $actual = null;
41
42
        $foo = new TOperandType();
43
        try {
44
            $foo->setString([]);
45
        } catch (\InvalidArgumentException $e) {
46
            $actual = $e->getMessage();
47
        }
48
        $this->assertEquals($expected, $actual);
49
    }
50
51
    public function testSetStringNonEmptyArrayAsString()
52
    {
53
        $expected = 'String must be a string';
54
        $actual = null;
55
56
        $foo = new TOperandType();
57
        try {
58
            $foo->setString(['a']);
59
        } catch (\InvalidArgumentException $e) {
60
            $actual = $e->getMessage();
61
        }
62
        $this->assertEquals($expected, $actual);
63
    }
64
65
    public function testSetStringObjectAsString()
66
    {
67
        $expected = 'String must be a string';
68
        $actual = null;
69
70
        $foo = new TOperandType();
71
        try {
72
            $foo->setString(new \DateTime());
73
        } catch (\InvalidArgumentException $e) {
74
            $actual = $e->getMessage();
75
        }
76
        $this->assertEquals($expected, $actual);
77
    }
78
79
    public function testSetGetBinaryRoundTrip()
80
    {
81
        $expected = 'zyxvut';
82
        $foo = new TOperandType();
83
        $foo->setBinary('zyxvut');
84
        $this->assertEquals($expected, $foo->getBinary());
85
    }
86
87
    public function testSetGetIntRoundTrip()
88
    {
89
        $expected = 'zyxvut';
90
        $foo = new TOperandType();
91
        $foo->setInt('zyxvut');
92
        $this->assertEquals($expected, $foo->getInt());
93
    }
94
95
    public function testSetGetFloatRoundTrip()
96
    {
97
        $expected = 'zyxvut';
98
        $foo = new TOperandType();
99
        $foo->setFloat('zyxvut');
100
        $this->assertEquals($expected, $foo->getFloat());
101
    }
102
103
    public function testSetGetDecimalRoundTrip()
104
    {
105
        $expected = 'zyxvut';
106
        $foo = new TOperandType();
107
        $foo->setDecimal('zyxvut');
108
        $this->assertEquals($expected, $foo->getDecimal());
109
    }
110
111
    public function testSetGetBoolRoundTrip()
112
    {
113
        $foo = new TOperandType();
114
        $foo->setBool(null);
115
        $this->assertFalse($foo->getBool());
116
    }
117
118
    public function testSetGetDateTimeRoundTrip()
119
    {
120
        $expected = new \DateTime();
121
        $foo = new TOperandType();
122
        $foo->setDateTime($expected);
123
        $this->assertEquals($expected, $foo->getDateTime());
124
    }
125
126
    public function testSetGetDateTimeOffsetRoundTrip()
127
    {
128
        $expected = new \DateTime();
129
        $foo = new TOperandType();
130
        $foo->setDateTimeOffset($expected);
131
        $this->assertEquals($expected, $foo->getDateTimeOffset());
132
    }
133
134
    public function testSetBadIf()
135
    {
136
        $expected = '';
137
        $actual = null;
138
139
        $if = m::mock(TIfExpressionType::class);
140
        $if->shouldReceive('isOK')->andReturn(false);
141
142
        $foo = new TOperandType();
143
144
        try {
145
            $foo->setIf($if);
146
        } catch (\InvalidArgumentException $e) {
147
            $actual = $e->getMessage();
148
        }
149
        $this->assertEquals($expected, $actual);
150
    }
151
152
    public function testSetGetIfRoundTrip()
153
    {
154
        $if = m::mock(TIfExpressionType::class);
155
        $if->shouldReceive('isOK')->andReturn(true);
156
157
        $foo = new TOperandType();
158
        $foo->setIf($if);
159
        $result = $foo->getIf();
160
        $this->assertTrue($result instanceof TIfExpressionType, get_class($result));
161
        $this->assertTrue($result->isOK());
162
    }
163
164
    public function testSetBadRecord()
165
    {
166
        $expected = '';
167
        $actual = null;
168
169
        $if = m::mock(TRecordExpressionType::class);
170
        $if->shouldReceive('isOK')->andReturn(false);
171
172
        $foo = new TOperandType();
173
174
        try {
175
            $foo->setRecord($if);
176
        } catch (\InvalidArgumentException $e) {
177
            $actual = $e->getMessage();
178
        }
179
        $this->assertEquals($expected, $actual);
180
    }
181
182
    public function testSetGetRecordRoundTrip()
183
    {
184
        $if = m::mock(TRecordExpressionType::class);
185
        $if->shouldReceive('isOK')->andReturn(true);
186
187
        $foo = new TOperandType();
188
        $foo->setRecord($if);
189
        $result = $foo->getRecord();
190
        $this->assertTrue($result instanceof TRecordExpressionType, get_class($result));
191
        $this->assertTrue($result->isOK());
192
    }
193
194
    public function testSetBadCollection()
195
    {
196
        $expected = '';
197
        $actual = null;
198
199
        $if = m::mock(TCollectionExpressionType::class);
200
        $if->shouldReceive('isOK')->andReturn(false);
201
202
        $foo = new TOperandType();
203
204
        try {
205
            $foo->setCollection($if);
206
        } catch (\InvalidArgumentException $e) {
207
            $actual = $e->getMessage();
208
        }
209
        $this->assertEquals($expected, $actual);
210
    }
211
212
    public function testSetGetCollectionRoundTrip()
213
    {
214
        $if = m::mock(TCollectionExpressionType::class);
215
        $if->shouldReceive('isOK')->andReturn(true);
216
217
        $foo = new TOperandType();
218
        $foo->setCollection($if);
219
        $result = $foo->getCollection();
220
        $this->assertTrue($result instanceof TCollectionExpressionType, get_class($result));
221
        $this->assertTrue($result->isOK());
222
    }
223
224
    public function testSetBadTypeAssert()
225
    {
226
        $expected = '';
227
        $actual = null;
228
229
        $if = m::mock(TTypeAssertExpressionType::class);
230
        $if->shouldReceive('isOK')->andReturn(false);
231
232
        $foo = new TOperandType();
233
234
        try {
235
            $foo->setTypeAssert($if);
236
        } catch (\InvalidArgumentException $e) {
237
            $actual = $e->getMessage();
238
        }
239
        $this->assertEquals($expected, $actual);
240
    }
241
242
    public function testSetGetTypeAssertRoundTrip()
243
    {
244
        $if = m::mock(TTypeAssertExpressionType::class);
245
        $if->shouldReceive('isOK')->andReturn(true);
246
247
        $foo = new TOperandType();
248
        $foo->setTypeAssert($if);
249
        $result = $foo->getTypeAssert();
250
        $this->assertTrue($result instanceof TTypeAssertExpressionType, get_class($result));
251
        $this->assertTrue($result->isOK());
252
    }
253
254
    public function testSetBadTypeTest()
255
    {
256
        $expected = '';
257
        $actual = null;
258
259
        $if = m::mock(TTypeTestExpressionType::class);
260
        $if->shouldReceive('isOK')->andReturn(false);
261
262
        $foo = new TOperandType();
263
264
        try {
265
            $foo->setTypeTest($if);
266
        } catch (\InvalidArgumentException $e) {
267
            $actual = $e->getMessage();
268
        }
269
        $this->assertEquals($expected, $actual);
270
    }
271
272
    public function testSetGetTypeTestRoundTrip()
273
    {
274
        $if = m::mock(TTypeTestExpressionType::class);
275
        $if->shouldReceive('isOK')->andReturn(true);
276
277
        $foo = new TOperandType();
278
        $foo->setTypeTest($if);
279
        $result = $foo->getTypeTest();
280
        $this->assertTrue($result instanceof TTypeTestExpressionType, get_class($result));
281
        $this->assertTrue($result->isOK());
282
    }
283
284
    public function testSetBadFunctionReference()
285
    {
286
        $expected = '';
287
        $actual = null;
288
289
        $if = m::mock(TFunctionReferenceExpressionType::class);
290
        $if->shouldReceive('isOK')->andReturn(false);
291
292
        $foo = new TOperandType();
293
294
        try {
295
            $foo->setFunctionReference($if);
296
        } catch (\InvalidArgumentException $e) {
297
            $actual = $e->getMessage();
298
        }
299
        $this->assertEquals($expected, $actual);
300
    }
301
302
    public function testSetGetFunctionReferenceRoundTrip()
303
    {
304
        $if = m::mock(TFunctionReferenceExpressionType::class);
305
        $if->shouldReceive('isOK')->andReturn(true);
306
307
        $foo = new TOperandType();
308
        $foo->setFunctionReference($if);
309
        $result = $foo->getFunctionReference();
310
        $this->assertTrue($result instanceof TFunctionReferenceExpressionType, get_class($result));
311
        $this->assertTrue($result->isOK());
312
    }
313
314
    public function testSetBadEntitySetReference()
315
    {
316
        $expected = '';
317
        $actual = null;
318
319
        $if = m::mock(TEntitySetReferenceExpressionType::class);
320
        $if->shouldReceive('isOK')->andReturn(false);
321
322
        $foo = new TOperandType();
323
324
        try {
325
            $foo->setEntitySetReference($if);
326
        } catch (\InvalidArgumentException $e) {
327
            $actual = $e->getMessage();
328
        }
329
        $this->assertEquals($expected, $actual);
330
    }
331
332
    public function testSetGetEntitySetReferenceRoundTrip()
333
    {
334
        $if = m::mock(TEntitySetReferenceExpressionType::class);
335
        $if->shouldReceive('isOK')->andReturn(true);
336
337
        $foo = new TOperandType();
338
        $foo->setEntitySetReference($if);
339
        $result = $foo->getEntitySetReference();
340
        $this->assertTrue($result instanceof TEntitySetReferenceExpressionType, get_class($result));
341
        $this->assertTrue($result->isOK());
342
    }
343
344
    public function testSetBadAnonymousFunction()
345
    {
346
        $expected = '';
347
        $actual = null;
348
349
        $if = m::mock(TAnonymousFunctionExpressionType::class);
350
        $if->shouldReceive('isOK')->andReturn(false);
351
352
        $foo = new TOperandType();
353
354
        try {
355
            $foo->setAnonymousFunction($if);
356
        } catch (\InvalidArgumentException $e) {
357
            $actual = $e->getMessage();
358
        }
359
        $this->assertEquals($expected, $actual);
360
    }
361
362
    public function testSetGetAnonymousFunctionRoundTrip()
363
    {
364
        $if = m::mock(TAnonymousFunctionExpressionType::class);
365
        $if->shouldReceive('isOK')->andReturn(true);
366
367
        $foo = new TOperandType();
368
        $foo->setAnonymousFunction($if);
369
        $result = $foo->getAnonymousFunction();
370
        $this->assertTrue($result instanceof TAnonymousFunctionExpressionType, get_class($result));
371
        $this->assertTrue($result->isOK());
372
    }
373
374
    public function testSetBadParameterReference()
375
    {
376
        $expected = '';
377
        $actual = null;
378
379
        $if = m::mock(TParameterReferenceExpressionType::class);
380
        $if->shouldReceive('isOK')->andReturn(false);
381
382
        $foo = new TOperandType();
383
384
        try {
385
            $foo->setParameterReference($if);
386
        } catch (\InvalidArgumentException $e) {
387
            $actual = $e->getMessage();
388
        }
389
        $this->assertEquals($expected, $actual);
390
    }
391
392
    public function testSetGetParameterReferenceRoundTrip()
393
    {
394
        $if = m::mock(TParameterReferenceExpressionType::class);
395
        $if->shouldReceive('isOK')->andReturn(true);
396
397
        $foo = new TOperandType();
398
        $foo->setParameterReference($if);
399
        $result = $foo->getParameterReference();
400
        $this->assertTrue($result instanceof TParameterReferenceExpressionType, get_class($result));
401
        $this->assertTrue($result->isOK());
402
    }
403
404
    public function testSetBadApply()
405
    {
406
        $expected = '';
407
        $actual = null;
408
409
        $if = m::mock(TApplyExpressionType::class);
410
        $if->shouldReceive('isOK')->andReturn(false);
411
412
        $foo = new TOperandType();
413
414
        try {
415
            $foo->setApply($if);
416
        } catch (\InvalidArgumentException $e) {
417
            $actual = $e->getMessage();
418
        }
419
        $this->assertEquals($expected, $actual);
420
    }
421
422
    public function testSetGetApplyRoundTrip()
423
    {
424
        $if = m::mock(TApplyExpressionType::class);
425
        $if->shouldReceive('isOK')->andReturn(true);
426
427
        $foo = new TOperandType();
428
        $foo->setApply($if);
429
        $result = $foo->getApply();
430
        $this->assertTrue($result instanceof TApplyExpressionType, get_class($result));
431
        $this->assertTrue($result->isOK());
432
    }
433
434
    public function testSetBadPropertyReference()
435
    {
436
        $expected = '';
437
        $actual = null;
438
439
        $if = m::mock(TPropertyReferenceExpressionType::class);
440
        $if->shouldReceive('isOK')->andReturn(false);
441
442
        $foo = new TOperandType();
443
444
        try {
445
            $foo->setPropertyReference($if);
446
        } catch (\InvalidArgumentException $e) {
447
            $actual = $e->getMessage();
448
        }
449
        $this->assertEquals($expected, $actual);
450
    }
451
452
    public function testSetGetPropertyReferenceRoundTrip()
453
    {
454
        $if = m::mock(TPropertyReferenceExpressionType::class);
455
        $if->shouldReceive('isOK')->andReturn(true);
456
457
        $foo = new TOperandType();
458
        $foo->setPropertyReference($if);
459
        $result = $foo->getPropertyReference();
460
        $this->assertTrue($result instanceof TPropertyReferenceExpressionType, get_class($result));
461
        $this->assertTrue($result->isOK());
462
    }
463
464
    public function testSetBadValueTermReference()
465
    {
466
        $expected = '';
467
        $actual = null;
468
469
        $if = m::mock(TValueTermReferenceExpressionType::class);
470
        $if->shouldReceive('isOK')->andReturn(false);
471
472
        $foo = new TOperandType();
473
474
        try {
475
            $foo->setValueTermReference($if);
476
        } catch (\InvalidArgumentException $e) {
477
            $actual = $e->getMessage();
478
        }
479
        $this->assertEquals($expected, $actual);
480
    }
481
482
    public function testSetGetValueTermReferenceRoundTrip()
483
    {
484
        $if = m::mock(TValueTermReferenceExpressionType::class);
485
        $if->shouldReceive('isOK')->andReturn(true);
486
487
        $foo = new TOperandType();
488
        $foo->setValueTermReference($if);
489
        $result = $foo->getValueTermReference();
490
        $this->assertTrue($result instanceof TValueTermReferenceExpressionType, get_class($result));
491
        $this->assertTrue($result->isOK());
492
    }
493
494
    public function testGExpressionNotValidWhenMoreThanOneBasicExpressionSet()
495
    {
496
        $expected = '2 fields not null.  Need maximum of 1: AlgoWeb\ODataMetadata\MetadataV3\edm\TOperandType';
497
        $actual = null;
498
499
        $if = m::mock(TValueTermReferenceExpressionType::class);
500
        $if->shouldReceive('isOK')->andReturn(true);
501
502
        $apply = m::mock(TApplyExpressionType::class);
503
        $apply->shouldReceive('isOK')->andReturn(true);
504
505
        $foo = new TOperandType();
506
        $foo->setValueTermReference($if);
507
        $foo->setApply($apply);
508
509
        $this->assertFalse($foo->isGExpressionValid($actual));
510
        $this->assertEquals($expected, $actual);
511
    }
512
513
    public function testSetBadEnum()
514
    {
515
        $expected = 'Enum must be a valid TQualifiedName';
516
        $actual = null;
517
518
        $foo = new TOperandType();
519
        try {
520
            $foo->setEnum(' _ ');
521
        } catch (\InvalidArgumentException $e) {
522
            $actual = $e->getMessage();
523
        }
524
        $this->assertEquals($expected, $actual);
525
    }
526
527
    public function testSetGetEnumRoundTrip()
528
    {
529
        $expected = 'Org.OData.Publication.V1.DocumentationUrl';
530
531
        $foo = new TOperandType();
532
        $foo->setEnum($expected);
533
        $actual = $foo->getEnum();
534
535
        $this->assertEquals($expected, $actual);
536
    }
537
538
    public function testSetBadPath()
539
    {
540
        $expected = 'Path must be a valid TQualifiedName';
541
        $actual = null;
542
543
        $foo = new TOperandType();
544
        try {
545
            $foo->setPath(' _ ');
546
        } catch (\InvalidArgumentException $e) {
547
            $actual = $e->getMessage();
548
        }
549
        $this->assertEquals($expected, $actual);
550
    }
551
552
    public function testSetGetPathRoundTrip()
553
    {
554
        $expected = 'Org.OData.Publication.V1.DocumentationUrl';
555
556
        $foo = new TOperandType();
557
        $foo->setPath($expected);
558
        $actual = $foo->getPath();
559
560
        $this->assertEquals($expected, $actual);
561
    }
562
563
    public function testSetBadGuid()
564
    {
565
        $expected = 'Guid must be a valid TGuidLiteral';
566
        $actual = null;
567
568
        $foo = new TOperandType();
569
        try {
570
            $foo->setGuid(' _ ');
571
        } catch (\InvalidArgumentException $e) {
572
            $actual = $e->getMessage();
573
        }
574
        $this->assertEquals($expected, $actual);
575
    }
576
577
    public function testSetGetGuidRoundTrip()
578
    {
579
        $expected = '00000000-0000-0000-0000-000000000000';
580
        $actual = null;
581
582
        $foo = new TOperandType();
583
        $foo->setGuid($expected);
584
        $actual = $foo->getGuid();
585
        $this->assertEquals($expected, $actual);
586
    }
587
}
588