2013-10-08 11:26:11 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
2014-05-12 18:26:17 +02:00
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
2013-10-08 11:26:11 +02:00
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
2014-05-12 18:26:17 +02:00
|
|
|
* @link https://www.phptesting.org/
|
2013-10-08 11:26:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PHPCI\Plugin;
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
use PHPCI\Builder;
|
2014-12-04 16:48:52 +01:00
|
|
|
use PHPCI\Helper\Lang;
|
2013-10-10 02:01:06 +02:00
|
|
|
use PHPCI\Model\Build;
|
2015-10-15 11:07:54 +02:00
|
|
|
use PHPCI\Model\BuildError;
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2013-10-08 11:26:11 +02:00
|
|
|
/**
|
|
|
|
* Behat BDD Plugin
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
|
|
|
class Behat implements \PHPCI\Plugin
|
|
|
|
{
|
|
|
|
protected $phpci;
|
2014-05-02 15:48:40 +02:00
|
|
|
protected $build;
|
2013-10-10 02:01:06 +02:00
|
|
|
protected $features;
|
2013-10-08 11:26:11 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Standard Constructor
|
|
|
|
*
|
|
|
|
* $options['directory'] Output Directory. Default: %BUILDPATH%
|
|
|
|
* $options['filename'] Phar Filename. Default: build.phar
|
|
|
|
* $options['regexp'] Regular Expression Filename Capture. Default: /\.php$/
|
|
|
|
* $options['stub'] Stub Content. No Default Value
|
|
|
|
*
|
|
|
|
* @param Builder $phpci
|
|
|
|
* @param Build $build
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = array())
|
2013-10-08 11:26:11 +02:00
|
|
|
{
|
2014-05-16 16:14:14 +02:00
|
|
|
$this->phpci = $phpci;
|
|
|
|
$this->build = $build;
|
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 {
|
2014-05-16 16:14:14 +02:00
|
|
|
$this->executable = $this->phpci->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()
|
|
|
|
{
|
|
|
|
$curdir = getcwd();
|
|
|
|
chdir($this->phpci->buildPath);
|
|
|
|
|
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) {
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->phpci->logFailure(Lang::get('could_not_find', 'behat'));
|
2015-02-18 17:51:55 +01:00
|
|
|
|
2013-10-08 11:26:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-09 14:04:09 +02:00
|
|
|
$success = $this->phpci->executeCommand($behat . ' %s', $this->features);
|
2013-10-08 11:26:11 +02:00
|
|
|
chdir($curdir);
|
|
|
|
|
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->phpci->getLastOutput();
|
|
|
|
|
|
|
|
$parts = explode('---', $output);
|
|
|
|
|
|
|
|
if (count($parts) <= 1) {
|
|
|
|
return array(0, array());
|
|
|
|
}
|
|
|
|
|
|
|
|
$lines = explode(PHP_EOL, $parts[1]);
|
|
|
|
|
|
|
|
$storeFailures = false;
|
2015-02-20 14:37:36 +01:00
|
|
|
$data = array();
|
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);
|
|
|
|
$data[] = array(
|
|
|
|
'file' => $lineParts[0],
|
|
|
|
'line' => $lineParts[1]
|
|
|
|
);
|
2015-02-20 14:37:36 +01:00
|
|
|
|
2015-10-15 11:07:54 +02:00
|
|
|
$this->build->reportError(
|
|
|
|
$this->phpci,
|
|
|
|
'behat',
|
|
|
|
'Behat scenario failed.',
|
|
|
|
BuildError::SEVERITY_HIGH,
|
|
|
|
$lineParts[0],
|
|
|
|
$lineParts[1]
|
|
|
|
);
|
2015-02-18 17:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$errorCount = count($data);
|
|
|
|
|
|
|
|
return array($errorCount, $data);
|
|
|
|
}
|
2013-10-08 11:26:11 +02:00
|
|
|
}
|