phpci/PHPCI/Plugin/Behat.php

132 lines
3.2 KiB
PHP
Raw Normal View History

2013-10-08 11:26:11 +02:00
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
2013-10-08 11:26:11 +02:00
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @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;
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
/**
* 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 = '';
if (isset($options['executable'])) {
$this->executable = $options['executable'];
} else {
2014-05-16 16:14:14 +02:00
$this->executable = $this->phpci->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()
{
$curdir = getcwd();
chdir($this->phpci->buildPath);
$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;
}
$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]);
$errorCount = 0;
$storeFailures = false;
$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]
);
$this->build->reportError($this->phpci, $lineParts[0], $lineParts[1], 'Behat scenario failed.');
2015-02-18 17:51:55 +01:00
}
}
$errorCount = count($data);
return array($errorCount, $data);
}
2013-10-08 11:26:11 +02:00
}