Testing   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
c 3
b 1
f 1
lcom 0
cbo 3
dl 0
loc 108
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C runUnitTests() 0 47 9
A coverageStart() 0 8 2
B coverageStop() 0 30 3
1
<?php
2
/**
3
 * Scabbia2 Testing Component
4
 * https://github.com/eserozvataf/scabbia2
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @link        https://github.com/eserozvataf/scabbia2-testing for the canonical source repository
10
 * @copyright   2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/)
11
 * @license     http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0
12
 */
13
14
namespace Scabbia\Testing;
15
16
use Scabbia\Helpers\FileSystem;
17
use Scabbia\Formatters\FormatterInterface;
18
use Scabbia\Formatters\Formatters;
19
20
/**
21
 * A small test implementation which helps us during the development of
22
 * Scabbia2 PHP Framework's itself and related production code
23
 *
24
 * @package     Scabbia\Testing
25
 * @author      Eser Ozvataf <[email protected]>
26
 * @since       2.0.0
27
 */
28
class Testing
29
{
30
    /**
31
     * Runs given unit tests
32
     *
33
     * @param array              $uTestClasses set of unit test classes
34
     * @param FormatterInterface $uFormatter   formatter class
35
     *
36
     * @return int exit code
37
     */
38
    public static function runUnitTests(array $uTestClasses, $uFormatter = null)
39
    {
40
        if ($uFormatter === null) {
41
            $uFormatter = Formatters::getCurrent();
42
        }
43
44
        $tIsEverFailed = false;
45
46
        $uFormatter->writeHeader(1, "Unit Tests");
47
48
        /** @type string $tTestClass */
49
        foreach ($uTestClasses as $tTestClass) {
50
            $uFormatter->writeHeader(2, $tTestClass);
51
52
            $tInstance = new $tTestClass ();
53
            $tInstance->test();
54
55
            if ($tInstance->isFailed) {
56
                $tIsEverFailed = true;
57
            }
58
59
            foreach ($tInstance->testReport as $tTestName => $tTest) {
60
                $tFails = [];
61
                foreach ($tTest as $tTestCase) {
62
                    if ($tTestCase["failed"]) {
63
                        $tFails[] = [
64
                            "operation" => $tTestCase["operation"],
65
                            "message" => $tTestCase["message"]
66
                        ];
67
                    }
68
                }
69
70
                if (count($tFails) === 0) {
71
                    $uFormatter->write(sprintf("[OK] %s", $tTestName));
72
                } else {
73
                    $uFormatter->writeColor("red", sprintf("[FAIL] %s", $tTestName));
74
                    $uFormatter->writeArray($tFails);
75
                }
76
            }
77
        }
78
79
        if ($tIsEverFailed) {
80
            return 1;
81
        }
82
83
        return 0;
84
    }
85
86
    /**
87
     * Starts the code coverage
88
     *
89
     * @return void
90
     */
91
    public static function coverageStart()
92
    {
93
        if (!extension_loaded("xdebug")) {
94
            return;
95
        }
96
97
        xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
98
    }
99
100
    /**
101
     * Stops the code coverage
102
     *
103
     * @return array results
104
     */
105
    public static function coverageStop()
106
    {
107
        if (!extension_loaded("xdebug")) {
108
            return null;
109
        }
110
111
        $tCoverageData = xdebug_get_code_coverage();
112
        xdebug_stop_code_coverage();
113
114
        $tFinal = [
115
            "files" => [],
116
            "total" => [ "coveredLines" => 0, "totalLines" => 0 ]
117
        ];
118
119
        foreach ($tCoverageData as $tPath => $tLines) {
120
            $tFileCoverage = [
121
                "path"         => $tPath,
122
                "coveredLines" => array_keys($tLines),
123
                "totalLines"   => FileSystem::getFileLineCount($tPath)
124
            ];
125
126
            $tFinal["files"][] = $tFileCoverage;
127
            $tFinal["total"]["coveredLines"] += count($tFileCoverage["coveredLines"]);
128
            $tFinal["total"]["totalLines"] += $tFileCoverage["totalLines"];
129
        }
130
131
        $tFinal["total"]["percentage"] = ($tFinal["total"]["coveredLines"] * 100) / $tFinal["total"]["totalLines"];
132
133
        return $tFinal;
134
    }
135
}
136