/
Report.php
170 lines (150 loc) · 3.89 KB
/
Report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2009, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\test;
use \Exception;
use \lithium\core\Libraries;
use \lithium\util\Inflector;
/**
* Report object for running group tests holding results
*
* @package default
*/
class Report extends \lithium\core\Object {
/**
* Contains an instance of `lithium\test\Group`, which contains all unit tests to be executed
* this test run.
*
* @var object
*/
public $group = null;
/**
* Contains an instance of `lithium\test\Reporter`, which contains the format to be displayed
*
* @var object
*/
public $reporter = null;
/**
* An array of fully-namespaced class names representing the filters to be applied to this test
* group.
*
* @var array
*/
public $filters = array();
/**
* Title of the group being run
*
* @var string
*/
public $title = null;
/**
* group and filter results
*
* @var array
*/
public $results = array('group' => array(), 'filters' => array());
/**
* start and end timers
*
* @var array
*/
public $timer = array('start' => null, 'end' => null);
/**
* Construct Report Object
*
* @param array $config Options array for the test run. Valid options are:
* - 'group': The test group with items to be run.
* - 'filters': An array of filters that the test output should be run through.
*/
public function __construct($config = array()) {
$defaults = array(
'title' => null,
'group' => null,
'filters' => array(),
'reporter' => 'text'
);
parent::__construct((array) $config + $defaults);
}
/**
* undocumented function
*
* @return void
*/
protected function _init() {
$class = Inflector::camelize($this->_config['reporter']);
if (!$reporter = Libraries::locate('test.reporter', $class)) {
throw new Exception("{$class} is not a valid reporter");
}
$this->reporter = new $reporter();
$this->group = $this->_config['group'];
$this->filters = $this->_config['filters'];
$this->title = $this->_config['title'] ?: $this->_config['title'];
}
/**
* undocumented function
*
* @return void
*/
public function run() {
$this->timer['start'] = microtime(true);
$tests = $this->group->tests();
foreach ($this->filters as $filter => $options) {
$options = isset($options['apply']) ? $options['apply'] : array();
$tests = $filter::apply($tests, $options);
}
$this->results['group'] = $tests->run();
foreach ($this->filters as $filter => $options) {
$options = isset($options['analyze']) ? $options['analyze'] : array();
$this->results['filters'][$filter] = $filter::analyze($this->results['group'], $options);
}
$this->timer['end'] = microtime(true);
}
/**
* undocumented function
*
* @return void
*/
public function stats() {
$results = (array) $this->results['group'];
return $this->reporter->stats(array_reduce($results, function($stats, $result) {
$stats = (array) $stats + array(
'asserts' => 0,
'passes' => array(),
'fails' => array(),
'exceptions' => array(),
'errors' => array()
);
$result = empty($result[0]) ? array($result) : $result;
foreach ($result as $response) {
if (empty($response['result'])) {
continue;
}
$result = $response['result'];
if (in_array($result, array('fail', 'exception'))) {
$stats['errors'][] = $response;
}
unset($response['file'], $response['result']);
if (in_array($result, array('pass', 'fail'))) {
$stats['asserts']++;
}
if (in_array($result, array('pass', 'fail', 'exception'))) {
$stats[Inflector::pluralize($result)][] = $response;
}
}
return $stats;
}));
}
/**
* undocumented function
*
* @return void
*/
public function filters() {
return $this->reporter->filters((array) $this->results['filters']);
}
}
?>