Completed
Push — master ( 95acd7...e70d94 )
by Naveen
13:02 queued 11:07
created

Framework_TestCaseTest   D

Complexity

Total Complexity 43

Size/Duplication

Total Lines 523
Duplicated Lines 48.37 %

Coupling/Cohesion

Components 2
Dependencies 22
Metric Value
wmc 43
lcom 2
cbo 22
dl 253
loc 523
rs 4.71

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 Framework_TestCaseTest 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 Framework_TestCaseTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'NoArgTestCaseTest.php';
12
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Singleton.php';
13
14
$GLOBALS['a']  = 'a';
15
$_ENV['b']     = 'b';
16
$_POST['c']    = 'c';
17
$_GET['d']     = 'd';
18
$_COOKIE['e']  = 'e';
19
$_SERVER['f']  = 'f';
20
$_FILES['g']   = 'g';
21
$_REQUEST['h'] = 'h';
22
$GLOBALS['i']  = 'i';
23
24
/**
25
 * @since      Class available since Release 2.0.0
26
 * @covers     PHPUnit_Framework_TestCase
27
 */
28
class Framework_TestCaseTest extends PHPUnit_Framework_TestCase
29
{
30
    protected $backupGlobalsBlacklist = array('i', 'singleton');
31
32
    /**
33
     * Used be testStaticAttributesBackupPre
34
     */
35
    protected static $_testStatic = 0;
36
37
    public function testCaseToString()
38
    {
39
        $this->assertEquals(
40
            'Framework_TestCaseTest::testCaseToString',
41
            $this->toString()
42
        );
43
    }
44
45
    public function testSuccess()
46
    {
47
        $test   = new Success;
48
        $result = $test->run();
49
50
        $this->assertEquals(PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $test->getStatus());
51
        $this->assertEquals(0, $result->errorCount());
52
        $this->assertEquals(0, $result->failureCount());
53
        $this->assertEquals(0, $result->skippedCount());
54
        $this->assertEquals(1, count($result));
55
    }
56
57
    public function testFailure()
58
    {
59
        $test   = new Failure;
60
        $result = $test->run();
61
62
        $this->assertEquals(PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE, $test->getStatus());
63
        $this->assertEquals(0, $result->errorCount());
64
        $this->assertEquals(1, $result->failureCount());
65
        $this->assertEquals(0, $result->skippedCount());
66
        $this->assertEquals(1, count($result));
67
    }
68
69
    public function testError()
70
    {
71
        $test   = new TestError;
72
        $result = $test->run();
73
74
        $this->assertEquals(PHPUnit_Runner_BaseTestRunner::STATUS_ERROR, $test->getStatus());
75
        $this->assertEquals(1, $result->errorCount());
76
        $this->assertEquals(0, $result->failureCount());
77
        $this->assertEquals(0, $result->skippedCount());
78
        $this->assertEquals(1, count($result));
79
    }
80
81
    public function testSkipped()
82
    {
83
        $test   = new TestSkipped();
84
        $result = $test->run();
85
86
        $this->assertEquals(PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED, $test->getStatus());
87
        $this->assertEquals('Skipped test', $test->getStatusMessage());
88
        $this->assertEquals(0, $result->errorCount());
89
        $this->assertEquals(0, $result->failureCount());
90
        $this->assertEquals(1, $result->skippedCount());
91
        $this->assertEquals(1, count($result));
92
    }
93
94
    public function testIncomplete()
95
    {
96
        $test   = new TestIncomplete();
97
        $result = $test->run();
98
99
        $this->assertEquals(PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE, $test->getStatus());
100
        $this->assertEquals('Incomplete test', $test->getStatusMessage());
101
        $this->assertEquals(0, $result->errorCount());
102
        $this->assertEquals(0, $result->failureCount());
103
        $this->assertEquals(0, $result->skippedCount());
104
        $this->assertEquals(1, count($result));
105
    }
106
107
    public function testExceptionInSetUp()
108
    {
109
        $test   = new ExceptionInSetUpTest('testSomething');
110
        $result = $test->run();
111
112
        $this->assertTrue($test->setUp);
113
        $this->assertFalse($test->assertPreConditions);
114
        $this->assertFalse($test->testSomething);
115
        $this->assertFalse($test->assertPostConditions);
116
        $this->assertTrue($test->tearDown);
117
    }
118
119
    public function testExceptionInAssertPreConditions()
120
    {
121
        $test   = new ExceptionInAssertPreConditionsTest('testSomething');
122
        $result = $test->run();
123
124
        $this->assertTrue($test->setUp);
125
        $this->assertTrue($test->assertPreConditions);
126
        $this->assertFalse($test->testSomething);
127
        $this->assertFalse($test->assertPostConditions);
128
        $this->assertTrue($test->tearDown);
129
    }
130
131
    public function testExceptionInTest()
132
    {
133
        $test   = new ExceptionInTest('testSomething');
134
        $result = $test->run();
135
136
        $this->assertTrue($test->setUp);
137
        $this->assertTrue($test->assertPreConditions);
138
        $this->assertTrue($test->testSomething);
139
        $this->assertFalse($test->assertPostConditions);
140
        $this->assertTrue($test->tearDown);
141
    }
142
143
    public function testExceptionInAssertPostConditions()
144
    {
145
        $test   = new ExceptionInAssertPostConditionsTest('testSomething');
146
        $result = $test->run();
147
148
        $this->assertTrue($test->setUp);
149
        $this->assertTrue($test->assertPreConditions);
150
        $this->assertTrue($test->testSomething);
151
        $this->assertTrue($test->assertPostConditions);
152
        $this->assertTrue($test->tearDown);
153
    }
154
155
    public function testExceptionInTearDown()
156
    {
157
        $test   = new ExceptionInTearDownTest('testSomething');
158
        $result = $test->run();
159
160
        $this->assertTrue($test->setUp);
161
        $this->assertTrue($test->assertPreConditions);
162
        $this->assertTrue($test->testSomething);
163
        $this->assertTrue($test->assertPostConditions);
164
        $this->assertTrue($test->tearDown);
165
    }
166
167
    public function testNoArgTestCasePasses()
168
    {
169
        $result = new PHPUnit_Framework_TestResult;
170
        $t      = new PHPUnit_Framework_TestSuite('NoArgTestCaseTest');
171
172
        $t->run($result);
173
174
        $this->assertEquals(1, count($result));
175
        $this->assertEquals(0, $result->failureCount());
176
        $this->assertEquals(0, $result->errorCount());
177
    }
178
179
    public function testWasRun()
180
    {
181
        $test = new WasRun;
182
        $test->run();
183
184
        $this->assertTrue($test->wasRun);
185
    }
186
187
    public function testException()
188
    {
189
        $test = new ThrowExceptionTestCase('test');
190
        $test->setExpectedException('RuntimeException');
191
192
        $result = $test->run();
193
194
        $this->assertEquals(1, count($result));
195
        $this->assertTrue($result->wasSuccessful());
196
    }
197
198
    public function testExceptionWithMessage()
199
    {
200
        $test = new ThrowExceptionTestCase('test');
201
        $test->setExpectedException('RuntimeException', 'A runtime error occurred');
202
203
        $result = $test->run();
204
205
        $this->assertEquals(1, count($result));
206
        $this->assertTrue($result->wasSuccessful());
207
    }
208
209
    public function testExceptionWithWrongMessage()
210
    {
211
        $test = new ThrowExceptionTestCase('test');
212
        $test->setExpectedException('RuntimeException', 'A logic error occurred');
213
214
        $result = $test->run();
215
216
        $this->assertEquals(1, $result->failureCount());
217
        $this->assertEquals(1, count($result));
218
        $this->assertEquals(
219
            "Failed asserting that exception message 'A runtime error occurred' contains 'A logic error occurred'.",
220
            $test->getStatusMessage()
221
        );
222
    }
223
224
    public function testExceptionWithRegexpMessage()
225
    {
226
        $test = new ThrowExceptionTestCase('test');
227
        $test->setExpectedExceptionRegExp('RuntimeException', '/runtime .*? occurred/');
228
229
        $result = $test->run();
230
231
        $this->assertEquals(1, count($result));
232
        $this->assertTrue($result->wasSuccessful());
233
    }
234
235
    public function testExceptionWithWrongRegexpMessage()
236
    {
237
        $test = new ThrowExceptionTestCase('test');
238
        $test->setExpectedExceptionRegExp('RuntimeException', '/logic .*? occurred/');
239
240
        $result = $test->run();
241
242
        $this->assertEquals(1, $result->failureCount());
243
        $this->assertEquals(1, count($result));
244
        $this->assertEquals(
245
            "Failed asserting that exception message 'A runtime error occurred' matches '/logic .*? occurred/'.",
246
            $test->getStatusMessage()
247
        );
248
    }
249
250
    /**
251
     * @covers PHPUnit_Framework_Constraint_ExceptionMessageRegExp
252
     */
253
    public function testExceptionWithInvalidRegexpMessage()
254
    {
255
        $test = new ThrowExceptionTestCase('test');
256
        $test->setExpectedExceptionRegExp('RuntimeException', '#runtime .*? occurred/'); // wrong delimiter
257
258
        $result = $test->run();
259
260
        $this->assertEquals(
261
            "Invalid expected exception message regex given: '#runtime .*? occurred/'",
262
            $test->getStatusMessage()
263
        );
264
    }
265
266
    public function testNoException()
267
    {
268
        $test = new ThrowNoExceptionTestCase('test');
269
        $test->setExpectedException('RuntimeException');
270
271
        $result = $test->run();
272
273
        $this->assertEquals(1, $result->failureCount());
274
        $this->assertEquals(1, count($result));
275
    }
276
277
    public function testWrongException()
278
    {
279
        $test = new ThrowExceptionTestCase('test');
280
        $test->setExpectedException('InvalidArgumentException');
281
282
        $result = $test->run();
283
284
        $this->assertEquals(1, $result->failureCount());
285
        $this->assertEquals(1, count($result));
286
    }
287
288
    /**
289
     * @backupGlobals enabled
290
     */
291
    public function testGlobalsBackupPre()
292
    {
293
        global $a;
294
        global $i;
295
296
        $this->assertEquals('a', $a);
297
        $this->assertEquals('a', $GLOBALS['a']);
298
        $this->assertEquals('b', $_ENV['b']);
299
        $this->assertEquals('c', $_POST['c']);
300
        $this->assertEquals('d', $_GET['d']);
301
        $this->assertEquals('e', $_COOKIE['e']);
302
        $this->assertEquals('f', $_SERVER['f']);
303
        $this->assertEquals('g', $_FILES['g']);
304
        $this->assertEquals('h', $_REQUEST['h']);
305
        $this->assertEquals('i', $i);
306
        $this->assertEquals('i', $GLOBALS['i']);
307
308
        $GLOBALS['a']   = 'aa';
309
        $GLOBALS['foo'] = 'bar';
310
        $_ENV['b']      = 'bb';
311
        $_POST['c']     = 'cc';
312
        $_GET['d']      = 'dd';
313
        $_COOKIE['e']   = 'ee';
314
        $_SERVER['f']   = 'ff';
315
        $_FILES['g']    = 'gg';
316
        $_REQUEST['h']  = 'hh';
317
        $GLOBALS['i']   = 'ii';
318
319
        $this->assertEquals('aa', $a);
320
        $this->assertEquals('aa', $GLOBALS['a']);
321
        $this->assertEquals('bar', $GLOBALS['foo']);
322
        $this->assertEquals('bb', $_ENV['b']);
323
        $this->assertEquals('cc', $_POST['c']);
324
        $this->assertEquals('dd', $_GET['d']);
325
        $this->assertEquals('ee', $_COOKIE['e']);
326
        $this->assertEquals('ff', $_SERVER['f']);
327
        $this->assertEquals('gg', $_FILES['g']);
328
        $this->assertEquals('hh', $_REQUEST['h']);
329
        $this->assertEquals('ii', $i);
330
        $this->assertEquals('ii', $GLOBALS['i']);
331
    }
332
333
    public function testGlobalsBackupPost()
334
    {
335
        global $a;
336
        global $i;
337
338
        $this->assertEquals('a', $a);
339
        $this->assertEquals('a', $GLOBALS['a']);
340
        $this->assertEquals('b', $_ENV['b']);
341
        $this->assertEquals('c', $_POST['c']);
342
        $this->assertEquals('d', $_GET['d']);
343
        $this->assertEquals('e', $_COOKIE['e']);
344
        $this->assertEquals('f', $_SERVER['f']);
345
        $this->assertEquals('g', $_FILES['g']);
346
        $this->assertEquals('h', $_REQUEST['h']);
347
        $this->assertEquals('ii', $i);
348
        $this->assertEquals('ii', $GLOBALS['i']);
349
350
        $this->assertArrayNotHasKey('foo', $GLOBALS);
351
    }
352
353
    /**
354
     * @backupGlobals enabled
355
     * @backupStaticAttributes enabled
356
     */
357
    public function testStaticAttributesBackupPre()
358
    {
359
        $GLOBALS['singleton'] = Singleton::getInstance();
360
        self::$_testStatic    = 123;
361
    }
362
363
    /**
364
     * @depends testStaticAttributesBackupPre
365
     */
366
    public function testStaticAttributesBackupPost()
367
    {
368
        $this->assertNotSame($GLOBALS['singleton'], Singleton::getInstance());
369
        $this->assertSame(0, self::$_testStatic);
370
    }
371
372
    public function testIsInIsolationReturnsFalse()
373
    {
374
        $test   = new IsolationTest('testIsInIsolationReturnsFalse');
375
        $result = $test->run();
376
377
        $this->assertEquals(1, count($result));
378
        $this->assertTrue($result->wasSuccessful());
379
    }
380
381
    public function testIsInIsolationReturnsTrue()
382
    {
383
        $test   = new IsolationTest('testIsInIsolationReturnsTrue');
384
        $test->setRunTestInSeparateProcess(true);
385
        $result = $test->run();
386
387
        $this->assertEquals(1, count($result));
388
        $this->assertTrue($result->wasSuccessful());
389
    }
390
391
    public function testExpectOutputStringFooActualFoo()
392
    {
393
        $test   = new OutputTestCase('testExpectOutputStringFooActualFoo');
394
        $result = $test->run();
395
396
        $this->assertEquals(1, count($result));
397
        $this->assertTrue($result->wasSuccessful());
398
    }
399
400
    public function testExpectOutputStringFooActualBar()
401
    {
402
        $test   = new OutputTestCase('testExpectOutputStringFooActualBar');
403
        $result = $test->run();
404
405
        $this->assertEquals(1, count($result));
406
        $this->assertFalse($result->wasSuccessful());
407
    }
408
409
    public function testExpectOutputRegexFooActualFoo()
410
    {
411
        $test   = new OutputTestCase('testExpectOutputRegexFooActualFoo');
412
        $result = $test->run();
413
414
        $this->assertEquals(1, count($result));
415
        $this->assertTrue($result->wasSuccessful());
416
    }
417
418
    public function testExpectOutputRegexFooActualBar()
419
    {
420
        $test   = new OutputTestCase('testExpectOutputRegexFooActualBar');
421
        $result = $test->run();
422
423
        $this->assertEquals(1, count($result));
424
        $this->assertFalse($result->wasSuccessful());
425
    }
426
427
    public function testSkipsIfRequiresHigherVersionOfPHPUnit()
428
    {
429
        $test   = new RequirementsTest('testAlwaysSkip');
430
        $result = $test->run();
431
432
        $this->assertEquals(1, $result->skippedCount());
433
        $this->assertEquals(
434
            'PHPUnit 1111111 (or later) is required.',
435
            $test->getStatusMessage()
436
        );
437
    }
438
439
    public function testSkipsIfRequiresHigherVersionOfPHP()
440
    {
441
        $test   = new RequirementsTest('testAlwaysSkip2');
442
        $result = $test->run();
443
444
        $this->assertEquals(1, $result->skippedCount());
445
        $this->assertEquals(
446
            'PHP 9999999 (or later) is required.',
447
            $test->getStatusMessage()
448
        );
449
    }
450
451
    public function testSkipsIfRequiresNonExistingOs()
452
    {
453
        $test   = new RequirementsTest('testAlwaysSkip3');
454
        $result = $test->run();
455
456
        $this->assertEquals(1, $result->skippedCount());
457
        $this->assertEquals(
458
            'Operating system matching /DOESNOTEXIST/i is required.',
459
            $test->getStatusMessage()
460
        );
461
    }
462
463
    public function testSkipsIfRequiresNonExistingFunction()
464
    {
465
        $test   = new RequirementsTest('testNine');
466
        $result = $test->run();
467
468
        $this->assertEquals(1, $result->skippedCount());
469
        $this->assertEquals(
470
            'Function testFunc is required.',
471
            $test->getStatusMessage()
472
        );
473
    }
474
475
    public function testSkipsIfRequiresNonExistingExtension()
476
    {
477
        $test   = new RequirementsTest('testTen');
478
        $result = $test->run();
479
480
        $this->assertEquals(
481
            'Extension testExt is required.',
482
            $test->getStatusMessage()
483
        );
484
    }
485
486
    public function testSkipsProvidesMessagesForAllSkippingReasons()
487
    {
488
        $test   = new RequirementsTest('testAllPossibleRequirements');
489
        $result = $test->run();
490
491
        $this->assertEquals(
492
            'PHP 99-dev (or later) is required.' . PHP_EOL .
493
            'PHPUnit 9-dev (or later) is required.' . PHP_EOL .
494
            'Operating system matching /DOESNOTEXIST/i is required.' . PHP_EOL .
495
            'Function testFuncOne is required.' . PHP_EOL .
496
            'Function testFuncTwo is required.' . PHP_EOL .
497
            'Extension testExtOne is required.' . PHP_EOL .
498
            'Extension testExtTwo is required.',
499
            $test->getStatusMessage()
500
        );
501
    }
502
503
    public function testRequiringAnExistingMethodDoesNotSkip()
504
    {
505
        $test   = new RequirementsTest('testExistingMethod');
506
        $result = $test->run();
507
        $this->assertEquals(0, $result->skippedCount());
508
    }
509
510
    public function testRequiringAnExistingFunctionDoesNotSkip()
511
    {
512
        $test   = new RequirementsTest('testExistingFunction');
513
        $result = $test->run();
514
        $this->assertEquals(0, $result->skippedCount());
515
    }
516
517
    public function testRequiringAnExistingExtensionDoesNotSkip()
518
    {
519
        $test   = new RequirementsTest('testExistingExtension');
520
        $result = $test->run();
521
        $this->assertEquals(0, $result->skippedCount());
522
    }
523
524
    public function testRequiringAnExistingOsDoesNotSkip()
525
    {
526
        $test   = new RequirementsTest('testExistingOs');
527
        $result = $test->run();
528
        $this->assertEquals(0, $result->skippedCount());
529
    }
530
531
    public function testCurrentWorkingDirectoryIsRestored()
532
    {
533
        $expectedCwd = getcwd();
534
535
        $test = new ChangeCurrentWorkingDirectoryTest('testSomethingThatChangesTheCwd');
536
        $test->run();
537
538
        $this->assertSame($expectedCwd, getcwd());
539
    }
540
541
    /**
542
     * @requires PHP 7
543
     * @expectedException TypeError
544
     */
545
    public function testTypeErrorCanBeExpected()
546
    {
547
        $o = new ClassWithScalarTypeDeclarations;
548
        $o->foo(null, null);
549
    }
550
}
551