/
Home.php
160 lines (136 loc) · 5.32 KB
/
Home.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
<?php
namespace app\controller;
class Home extends \app\core\Controller {
protected function _create_snapshot($view_data) {
$filename = realpath(
\app\lib\Library::retrieve('snapshot_directory')
) . '/' . date('Y-m-d_H-i') . '.html';
$contents = $this->render_html('partial/test_results', $view_data);
$handle = @fopen($filename, 'a');
if ( !$handle ) {
return array(
'type' => 'failed',
'title' => 'Error Creating Snapshot',
'message' => 'Please ensure that the '
. '<code>snapshot_directory</code> in '
. '<code>app/config/bootstrap.php</code> exists and '
. 'has the proper permissions.'
);
}
fwrite($handle, $contents);
fclose($handle);
return array(
'type' => 'succeeded',
'title' => 'Snapshot Created',
'message' => "Snapshot can be found at <code>{$filename}</code>."
);
}
// GET
public function help($request) {
return array();
}
// GET/POST
public function index($request) {
if ( $request->is('get') ) {
$normalize_path = function($path) {
return str_replace('\\', '/', realpath($path));
};
$test_directories = json_encode(array_map(
$normalize_path, \app\lib\Library::retrieve('test_directories')
));
$suites = array();
$stats = array();
$store_statistics = \app\lib\Library::retrieve('store_statistics');
$create_snapshots = \app\lib\Library::retrieve('create_snapshots');
$sandbox_errors = \app\lib\Library::retrieve('sandbox_errors');
$xml_configuration_files = \app\lib\Library::retrieve(
'xml_configuration_files'
);
return compact(
'create_snapshots',
'sandbox_errors',
'stats',
'store_statistics',
'suites',
'test_directories',
'xml_configuration_files'
);
}
$tests = explode('|', $request->data['test_files']);
$vpu = new \app\lib\VPU();
if ( $request->data['sandbox_errors'] ) {
error_reporting(\app\lib\Library::retrieve('error_reporting'));
set_error_handler(array($vpu, 'handle_errors'));
}
$xml_config = false;
$notifications = array();
if ( $xml_file_index = $request->data['xml_configuration_file'] ) {
$files = \app\lib\Library::retrieve('xml_configuration_files');
$xml_config = $files[$xml_file_index - 1];
if ( !$xml_config || !$xml_config = realpath($xml_config) ) {
$notifications[] = array(
'type' => 'failed',
'title' => 'No Valid XML Configuration File Found',
'message' => 'Please ensure that the '
. '<code>xml_configuration_file</code> in '
. '<code>app/config/bootstrap.php</code> exists and '
. 'has the proper permissions.'
);
}
}
$results = ( $xml_config )
? $vpu->run_with_xml($xml_config)
: $vpu->run_tests($tests);
$results = $vpu->compile_suites($results, 'web');
if ( $request->data['sandbox_errors'] ) {
restore_error_handler();
}
$suites = $results['suites'];
$stats = $results['stats'];
$errors = $vpu->get_errors();
$to_view = compact('suites', 'stats', 'errors');
if ( $request->data['create_snapshots'] ) {
$notifications[] = $this->_create_snapshot($to_view);
}
if ( $request->data['store_statistics'] ) {
$notifications[] = $this->_store_statistics($stats);
}
return $to_view + compact('notifications');
}
protected function _store_statistics($stats) {
$db_options = \app\lib\Library::retrieve('db');
$db = new $db_options['plugin']();
if ( !$db->connect($db_options) ) {
return array(
'type' => 'failed',
'title' => 'Error Connecting to Database',
'message' => implode(' ', $db->get_errors())
);
}
$now = date('Y-m-d H:i:s');
foreach ( $stats as $key => $stat ) {
$data = array(
'run_date' => $now,
'failed' => $stat['failed'],
'incomplete' => $stat['incomplete'],
'skipped' => $stat['skipped'],
'succeeded' => $stat['succeeded']
);
$table = ucfirst(rtrim($key, 's')) . 'Result';
if ( !$db->insert($table, $data) ) {
return array(
'type' => 'failed',
'title' => 'Error Inserting Record',
'message' => implode(' ', $db->get_errors())
);
}
}
return array(
'type' => 'succeeded',
'title' => 'Statistics Stored',
'message' => 'The statistics generated during this test run were '
. 'successfully stored.'
);
}
}
?>