Improved PHPSpec plugin. Now includes a UI Plugin.

This commit is contained in:
michael.schramm 2014-11-05 01:15:37 +01:00 committed by Dan Cryer
parent 0b8b2fd364
commit aa27bf80c7
2 changed files with 163 additions and 2 deletions

View file

@ -58,10 +58,91 @@ class PhpSpec implements PHPCI\Plugin
return false;
}
$success = $this->phpci->executeCommand($phpspec . ' --format=pretty --no-code-generation run');
$success = $this->phpci->executeCommand($phpspec . ' --format=junit --no-code-generation run');
$output = $this->phpci->getLastOutput();
chdir($curdir);
/*
* process xml output
*
* <testsuites time=FLOAT tests=INT failures=INT errors=INT>
* <testsuite name=STRING time=FLOAT tests=INT failures=INT errors=INT skipped=INT>
* <testcase name=STRING time=FLOAT classname=STRING status=STRING/>
* </testsuite>
* </testsuites
*/
$xml = new \SimpleXMLElement($output);
$attr = $xml->attributes();
$data = array(
'time' => (float)$attr['time'],
'tests' => (int)$attr['tests'],
'failures' => (int)$attr['failures'],
'errors' => (int)$attr['errors'],
// now all the tests
'suites' => array()
);
/**
* @var \SimpleXMLElement $group
*/
foreach ($xml->xpath('testsuite') as $group) {
$attr = $group->attributes();
$suite = array(
'name' => (String)$attr['name'],
'time' => (float)$attr['time'],
'tests' => (int)$attr['tests'],
'failures' => (int)$attr['failures'],
'errors' => (int)$attr['errors'],
'skipped' => (int)$attr['skipped'],
// now the cases
'cases' => array()
);
/**
* @var \SimpleXMLElement $child
*/
foreach ($group->xpath('testcase') as $child) {
$attr = $child->attributes();
$case = array(
'name' => (String)$attr['name'],
'classname' => (String)$attr['classname'],
'time' => (float)$attr['time'],
'status' => (String)$attr['status'],
);
if ($case['status']=='failed') {
$error = array();
/*
* ok, sad, we had an error
*
* there should be one - foreach makes this easier
*/
foreach ($child->xpath('failure') as $failure) {
$attr = $failure->attributes();
$error['type'] = (String)$attr['type'];
$error['message'] = (String)$attr['message'];
}
foreach ($child->xpath('system-err') as $system_err) {
$error['raw'] = (String)$system_err;
}
$case['error'] = $error;
}
$suite['cases'][] = $case;
}
$data['suites'][] = $suite;
}
$this->build->storeMeta('phpspec', $data);
return $success;
}
}

View file

@ -0,0 +1,80 @@
var phpspecPlugin = PHPCI.UiPlugin.extend({
id: 'build-phpspec-errors',
css: 'col-lg-12 col-md-12 col-sm-12 col-xs-12',
title: 'PHPSpec',
lastData: null,
displayOnUpdate: false,
box: true,
rendered: false,
register: function() {
var self = this;
var query = PHPCI.registerQuery('phpspec', -1, {key: 'phpspec'})
$(window).on('phpspec', function(data) {
self.onUpdate(data);
});
$(window).on('build-updated', function() {
if (!self.rendered) {
self.displayOnUpdate = true;
query();
}
});
},
render: function() {
return $('<table class="table table-striped" id="phpspec-data">' +
'<thead>' +
'<tr>' +
' <th>Suite</th>' +
' <th>Test</th>' +
' <th>Result</th>' +
'</tr>' +
'</thead><tbody></tbody></table>');
},
onUpdate: function(e) {
if (!e.queryData) {
$('#build-phpspec-errors').hide();
return;
}
this.rendered = true;
this.lastData = e.queryData;
var tests = this.lastData[0].meta_value;
var tbody = $('#phpspec-data tbody');
tbody.empty();
for (var i in tests.suites) {
var test_suite = tests.suites[i];
for(var k in test_suite.cases){
var test_case = test_suite.cases[k];
var row = $(
'<tr>'+
'<td>'+test_suite.name+'</td>'+
'<td title="Took '+test_case['time']+'Seconds">'+test_case.name+'</td>'+
'<td>'+(test_case.message?test_case.message:'OK')+'</td>'+
'</tr>'
);
if (test_case.status!='passed') {
row.addClass('danger');
} else {
row.addClass('success');
}
tbody.append(row);
}
}
// show plugin once preparation of grid is done
$('#build-phpspec-errors').show();
}
});
PHPCI.registerPlugin(new phpspecPlugin());