php-censor/src/Plugin/Behat.php

129 lines
2.9 KiB
PHP
Raw Normal View History

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
*
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';
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
2013-10-08 11:26:11 +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 = '';
if (isset($options['executable'])) {
$this->executable = $options['executable'];
} else {
$this->executable = $this->findBinary('behat');
}
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()
{
$currentDir = getcwd();
chdir($this->builder->buildPath);
2013-10-08 11:26:11 +02:00
$behat = $this->executable;
2013-10-08 11:26:11 +02:00
2013-10-10 02:01:06 +02:00
if (!$behat) {
$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;
}
$success = $this->builder->executeCommand($behat . ' %s', $this->features);
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()
{
$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
];
$this->build->reportError(
$this->builder,
'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
}