php-censor/src/PHPCensor/Plugin/PhpSpec.php

119 lines
3.4 KiB
PHP
Raw Normal View History

2013-05-14 15:37:14 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
2013-05-14 15:37:14 +02:00
2016-07-19 20:28:11 +02:00
use PHPCensor;
2016-07-11 18:00:04 +02:00
use PHPCensor\Plugin;
2013-10-10 02:01:06 +02:00
/**
* PHP Spec Plugin - Allows PHP Spec testing.
*
2017-03-04 16:39:56 +01:00
* @author Dan Cryer <dan@block8.co.uk>
*/
2016-07-11 18:00:04 +02:00
class PhpSpec extends Plugin
2013-05-14 15:37:14 +02:00
{
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'php_spec';
}
/**
* Runs PHP Spec tests.
*/
public function execute()
{
$curdir = getcwd();
chdir($this->builder->buildPath);
2013-10-08 09:50:10 +02:00
$phpspec = $this->builder->findBinary(['phpspec', 'phpspec.php']);
2013-10-08 09:50:10 +02:00
$success = $this->builder->executeCommand($phpspec . ' --format=junit --no-code-generation run');
$output = $this->builder->getLastOutput();
2013-10-10 02:01:06 +02:00
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();
2016-04-21 06:58:09 +02:00
$data = [
'time' => (float)$attr['time'],
'tests' => (int)$attr['tests'],
'failures' => (int)$attr['failures'],
2016-04-21 06:58:09 +02:00
'errors' => (int)$attr['errors'],
// now all the tests
2016-04-21 06:58:09 +02:00
'suites' => []
];
/**
* @var \SimpleXMLElement $group
*/
foreach ($xml->xpath('testsuite') as $group) {
2016-04-21 06:58:09 +02:00
$attr = $group->attributes();
$suite = [
'name' => (String)$attr['name'],
'time' => (float)$attr['time'],
'tests' => (int)$attr['tests'],
'failures' => (int)$attr['failures'],
2016-04-21 06:58:09 +02:00
'errors' => (int)$attr['errors'],
'skipped' => (int)$attr['skipped'],
// now the cases
2016-04-21 06:58:09 +02:00
'cases' => []
];
/**
* @var \SimpleXMLElement $child
*/
foreach ($group->xpath('testcase') as $child) {
$attr = $child->attributes();
2016-04-21 06:58:09 +02:00
$case = [
'name' => (String)$attr['name'],
'classname' => (String)$attr['classname'],
2016-04-21 06:58:09 +02:00
'time' => (float)$attr['time'],
'status' => (String)$attr['status'],
];
if ($case['status']=='failed') {
2016-04-21 06:58:09 +02:00
$error = [];
/*
* 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;
}
}