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

TestTypeTest   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 374
Duplicated Lines 32.09 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 40
lcom 1
cbo 5
dl 120
loc 374
rs 8.2608
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 TestTypeTest 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 TestTypeTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace AlgoWeb\ODataMetadata\Tests;
4
5
use \Mockery as m;
6
7
class TestTypeTest extends TestCase
8
{
9
    public function testIsStringNotNullOrEmptyWithNull()
10
    {
11
        $string = null;
12
        $foo = new testType();
13
        $this->assertFalse($foo->isStringNotNullOrEmpty($string));
14
    }
15
16
    public function testIsStringNotNullOrEmptyWithEmpty()
17
    {
18
        $string = '';
19
        $foo = new testType();
20
        $this->assertFalse($foo->isStringNotNullOrEmpty($string));
21
    }
22
23
    public function testIsStringNotNullOrEmptyWithNonString()
24
    {
25
        $string = new \stdClass();
26
        $foo = new testType();
27
        $this->assertFalse($foo->isStringNotNullOrEmpty($string));
28
    }
29
30
    public function testIsStringNotNullOrEmptyWithObject()
31
    {
32
        $string = new \stdClass();
33
        $foo = new testType();
34
        $this->assertFalse($foo->isStringNotNullOrEmpty($string));
35
    }
36
37
    public function testIsStringNotNullOrEmptyWithNumber()
38
    {
39
        $string = 2134;
40
        $foo = new testType();
41
        $this->assertFalse($foo->isStringNotNullOrEmpty($string));
42
    }
43
44
    public function testIsStringNotNullOrEmptyWithString()
45
    {
46
        $string = 'An actual string';
47
        $foo = new testType();
48
        $this->assertTrue($foo->isStringNotNullOrEmpty($string));
49
    }
50
51
    public function testIsStringNotNullOrEmptyWithNumberAsString()
52
    {
53
        $string = '1234';
54
        $foo = new testType();
55
        $this->assertTrue($foo->isStringNotNullOrEmpty($string));
56
    }
57
58
    public function testIsNotNullInstanceOfWhenPassedObjectToCheck()
59
    {
60
        $var = new \stdClass();
61
        $instanceof = new \stdClass();
62
63
        $foo = new testType();
64
        $this->assertTrue($foo->isNotNullInstanceOf($var, $instanceof));
65
    }
66
67
    public function testIsNotNullInstanceOfWhenPassedStringToCheck()
68
    {
69
        $var = new \stdClass();
70
        $instanceof = get_class($var);
71
72
        $foo = new testType();
73
        $this->assertTrue($foo->isNotNullInstanceOf($var, $instanceof));
74
    }
75
76
    public function testIsNotNullInstanceOfWhenPassedNonObjectToCheck()
77
    {
78
        $var = 'Another string.  How amazing.';
79
        $instanceof = new \stdClass();
80
81
        $foo = new testType();
82
        $this->assertFalse($foo->isNotNullInstanceOf($var, $instanceof));
83
    }
84
85
    public function testIsNotNullInstanceOfWhenPassedNullToCheck()
86
    {
87
        $var = null;
88
        $instanceof = new \stdClass();
89
90
        $foo = new testType();
91
        $this->assertFalse($foo->isNotNullInstanceOf($var, $instanceof));
92
    }
93
94
    public function testIsNullInstanceOfWhenPassedObjectToCheck()
95
    {
96
        $var = new \stdClass();
97
        $instanceof = new \stdClass();
98
99
        $foo = new testType();
100
        $this->assertFalse($foo->isNullInstanceOf($var, $instanceof));
101
    }
102
103
    public function testIsNullInstanceOfWhenPassedStringToCheck()
104
    {
105
        $var = new \stdClass();
106
        $instanceof = get_class($var);
107
108
        $foo = new testType();
109
        $this->assertFalse($foo->isNullInstanceOf($var, $instanceof));
110
    }
111
112
    public function testIsNullInstanceOfWhenPassedNonObjectToCheck()
113
    {
114
        $var = 'Another string.  How amazing.';
115
        $instanceof = new \stdClass();
116
117
        $foo = new testType();
118
        $this->assertFalse($foo->isNullInstanceOf($var, $instanceof));
119
    }
120
121
    public function testIsNullInstanceOfWhenPassedNullToCheck()
122
    {
123
        $var = null;
124
        $instanceof = new \stdClass();
125
126
        $foo = new testType();
127
        $this->assertTrue($foo->isNullInstanceOf($var, $instanceof));
128
    }
129
130
    public function testIsValidArrayNotEnoughBitz()
131
    {
132
        $arr = [];
133
        $instanceof = get_class(new \stdClass());
134
135
        $foo = new testType();
136
        $this->assertFalse($foo->isValidArray($arr, $instanceof, 1, -1));
137
    }
138
139
    public function testIsValidArrayTooManyBitz()
140
    {
141
        $arr = ['abc'];
142
        $instanceof = get_class(new \stdClass());
143
144
        $foo = new testType();
145
        $this->assertFalse($foo->isValidArray($arr, $instanceof, 0, 0));
146
    }
147
148
    public function testIsValidArrayWrongType()
149
    {
150
        $arr = ['abc'];
151
        $instanceof = get_class(new \stdClass());
152
153
        $foo = new testType();
154
        $this->assertFalse($foo->isValidArray($arr, $instanceof));
155
    }
156
157
    public function testIsValidArrayRightType()
158
    {
159
        $arr = [new \stdClass()];
160
        $instanceof = get_class(new \stdClass());
161
162
        $foo = new testType();
163
        $this->assertTrue($foo->isValidArray($arr, $instanceof));
164
    }
165
166
    public function testIsChildArrayOkForEmptyArray()
167
    {
168
        $msg = null;
169
        $arr = [];
170
171
        $foo = new testType();
172
        $this->assertTrue($foo->isChildArrayOK($arr, $msg), $msg);
173
    }
174
175
    public function testIsChildArrayOkForNonEmptyArrayWithBadGubbins()
176
    {
177
        $msg = null;
178
        $expected = 'Child item is not an instance of IsOK: AlgoWeb\\ODataMetadata\\Tests\\testType';
179
        $arr = ['abc'];
180
181
        $foo = new testType();
182
        $this->assertFalse($foo->isChildArrayOK($arr, $msg), $msg);
183
        $this->assertEquals($expected, $msg);
184
    }
185
186
    public function testIsChildArrayOkForNonEmptyArrayWithGoodGubbinsNotOk()
187
    {
188
        $bar = m::mock(testType::class);
189
        // closure needs to return true to match the matcher and thus trip the andReturn(false) bit
190
        $bar->shouldReceive('isOK')->with(m::on(function (&$msg) {
191
            $msg = 'OH NOES!';
192
            return true;
193
        }))->andReturn(false);
194
195
        $msg = null;
196
        $expected = 'OH NOES!';
197
        $arr = [ $bar ];
198
199
        $foo = new testType();
200
        $this->assertFalse($foo->isChildArrayOK($arr, $msg), $msg);
201
        $this->assertEquals($expected, $msg);
202
    }
203
204
    public function testIsUrlValidWithNull()
205
    {
206
        $url = null;
207
208
        $foo = new testType();
209
        $this->assertFalse($foo->isURLValid($url));
210
    }
211
212
    public function testIsUrlValidWithNonUrlString()
213
    {
214
        $url = 'abc';
215
216
        $foo = new testType();
217
        $this->assertFalse($foo->isURLValid($url));
218
    }
219
220
    public function testIsUrlValidWithUrlString()
221
    {
222
        $url = 'https://google.com';
223
224
        $foo = new testType();
225
        $this->assertTrue($foo->isURLValid($url));
226
    }
227
228
    public function testIsUrlValidWithUrlStringWithWWW()
229
    {
230
        $url = 'http://www.google.com';
231
232
        $foo = new testType();
233
        $this->assertTrue($foo->isURLValid($url));
234
    }
235
236
    public function testObjectNullOrOkWithNullObject()
237
    {
238
        $msg = null;
239
        $obj = null;
240
        $foo = new testType();
241
        $this->assertTrue($foo->isObjectNullOrOk($obj, $msg));
242
    }
243
244
    public function testObjectNullOrOkWithIsOkObjectActuallyOk()
245
    {
246
        $msg = null;
247
        $obj = m::mock(testType::class);
248
        // closure needs to return true to match the matcher and thus trip the andReturn(false) bit
249
        $obj->shouldReceive('isOK')->with(m::on(function (&$msg) {
250
            $msg = null;
251
            return true;
252
        }))->andReturn(true);
253
        $foo = new testType();
254
        $this->assertTrue($foo->isObjectNullOrOk($obj, $msg));
255
    }
256
257
    public function testObjectNullOrOkWithIsOkObjectNotOk()
258
    {
259
        $msg = null;
260
        $obj = m::mock(testType::class);
261
        // closure needs to return true to match the matcher and thus trip the andReturn(false) bit
262
        $obj->shouldReceive('isOK')->with(m::on(function (&$msg) {
263
            $msg = 'OH NOES!';
264
            return true;
265
        }))->andReturn(false);
266
        $expected = 'OH NOES!';
267
        $foo = new testType();
268
        $this->assertFalse($foo->isObjectNullOrOk($obj, $msg));
269
        $this->assertEquals($expected, $msg);
270
    }
271
272
    public function testIsValidArrayOkWhenNotValidArray()
273
    {
274
        $foo = m::mock(testType::class)->makePartial();
275
        $foo->shouldReceive('isValidArray')->withAnyArgs()->andReturn(false);
276
277
        $expected = 'Supplied array not a valid array: Mockery';
278
        $msg = null;
279
        $this->assertFalse($foo->isValidArrayOk([], '', $msg));
280
        $msg = substr($msg, 0, strlen($expected));
281
        $this->assertEquals($expected, $msg);
282
    }
283
284
    public function testIsValidArrayOkWhenChildArrayNotOK()
285
    {
286
        $foo = m::mock(testType::class)->makePartial();
287
        $foo->shouldReceive('isValidArray')->withAnyArgs()->andReturn(true);
288
        $foo->shouldReceive('isChildArrayOK')->with(m::any(), m::on(function (&$msg) {
289
            $msg = 'OH NOES!';
290
            return true;
291
        }))->andReturn(false);
292
        $expected = 'OH NOES!';
293
        $msg = null;
294
        $this->assertFalse($foo->isValidArrayOK([], '', $msg));
295
        $this->assertEquals($expected, $msg);
296
    }
297
298
    public function testIsValidArrayOkWhenChildArrayIsOk()
299
    {
300
        $foo = m::mock(testType::class)->makePartial();
301
        $foo->shouldReceive('isValidArray')->withAnyArgs()->andReturn(true);
302
        $foo->shouldReceive('isChildArrayOK')->with(m::any(), m::on(function (&$msg) {
303
            $msg = null;
304
            return true;
305
        }))->andReturn(true);
306
        $expected = null;
307
        $msg = null;
308
        $this->assertTrue($foo->isValidArrayOK([], '', $msg));
309
        $this->assertEquals($expected, $msg);
310
    }
311
312
    public function testReplaceStringStartingWithSpaces()
313
    {
314
        $string = 'This is a string';
315
        $expected = 'This is a string';
316
        $foo = new testType();
317
        $result = $foo->replaceString($string);
318
        $this->assertEquals($expected, $result);
319
    }
320
321
    public function testReplaceStringStartingWithTabs()
322
    {
323
        $string = "This\tis\ta\tstring";
324
        $expected = 'This is a string';
325
        $foo = new testType();
326
        $result = $foo->replaceString($string);
327
        $this->assertEquals($expected, $result);
328
    }
329
330
    public function testReplaceStringStartingWithNuLines()
331
    {
332
        $string = "This\nis\na\nstring";
333
        $expected = 'This is a string';
334
        $foo = new testType();
335
        $result = $foo->replaceString($string);
336
        $this->assertEquals($expected, $result);
337
    }
338
339
    public function testReplaceStringWithPadding()
340
    {
341
        $string = ' This is a string ';
342
        $expected = ' This is a string ';
343
        $foo = new testType();
344
        $result = $foo->replaceString($string);
345
        $this->assertEquals($expected, $result);
346
    }
347
348
    public function testCollapseStringWithSpacesAndPadding()
349
    {
350
        $string = '  This  is  a  string  ';
351
        $expected = 'This is a string';
352
        $foo = new testType();
353
        $result = $foo->collapseString($string);
354
        $this->assertEquals($expected, $result);
355
    }
356
357
    public function testSimpleIdentifierValidWithTrailingSpace()
358
    {
359
        $string = 'UnitPrice ';
360
361
        $foo = new testType();
362
        $this->assertFalse($foo->isTSimpleIdentifierValid($string));
363
    }
364
365
    public function testSimpleIdentifierValidWithoutTrailingSpace()
366
    {
367
        $string = 'UnitPrice';
368
369
        $foo = new testType();
370
        $this->assertTrue($foo->isTSimpleIdentifierValid($string));
371
    }
372
373
    public function testRegexPatternMatchAllOfString()
374
    {
375
        $string = 'This! IS! UNITPRICE';
376
377
        $foo = new testType();
378
        $this->assertFalse($foo->isTSimpleIdentifierValid($string));
379
    }
380
}
381