2013-10-08 11:26:11 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-10-08 11:26:11 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Model\BuildError;
|
|
|
|
use PHPCensor\Plugin;
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2013-10-08 11:26:11 +02:00
|
|
|
/**
|
|
|
|
* Behat BDD Plugin
|
2018-03-08 04:54:52 +01:00
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
2013-10-08 11:26:11 +02:00
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class Behat extends Plugin
|
2013-10-08 11:26:11 +02:00
|
|
|
{
|
2013-10-10 02:01:06 +02:00
|
|
|
protected $features;
|
2016-07-11 18:00:04 +02:00
|
|
|
protected $executable;
|
2013-10-08 11:26:11 +02:00
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'behat';
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
2016-07-11 18:00:04 +02:00
|
|
|
* {@inheritdoc}
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2013-10-08 11:26:11 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2016-07-11 18:00:04 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$this->features = '';
|
|
|
|
|
2014-01-10 10:58:14 +01:00
|
|
|
if (isset($options['executable'])) {
|
|
|
|
$this->executable = $options['executable'];
|
|
|
|
} else {
|
2017-07-19 05:26:51 +02:00
|
|
|
$this->executable = $this->findBinary('behat');
|
2014-01-10 10:58:14 +01:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (!empty($options['features'])) {
|
|
|
|
$this->features = $options['features'];
|
|
|
|
}
|
2013-10-08 11:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs Behat tests.
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
2018-03-08 04:54:52 +01:00
|
|
|
$currentDir = getcwd();
|
2016-07-22 09:05:34 +02:00
|
|
|
chdir($this->builder->buildPath);
|
2013-10-08 11:26:11 +02:00
|
|
|
|
2014-01-10 10:58:14 +01:00
|
|
|
$behat = $this->executable;
|
2013-10-08 11:26:11 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (!$behat) {
|
2017-02-07 13:05:44 +01:00
|
|
|
$this->builder->logFailure(sprintf('Could not find %s', 'behat'));
|
2015-02-18 17:51:55 +01:00
|
|
|
|
2013-10-08 11:26:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$success = $this->builder->executeCommand($behat . ' %s', $this->features);
|
2018-03-08 04:54:52 +01:00
|
|
|
chdir($currentDir);
|
2013-10-08 11:26:11 +02:00
|
|
|
|
2015-02-18 17:51:55 +01:00
|
|
|
list($errorCount, $data) = $this->parseBehatOutput();
|
|
|
|
|
|
|
|
$this->build->storeMeta('behat-warnings', $errorCount);
|
|
|
|
$this->build->storeMeta('behat-data', $data);
|
|
|
|
|
2013-10-08 11:26:11 +02:00
|
|
|
return $success;
|
|
|
|
}
|
2015-02-18 17:51:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the behat output and return details on failures
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function parseBehatOutput()
|
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
$output = $this->builder->getLastOutput();
|
2015-02-18 17:51:55 +01:00
|
|
|
|
|
|
|
$parts = explode('---', $output);
|
|
|
|
|
|
|
|
if (count($parts) <= 1) {
|
2016-04-21 06:58:09 +02:00
|
|
|
return [0, []];
|
2015-02-18 17:51:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$lines = explode(PHP_EOL, $parts[1]);
|
|
|
|
|
|
|
|
$storeFailures = false;
|
2016-04-21 06:58:09 +02:00
|
|
|
$data = [];
|
2015-02-18 17:51:55 +01:00
|
|
|
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$line = trim($line);
|
|
|
|
if ($line == 'Failed scenarios:') {
|
|
|
|
$storeFailures = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($line, ':') === false) {
|
|
|
|
$storeFailures = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storeFailures) {
|
|
|
|
$lineParts = explode(':', $line);
|
2016-04-21 06:58:09 +02:00
|
|
|
$data[] = [
|
2015-02-18 17:51:55 +01:00
|
|
|
'file' => $lineParts[0],
|
|
|
|
'line' => $lineParts[1]
|
2016-04-21 06:58:09 +02:00
|
|
|
];
|
2015-02-20 14:37:36 +01:00
|
|
|
|
2015-10-15 11:07:54 +02:00
|
|
|
$this->build->reportError(
|
2016-07-22 09:05:34 +02:00
|
|
|
$this->builder,
|
2015-10-15 11:07:54 +02:00
|
|
|
'behat',
|
|
|
|
'Behat scenario failed.',
|
|
|
|
BuildError::SEVERITY_HIGH,
|
|
|
|
$lineParts[0],
|
|
|
|
$lineParts[1]
|
|
|
|
);
|
2015-02-18 17:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$errorCount = count($data);
|
|
|
|
|
2016-04-21 06:58:09 +02:00
|
|
|
return [$errorCount, $data];
|
2015-02-18 17:51:55 +01:00
|
|
|
}
|
2013-10-08 11:26:11 +02:00
|
|
|
}
|