Completed
Push — master ( 2fdc96...4f1f24 )
by Damian
12:09
created

CliTestReporter::writeResults()   C

Complexity

Conditions 15
Paths 54

Size

Total Lines 63
Code Lines 42

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 63
rs 6.0778
cc 15
eloc 42
nc 54
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if(!class_exists('SapphireTestReporter')) {
4
	return;
5
}
6
7
/**
8
 * Test reporter optimised for CLI (ie, plain-text) output
9
 *
10
 * @package framework
11
 * @subpackage testing
12
 */
13
class CliTestReporter extends SapphireTestReporter {
14
15
	/**
16
	 * Display error bar if it exists
17
	 */
18
	public function writeResults() {
0 ignored issues
show
Coding Style introduced by
writeResults uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
19
		$passCount = 0;
20
		$failCount = $this->currentSession['failures'];
21
		$testCount = 0;
22
		$incompleteCount = $this->currentSession['incomplete'];
23
		$errorCount = $this->currentSession['errors'];
24
25
		foreach($this->suiteResults['suites'] as $suite) {
26
			foreach($suite['tests'] as $test) {
27
				$testCount++;
28
				switch($test['status']) {
29
					case TEST_INCOMPLETE: {
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
30
						$incompleteCount++;
31
						break;
32
					}
33
					case TEST_SUCCESS: {
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
34
						$passCount++;
35
						break;
36
					}
37
					case TEST_ERROR: {
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
38
						$errorCount++;
39
						break;
40
					}
41
					default: {
0 ignored issues
show
Coding Style introduced by
As per PSR2, default statements should not use curly braces.

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
42
						$failCount++;
43
						break;
44
					}
45
				}
46
			}
47
		}
48
49
		echo "\n\n";
50
		$breakages = $errorCount + $failCount;
51
		if ($breakages == 0 && $incompleteCount > 0) {
52
			echo SS_Cli::text(" OK, BUT INCOMPLETE TESTS! ", "black", "yellow");
53
		} elseif ($breakages == 0) {
54
			echo SS_Cli::text(" ALL TESTS PASS ", "black", "green");
55
		}  else {
56
			echo SS_Cli::text(" AT LEAST ONE FAILURE ", "black", "red");
57
		}
58
59
		echo sprintf("\n\n%d tests run: %s, %s, and %s\n", $testCount, SS_Cli::text("$passCount passes"),
60
			SS_Cli::text("$breakages failures"), SS_Cli::text("$incompleteCount incomplete"));
61
62
		echo "Maximum memory usage: " . number_format(memory_get_peak_usage()/(1024*1024), 1) . "M\n\n";
63
64
		// Use sake dev/tests/all --showslow to show slow tests
65
		if((isset($_GET['args']) && is_array($_GET['args']) && in_array('--showslow', $_GET['args']))
66
				|| isset($_GET['showslow'])) {
67
68
			$avgSpeed = round(array_sum($this->testSpeeds) / count($this->testSpeeds), 3);
69
			echo "Slow tests (more than the average $avgSpeed seconds):\n";
70
71
			arsort($this->testSpeeds);
72
			foreach($this->testSpeeds as $k => $v) {
73
				// Ignore below-average speeds
74
				if($v < $avgSpeed) break;
75
76
				echo " - $k: " . round($v,3) . "\n";
77
			}
78
		}
79
		echo "\n";
80
	}
81
82
	public function endTest( PHPUnit_Framework_Test $test, $time) {
83
		// Status indicator, a la PHPUnit
84
		switch($this->currentTest['status']) {
85
			case TEST_FAILURE: echo SS_Cli::text("F","red", null, true); break;
86
			case TEST_ERROR: echo SS_Cli::text("E","red", null, true); break;
87
			case TEST_INCOMPLETE: echo SS_Cli::text("I","yellow"); break;
88
			case TEST_SUCCESS: echo SS_Cli::text(".","green"); break;
89
			default: echo SS_Cli::text("?", "yellow"); break;
90
		}
91
92
		static $colCount = 0;
93
		$colCount++;
94
		if($colCount % 80 == 0) echo " - $colCount\n";
95
96
		$this->writeTest($this->currentTest);
97
		parent::endTest($test, $time);
98
	}
99
100
	protected function addStatus($status, $message, $exception, $trace) {
101
		if(!$this->currentTest && !$this->currentSuite) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->currentTest of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $this->currentSuite of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
102
			// Log non-test errors immediately
103
			$statusResult = array(
104
				'status' => $status,
105
				'message' => $message,
106
				'exception' => $exception,
107
				'trace' => $trace
108
			);
109
			$this->writeTest($statusResult);
110
		}
111
		parent::addStatus($status, $message, $exception, $trace);
112
	}
113
114
	protected function writeTest($test) {
115
		if ($test['status'] != TEST_SUCCESS) {
116
			$filteredTrace = array();
117
			foreach($test['trace'] as $item) {
118
				if(isset($item['file'])
119
						&& strpos($item['file'], 'PHPUnit/Framework') === false
120
						&& !isset($item['class'])) {
121
					$filteredTrace[] = $item;
122
				}
123
124
				if(isset($item['class']) && isset($item['function']) && $item['class'] == 'PHPUnit_Framework_TestSuite'
125
						&& $item['function'] == 'run') {
126
					break;
127
				}
128
129
			}
130
131
			$color = ($test['status'] == 2) ? 'yellow' : 'red';
132
			echo "\n" . SS_Cli::text($test['name'] . "\n". $test['message'] . "\n", $color, null);
133
			echo SS_Backtrace::get_rendered_backtrace($filteredTrace, true);
134
			echo "--------------------\n";
135
		}
136
	}
137
138
}
139